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/_app.tsx | 6 +++++- src/pages/index.tsx | 55 ++++++++++++++++++++++++++++++++++++++------------ src/styles/globals.css | 6 ++++++ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 082f72f..139eaeb 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -10,7 +10,11 @@ function MyApp({ Component, pageProps }: AppProps) { return ( - {process.env.APP_ENV === "development" ? (
Visit Quarkus dev page
) : undefined} + {process.env.APP_ENV === "development" ? ( +
+ Visit Quarkus dev page +
+ ) : undefined}
); } 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" + /> + + diff --git a/src/styles/globals.css b/src/styles/globals.css index 344cd26..1675722 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -11,6 +11,12 @@ a { text-decoration: none; } +div.paginate-container { + display: flex !important; + align-items: center; + justify-content: center; +} + * { box-sizing: border-box; } -- cgit