'use client'; import { useState, FormEvent, useEffect, useRef } from 'react'; import { Input } from '@/components/ui/Input'; import { Button } from '@/components/ui/Button'; import { Icons } from '@/components/ui/Icon'; import { SearchLoadingAnimation } from '@/components/SearchLoadingAnimation'; import { SearchHistoryDropdown } from '@/components/search/SearchHistoryDropdown'; import { useSearchHistoryStore } from '@/lib/store/search-history-store'; interface SearchFormProps { onSearch: (query: string) => void; onClear?: () => void; isLoading: boolean; initialQuery?: string; currentSource?: string; checkedSources?: number; totalSources?: number; } export function SearchForm({ onSearch, onClear, isLoading, initialQuery = '', currentSource = '', checkedSources = 0, totalSources = 16, }: SearchFormProps) { const [query, setQuery] = useState(initialQuery); const [showHistory, setShowHistory] = useState(false); const [inputRect, setInputRect] = useState(null); const inputRef = useRef(null); const { addSearchHistory } = useSearchHistoryStore(); // Update query when initialQuery changes useEffect(() => { setQuery(initialQuery); }, [initialQuery]); const handleSubmit = (e: FormEvent) => { e.preventDefault(); if (query.trim() && !isLoading) { addSearchHistory(query.trim()); onSearch(query); setShowHistory(false); } }; const handleClear = () => { setQuery(''); if (onClear) { onClear(); } setShowHistory(false); }; const handleInputFocus = () => { if (inputRef.current) { setInputRect(inputRef.current.getBoundingClientRect()); setShowHistory(true); } }; const handleHistorySelect = (selectedQuery: string) => { setQuery(selectedQuery); setShowHistory(false); onSearch(selectedQuery); }; return (
setQuery(e.target.value)} onFocus={handleInputFocus} placeholder="搜索电影、电视剧、综艺..." className="text-base sm:text-lg pr-24 md:pr-32 truncate" role="combobox" aria-expanded={showHistory && !isLoading} aria-controls="search-history-listbox" aria-autocomplete="list" aria-label="搜索视频内容" /> {query && ( )}
{/* Search History Dropdown */} setShowHistory(false)} inputRect={inputRect} /> {/* Loading Animation */} {isLoading && (
)} ); }