22 lines
605 B
TypeScript
22 lines
605 B
TypeScript
import { useState, useMemo } from "react";
|
|
import Input from "@/components/Low/Input";
|
|
import { search } from "@/utils/search";
|
|
|
|
export function useListSearch<T>(fields: string[][], rows: T[], placeholder?: string) {
|
|
const [text, setText] = useState("");
|
|
|
|
const renderSearch = () =>
|
|
<Input type="text" name="search" onChange={setText} placeholder={placeholder || "Enter search text"} value={text} />;
|
|
|
|
const updatedRows = useMemo(() => {
|
|
if (text.length > 0) return search(text, fields, rows);
|
|
return rows;
|
|
}, [fields, rows, text]);
|
|
|
|
return {
|
|
text,
|
|
rows: updatedRows,
|
|
renderSearch,
|
|
};
|
|
}
|