-
Notifications
You must be signed in to change notification settings - Fork 3
Individual Column Sorting and Filtering #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
naasanov
wants to merge
12
commits into
main
Choose a base branch
from
table-search-op
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2c5fd0e
sorting and filter functionality
naasanov f84231e
Added filter and sort functionality and dropdowns on each column
naasanov 2e0f434
added line between sorting and filtering in dropdown
naasanov eae2d2b
removed unnecessary change
naasanov 1c7ebd4
removed another unnecessary change
naasanov ee4bea6
removed another another unnecessary change
naasanov 3750fcf
small code quality changes
naasanov c21d12b
added tags filter
naasanov b83298e
added filter dropdown
naasanov be164d1
Merge branch 'main' into table-search-op
naasanov 15ad267
main branch integraiton
naasanov 81d3a4b
documentation and small fixes
naasanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import { flexRender, Header } from "@tanstack/react-table"; | ||
| import { useState, useEffect, useRef } from "react"; | ||
| import { | ||
| CheckIcon, | ||
| ArrowUpIcon, | ||
| ArrowDownIcon, | ||
| FunnelIcon, | ||
| XMarkIcon, | ||
| } from "@heroicons/react/24/outline"; | ||
|
|
||
| function DropdownCheckIcon({ className }: { className?: string }) { | ||
| return ( | ||
| <CheckIcon className={`w-4 h-4 ml-auto ${className}`} strokeWidth={2} /> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Component for rendering the header of a table column, | ||
| * as well as the dropdown menu for sorting and filtering. | ||
| */ | ||
| export function ColumnHeader<T>({ header }: { header: Header<T, unknown> }) { | ||
| const { column } = header; | ||
|
|
||
| const [dropdownType, setDropdownType] = useState<"menu" | "filter" | null>( | ||
| null | ||
| ); | ||
| const [sortDirection, setSortDirection] = useState<"asc" | "desc" | null>( | ||
| null | ||
| ); | ||
|
|
||
| const isFiltered = | ||
| column.getFilterValue() !== undefined && | ||
| column.getFilterValue() !== null && | ||
| column.getFilterValue() !== ""; | ||
|
|
||
| const headerRef = useRef<HTMLTableCellElement>(null); | ||
| const menuRef = useRef<HTMLDivElement>(null); | ||
| const filterRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| // Close the dropdown menu/filter input when clicking outside of it | ||
| useEffect(() => { | ||
| const handleOutsideClick = (e: MouseEvent) => { | ||
| const target = e.target as Node; | ||
| const clickOutsideMenu = | ||
| menuRef.current && !menuRef.current.contains(target); | ||
| const clickOutsideFilter = | ||
| filterRef.current && !filterRef.current.contains(target); | ||
|
|
||
| if (clickOutsideMenu || clickOutsideFilter) { | ||
| setDropdownType(null); | ||
| } | ||
| }; | ||
| document.addEventListener("click", handleOutsideClick); | ||
| return () => { | ||
| document.removeEventListener("click", handleOutsideClick); | ||
| }; | ||
| }, [dropdownType]); | ||
|
|
||
| // Set the sort direction based on the current state | ||
| useEffect(() => { | ||
| switch (sortDirection) { | ||
| case "asc": | ||
| column.toggleSorting(false); | ||
| break; | ||
| case "desc": | ||
| column.toggleSorting(true); | ||
| break; | ||
| default: | ||
| column.clearSorting(); | ||
| } | ||
| }, [sortDirection, column]); | ||
|
|
||
| return ( | ||
| <th | ||
| scope="col" | ||
| className="border-gray-200 border-y font-medium" | ||
| ref={headerRef} | ||
| > | ||
| <div> | ||
| {header.isPlaceholder ? null : ( | ||
| <div | ||
| className="flex p-2 h-auto items-center justify-between px-2 relative cursor-pointer hover:bg-gray-200/50" | ||
| onClick={() => | ||
| setDropdownType((prev) => | ||
| prev === null ? "menu" : null | ||
| ) | ||
| } | ||
| > | ||
| <div className="flex items-center"> | ||
| {flexRender( | ||
| column.columnDef.header, | ||
| header.getContext() | ||
| )} | ||
| {/* Choose the icon based on sort direction */} | ||
| {{ | ||
| asc: <ArrowUpIcon className="w-3 h-3 ml-1" />, | ||
| desc: ( | ||
| <ArrowDownIcon className="w-3 h-3 ml-1" /> | ||
| ), | ||
| }[column.getIsSorted() as string] ?? null} | ||
12beesinatrenchcoat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| <div className="relative"> | ||
| {/* Dropdown menu to add sorting or filter */} | ||
| {column.getCanFilter() && dropdownType === "menu" && ( | ||
| <div | ||
| ref={menuRef} | ||
| className="absolute flex flex-col justify-center items-center -top-2 left-0 mt-2 w-48 bg-white border border-gray-200 rounded-md shadow-lg z-10" | ||
| > | ||
| <button | ||
| className="flex items-center w-full text-left px-4 py-2 text-xs hover:bg-gray-100" | ||
| onClick={() => { | ||
| setSortDirection((prev) => | ||
| prev === "asc" ? null : "asc" | ||
| ); | ||
| setDropdownType(null); | ||
| }} | ||
| > | ||
| <ArrowUpIcon className="w-4 h-4 mr-2" /> | ||
| <span>Sort Ascending</span> | ||
| {sortDirection === "asc" && <DropdownCheckIcon />} | ||
| </button> | ||
| <button | ||
| className="flex items-center w-full text-left px-4 py-2 text-xs hover:bg-gray-100" | ||
| onClick={() => { | ||
| setSortDirection((prev) => | ||
| prev === "desc" ? null : "desc" | ||
| ); | ||
| setDropdownType(null); | ||
| }} | ||
| > | ||
| <ArrowDownIcon className="w-4 h-4 mr-2" /> | ||
| <span>Sort Descending</span> | ||
| {sortDirection === "desc" && <DropdownCheckIcon />} | ||
| </button> | ||
| <hr className="w-40" /> | ||
| <button | ||
| className="flex items-center w-full text-left px-4 py-2 text-xs hover:bg-gray-100" | ||
| onClick={() => { | ||
| column.getCanFilter() && | ||
| column.setFilterValue(""); | ||
| setDropdownType(isFiltered ? null : "filter"); | ||
| }} | ||
| > | ||
| {isFiltered ? ( | ||
| <> | ||
| <FunnelIcon className="`w-4 h-4 mr-2 text-red-400" /> | ||
| <span className="text-red-400"> | ||
| Clear Filter | ||
| </span> | ||
| <XMarkIcon className="w-4 h-4 ml-auto text-red-400" /> | ||
12beesinatrenchcoat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </> | ||
| ) : ( | ||
| <> | ||
| <FunnelIcon className="w-4 h-4 mr-2" /> | ||
| <span>Add Filter</span> | ||
| </> | ||
| )} | ||
| </button> | ||
| </div> | ||
| )} | ||
| {/* Dropdown menu to add a filter value */} | ||
| {column.getCanFilter() && dropdownType === "filter" && ( | ||
| <div | ||
| ref={filterRef} | ||
| className="absolute -top-2 left-0 mt-2 w-48 bg-white border border-gray-200 rounded-md shadow-lg z-10" | ||
| > | ||
| <div className="flex flex-col px-4 py-2"> | ||
| <span>Contains</span> | ||
| <input | ||
| type="text" | ||
| value={ | ||
| (column.getFilterValue() ?? "") as string | ||
| } | ||
| onChange={(e) => { | ||
| column.setFilterValue(e.target.value); | ||
| }} | ||
| placeholder="Filter..." | ||
12beesinatrenchcoat marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| className="border border-gray-300 rounded p-1" | ||
| /> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| </th> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.