import { useState, useRef } from 'react'; interface CompactSearchBoxProps { onSearch: (query: string) => void; initialQuery?: string; searchTime?: string; } export function CompactSearchBox({ onSearch, initialQuery = '', searchTime = '搜索用时 0.3秒' }: CompactSearchBoxProps) { const [searchQuery, setSearchQuery] = useState(initialQuery); const textareaRef = useRef(null); const handleSearchInputChange = (e: React.ChangeEvent) => { const value = e.target.value; setSearchQuery(value); // 自动调整高度 if (textareaRef.current) { textareaRef.current.style.height = 'auto'; textareaRef.current.style.height = Math.max(60, textareaRef.current.scrollHeight) + 'px'; } }; const handleSearch = () => { if (searchQuery.trim()) { onSearch(searchQuery); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { if (e.shiftKey) { // Shift+回车:允许换行,不做任何处理 return; } else { // 单独回车:触发搜索 e.preventDefault(); handleSearch(); } } }; return (