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 { SearchHistoryDropdown } from '@/components/search/SearchHistoryDropdown'; import { useSearchHistory } from '@/lib/hooks/useSearchHistory'; import { useSearchBoxHandlers } from './hooks/useSearchBoxHandlers'; interface SearchBoxProps { onSearch: (query: string) => void; onClear?: () => void; initialQuery?: string; placeholder?: string; } export function SearchBox({ onSearch, onClear, initialQuery = '', placeholder = '搜索电影、电视剧、综艺...' }: SearchBoxProps) { const [query, setQuery] = useState(initialQuery); const inputRef = useRef(null); // Search history hook const { searchHistory, isDropdownOpen, highlightedIndex, showDropdown, hideDropdown, addSearch, removeSearch, clearAll, selectHistoryItem, navigateDropdown, resetHighlight, } = useSearchHistory((selectedQuery) => { setQuery(selectedQuery); onSearch(selectedQuery); // Blur the input after selecting from history inputRef.current?.blur(); }); // Update query when initialQuery changes useEffect(() => { setQuery(initialQuery); }, [initialQuery]); const { handleSubmit, handleClear, handleInputFocus, handleInputBlur, handleKeyDown, } = useSearchBoxHandlers({ query, setQuery, onSearch, onClear, inputRef, isDropdownOpen, highlightedIndex, searchHistory, addSearch, hideDropdown, showDropdown, resetHighlight, selectHistoryItem, navigateDropdown, }); return (
setQuery(e.target.value)} onFocus={handleInputFocus} onBlur={handleInputBlur} onKeyDown={handleKeyDown} placeholder={placeholder} className="text-base sm:text-lg pr-28 sm:pr-36 md:pr-44 truncate" aria-label="搜索视频内容" aria-expanded={isDropdownOpen} aria-controls="search-history-dropdown" aria-autocomplete="list" />
{query && ( )}
{/* Search History Dropdown */} ); }