import CloseIcon from '@mui/icons-material/Close'; import { IconButton, InputAdornment, TextField } from '@mui/material'; import { memo, useEffect, useRef, useState } from 'react'; import { Search } from './icons/icons'; function SearchComponent({ placeholder = 'Поиск...', value = '', onChange, onClear, clearable = false, debounceMs = 0, height = '2rem', sx: sxProp, ...rest }) { const [inputValue, setInputValue] = useState(value); const onChangeRef = useRef(onChange); useEffect(() => { onChangeRef.current = onChange; }, [onChange]); useEffect(() => { setInputValue(value); }, [value]); useEffect(() => { if (!debounceMs) return; const timer = setTimeout(() => { if (inputValue !== value) { onChangeRef.current(inputValue); } }, debounceMs); return () => clearTimeout(timer); }, [inputValue, debounceMs, value]); const handleChange = (e) => { const next = e.target.value; setInputValue(next); if (!debounceMs) { onChange(next); } }; const handleClear = () => { setInputValue(''); (onClear ?? onChange)(''); }; return ( ), ...(clearable && inputValue ? { endAdornment: ( event.preventDefault()} onClick={handleClear}> ), } : {}), }, }} /> ); } export default memo(SearchComponent);