From 656edbfac74b2638be47189759cc695a2e34944b Mon Sep 17 00:00:00 2001 From: apcodes Date: Fri, 19 Apr 2024 16:49:30 -0400 Subject: [PATCH] added the filter box and edited the table action to add the filter UI --- compass/components/FilterBox.tsx | 73 ++++++++++++++++++++++++ compass/components/Table/Index.tsx | 1 + compass/components/Table/TableAction.tsx | 46 ++++++++++----- package-lock.json | 6 ++ 4 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 compass/components/FilterBox.tsx create mode 100644 package-lock.json diff --git a/compass/components/FilterBox.tsx b/compass/components/FilterBox.tsx new file mode 100644 index 0000000..a38574c --- /dev/null +++ b/compass/components/FilterBox.tsx @@ -0,0 +1,73 @@ +// FilterBox.tsx +import { useState } from "react"; + +const mockTags = ["food relief", "period poverty", "nutrition education"]; + +export const FilterBox = () => { + const [selectedTags, setSelectedTags] = useState([]); + const [searchTerm, setSearchTerm] = useState(""); + + const handleTagChange = (tag: string) => { + setSelectedTags((prevTags) => + prevTags.includes(tag) + ? prevTags.filter((t) => t !== tag) + : [...prevTags, tag] + ); + }; + + const handleSearchChange = (e: React.ChangeEvent) => { + setSearchTerm(e.target.value); + }; + + const renderSelectedTags = () => + selectedTags.map((tag) => ( +
+ {tag} + handleTagChange(tag)} + > + × + +
+ )); + + return ( +
+
+ Tags contains: + +
+
+ {selectedTags.length > 0 && renderSelectedTags()} + +
+
+ {mockTags + .filter((tag) => + tag.toLowerCase().includes(searchTerm.toLowerCase()) + ) + .map((tag) => ( +
+ handleTagChange(tag)} + className="mr-2" + /> + +
+ ))} +
+
+ ); +}; diff --git a/compass/components/Table/Index.tsx b/compass/components/Table/Index.tsx index 9c7329b..b373560 100644 --- a/compass/components/Table/Index.tsx +++ b/compass/components/Table/Index.tsx @@ -113,6 +113,7 @@ export const Table = () => { } // TODO: Filtering + // TODO: Sorting const table = useReactTable({ diff --git a/compass/components/Table/TableAction.tsx b/compass/components/Table/TableAction.tsx index e58e110..737b95a 100644 --- a/compass/components/Table/TableAction.tsx +++ b/compass/components/Table/TableAction.tsx @@ -1,46 +1,64 @@ -/** The actions (Filter, Sort, Search) at the top of the table. */ +// TableAction.tsx import { MagnifyingGlassIcon } from "@heroicons/react/24/solid"; -import { ChangeEventHandler, Dispatch, FunctionComponent, SetStateAction, useRef, useState } from "react"; +import { ChangeEventHandler, FunctionComponent, useRef, useState } from "react"; +import { FilterBox } from "../../components/FilterBox"; type TableActionProps = { - query: string - handleChange: ChangeEventHandler -} + query: string; + handleChange: ChangeEventHandler; +}; -export const TableAction: FunctionComponent = ({query, handleChange}) => { +export const TableAction: FunctionComponent = ({ + query, + handleChange, +}) => { const searchInput = useRef(null); const [searchActive, setSearchActive] = useState(false); + const [showFilterBox, setShowFilterBox] = useState(false); const activateSearch = () => { setSearchActive(true); - if (searchInput.current === null) { return; } + if (searchInput.current === null) { + return; + } searchInput.current.focus(); searchInput.current.addEventListener("focusout", () => { if (searchInput.current?.value.trim() === "") { searchInput.current.value = ""; deactivateSearch(); } - }) - } + }); + }; const deactivateSearch = () => setSearchActive(false); + const toggleFilterBox = () => setShowFilterBox((prev) => !prev); + return (
- Filter + + Filter + + {showFilterBox && } Sort - activateSearch()}> + + />
); -}; +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b5bb348 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "compass", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}