2023-03-18 01:40:49 -04:00
|
|
|
import { Search } from 'lucide-react';
|
2023-03-29 00:08:02 +08:00
|
|
|
import { useRecoilState } from 'recoil';
|
2023-03-28 20:36:21 +08:00
|
|
|
import store from '~/store';
|
2023-03-18 01:40:49 -04:00
|
|
|
|
2023-04-06 05:47:37 -07:00
|
|
|
export default function SearchBar({ clearSearch }) {
|
|
|
|
|
const [searchQuery, setSearchQuery] = useRecoilState(store.searchQuery);
|
2023-03-29 00:08:02 +08:00
|
|
|
|
2023-05-18 11:09:31 -07:00
|
|
|
const handleKeyUp = (e) => {
|
2023-03-18 01:40:49 -04:00
|
|
|
const { value } = e.target;
|
2023-03-28 20:36:21 +08:00
|
|
|
if (e.keyCode === 8 && value === '') {
|
|
|
|
|
setSearchQuery('');
|
2023-03-18 14:28:10 -04:00
|
|
|
clearSearch();
|
2023-03-18 01:40:49 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-18 15:22:48 -04:00
|
|
|
const onChange = (e) => {
|
|
|
|
|
const { value } = e.target;
|
|
|
|
|
setSearchQuery(value);
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-18 01:40:49 -04:00
|
|
|
return (
|
2023-05-18 11:09:31 -07:00
|
|
|
<div className="flex cursor-pointer items-center gap-3 rounded-md px-3 py-3 text-sm text-white transition-colors duration-200 hover:bg-gray-500/10">
|
2023-03-18 01:40:49 -04:00
|
|
|
{<Search className="h-4 w-4" />}
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
className="m-0 mr-0 w-full border-none bg-transparent p-0 text-sm leading-tight outline-none"
|
2023-04-06 05:47:37 -07:00
|
|
|
value={searchQuery}
|
2023-05-18 15:22:48 -04:00
|
|
|
onChange={onChange}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
e.code === 'Space' ? e.stopPropagation() : null;
|
|
|
|
|
}}
|
2023-03-18 01:40:49 -04:00
|
|
|
placeholder="Search messages"
|
|
|
|
|
onKeyUp={handleKeyUp}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|