File size: 1,354 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import React, { useEffect } from 'react';
import { Box, Input, InputGroup, InputRightElement } from '@chakra-ui/react';
import { FiSearch } from 'react-icons/fi';
import { useNavigate } from 'react-router-dom';
const SearchInput = (
{ w, display, mb, px, flex, placeholder, route, querySearchTerm },
ref
) => {
const navigate = useNavigate();
const handleSearch = (e) => {
e.preventDefault();
ref.current.blur();
const searchTerm = ref.current.value;
if (!searchTerm && route === 'search') return;
const queryName = route === 'search' ? 'spq' : 'stq'; // => searchPostQuery / searchTagQuery
navigate(`/${route}?${queryName}=${searchTerm}`);
};
useEffect(() => {
if (!querySearchTerm) {
ref.current.value = '';
} else {
ref.current.value = querySearchTerm;
}
}, [querySearchTerm, ref]);
return (
<Box
as='form'
onSubmit={handleSearch}
display={display}
px={px || '.5rem'}
mb={mb}
flex={flex}
>
<InputGroup h='39px' w={w || '100%'}>
<Input placeholder={placeholder || 'Search...'} ref={ref} />
<InputRightElement children={<FiSearch size={23} color='gray' />} />
</InputGroup>
</Box>
);
};
export default React.forwardRef(SearchInput);
|