// src/components/SearchButton/SearchButton.tsx import React, { useState, useRef, useEffect } from "react"; import { LookUp } from "../../Icons/LookUp"; import styles from "./SearchButton.module.css"; interface SearchButtonProps { onSearch?: (query: string) => void; placeholder?: string; initialSize?: string; // Пропс для начального размера круглой кнопки onToggleExpand?: (isExpanded: boolean) => void; // Новый пропс для связи с родителем } function SearchButton(props: Readonly) { const { onSearch, placeholder = 'Search...', initialSize = '60px', onToggleExpand } = props; const [isExpanded, setIsExpanded] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const inputRef = useRef(null); const containerRef = useRef(null); useEffect(() => { if (isExpanded && inputRef.current) { inputRef.current.focus(); } // Уведомляем родительский компонент об изменении состояния if (onToggleExpand) { onToggleExpand(isExpanded); } }, [isExpanded, onToggleExpand]); // onToggleExpand добавлен в зависимости const handleToggleExpand = () => { setIsExpanded(prevExpanded => !prevExpanded); // Используем функциональное обновление setSearchQuery(''); }; const handleSearchSubmit = (event: React.FormEvent) => { event.preventDefault(); if (searchQuery.trim() && onSearch) { onSearch(searchQuery.trim()); // Опционально: можно схлопнуть поле ввода после поиска // setIsExpanded(false); } }; const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Escape') { setIsExpanded(false); setSearchQuery(''); } }; useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if ( isExpanded && containerRef.current && !containerRef.current.contains(event.target as Node) ) { setIsExpanded(false); setSearchQuery(''); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [isExpanded]); const containerInlineStyles: React.CSSProperties = { width: isExpanded ? undefined : initialSize, // При развороте ширина определяется CSS, иначе - initialSize height: initialSize, // Высота всегда фиксирована initialSize (для круглой кнопки и высоты инпута) }; return (
{isExpanded ? (
setSearchQuery(e.target.value)} onKeyDown={handleKeyDown} />
) : ( )}
); } export default SearchButton;