import React, { FC, RefObject, useEffect, useState } from 'react'; import classNames from 'classnames'; import css from './index.module.css'; import SearchSVG from '../../assets/icons/search.svg'; import CloseSVG from '../../assets/icons/icon_close.svg'; export interface InputSearchProps { inputRef?: RefObject; value?: string; disabled?: boolean; placeholder?: string; label: string; name: string; hasResults?: boolean; onChange?: (e) => void; onKeyDown?: (e) => void; onFocus?: (e) => void; onInvalid?: (e) => void; onInput?: (e) => void; onSearchToggle?: (e) => void; setCloseSearch?: (value) => void; mobileNavOpen?: boolean; stickyHeader?: boolean; closeSearch: boolean; searchText: string; variant: 'normal' | 'header'; } const InputSearch: FC = ({ label, placeholder, name, value, onChange, onKeyDown, onFocus, disabled, onInvalid, onInput, inputRef, mobileNavOpen, stickyHeader, closeSearch, searchText, variant, setCloseSearch, }) => { const inputClassNames = classNames(css.inputSearch, { [css.inputSearchIcon]: true, [css.inputSearchOpen]: closeSearch, [css.searchHover]: !closeSearch, [css.inputSearchSticky]: stickyHeader, [css.inputSearchMobile]: mobileNavOpen, }); const inputClassNamesNormal = classNames(css.inputSearchNormal, { [css.inputSearchIcon]: true, }); const [inputValue, setInputValue] = useState(value); useEffect(() => { setInputValue(value); }, [value]); useEffect(() => { if (!closeSearch) { setInputValue(''); } }, [closeSearch]); if (variant === 'header') { return ( ); } else { return ( ); } }; export default InputSearch;