import React, { useState } from 'react'; import classNames from 'classnames'; import './Search.scss'; import Icon from '../Icon'; import { SearchProps } from './types'; import { checkEmpty } from '../../utils/checkEmpty/checkEmpty'; import Typography from '../Typography'; const Search = ({ placeholder = 'Search', onSearch, disabled = false, width = 150, }: SearchProps) => { const [isExpanded, setIsExpanded] = useState(false); const [searchvalue, setSearchValue] = useState(''); const handleKeyDown = (event: React.KeyboardEvent) => { if (event.key === 'Enter' && !checkEmpty(searchvalue)) { onSearch(searchvalue.trim()); } else if (event?.key === 'Escape') { handleSearchClearAndClose('Close'); } }; const onChange = (value: string) => { setSearchValue(value); if (value === '') { onSearch(''); } }; const handleIconClick = () => { setIsExpanded(!disabled); }; const handleSearchClearAndClose = (label: string) => { setSearchValue(''); onSearch(''); if (label === 'Close') { setIsExpanded(false); } }; return (
{ onChange(e.target.value); }} onKeyDown={handleKeyDown} /> {isExpanded && ( <>
{ handleSearchClearAndClose('Clear'); }} > {searchvalue !== '' && ( )}
{ handleSearchClearAndClose('Close'); }} />
)}
); }; export default Search;