Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 50 | import { useEffect, useState } from "react"; import useDebounce from "./useDebounce.ts"; function useBasicSearch(list: any[], searchKeys: string[]) { const [query, setQuery] = useState(""); const [filteredList, setFilteredList] = useState(list); const debouncedQuery = useDebounce(query, 0); const isEmpty = list?.length === 0; const notFound = filteredList?.length === 0 && list.length !== 0; const setValue = (e: any) => { Iif (!e) { setQuery(""); return; } const val = typeof e === "string" ? e : e.target.value; setQuery(val); }; const search = () => { if (debouncedQuery) { return list?.filter((item) => { const conditions = searchKeys.map((key) => item[key].toLowerCase().includes(debouncedQuery.toLowerCase()), ); return conditions.includes(true); }); } else { return list; } }; useEffect(() => { setFilteredList(search()); // eslint-disable-next-line react-hooks/exhaustive-deps }, [debouncedQuery, list]); return { query, filteredList, list, isEmpty, notFound, setValue, }; } export default useBasicSearch; |