From 2ee8cccbcb573f18f53662211b22b970539a6ccb Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Mon, 15 Aug 2022 01:59:01 +0000 Subject: Add global filter (#5) --- src/pages/index.tsx | 55 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) (limited to 'src/pages/index.tsx') diff --git a/src/pages/index.tsx b/src/pages/index.tsx index c5f0994..6ba4d3d 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -7,6 +7,8 @@ import { Message, Container, Pagination, + Input, + Grid, } from "semantic-ui-react"; import styles from "../styles/Home.module.css"; @@ -40,6 +42,7 @@ const Home: NextPage = () => { }, }); const [activePage, setActivePage] = useState(1); + const [searchState, setSearchState] = useState(""); const [sortState, setSortState] = useState({ field: "name", order: "ascending", @@ -51,13 +54,26 @@ const Home: NextPage = () => { ); const entries = useMemo(() => { const list = List(data); + + // case insensitive filter + const filterValue = searchState.trim().toUpperCase(); + const filterList: List = + filterValue !== "" + ? list.filter( + (item) => + item.name?.toUpperCase().includes(filterValue) || + item.description?.toUpperCase().includes(filterValue) || + item.quantityUnitType?.toUpperCase().includes(filterValue) + ) + : list; + // case insensitive sort - const sorted = list.sortBy((item) => + const sorted = filterList.sortBy((item) => item[sortState.field]?.toString().toUpperCase() ); return sortState.order === "ascending" ? sorted : sorted.reverse(); - }, [data, sortState]); + }, [data, sortState, searchState]); const handleSortChange = (field: keyof PantryItem) => { setSortState((state) => state.field === field @@ -140,17 +156,30 @@ const Home: NextPage = () => { - - - setActivePage(Number(activePage ?? 1)) - } - totalPages={Math.max( - 1, - Math.ceil(entries.size / ENTRIES_PER_PAGE) - )} - /> + + + + + + setActivePage(Number(activePage ?? 1)) + } + totalPages={Math.max( + 1, + Math.ceil(entries.size / ENTRIES_PER_PAGE) + )} + /> + + + setSearchState(value)} + placeholder="Search..." + icon="search" + /> + + -- cgit