Skip to content

Commit

Permalink
fix: make reset button clear filter as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Gum-Joe committed Aug 23, 2024
1 parent 09417ac commit 151c6b5
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions components/TanstackTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ export default function TanstackTable<T>({
setColumnFilters(newFilters)
}

/** Clears the current filter if it is not set as a chip */
const clearCurrentFilter = () => {
if (!prevFilters.find(f => f.id === currentFilteredColumn)) {
setColumnFilters(columnFilters.filter(f => f.id !== currentFilteredColumn))
}

setSearchQuery("")
}

return (
<Flex gap="4" direction="column" width="100%">
{enableSearch && (
Expand All @@ -221,7 +230,7 @@ export default function TanstackTable<T>({
<MagnifyingGlassIcon height="16" width="16" />
</TextField.Slot>
<TextField.Slot>
<Button variant="ghost" onClick={() => setSearchQuery("")}>
<Button variant="ghost" onClick={clearCurrentFilter}>
Reset
</Button>
</TextField.Slot>
Expand Down Expand Up @@ -267,13 +276,9 @@ export default function TanstackTable<T>({
}))}
defaultValue={filterableColumns[0].id ?? ""}
onValueChange={newFilterCol => {
// If not in prevFilters, remove from current filters
if (!prevFilters.find(f => f.id === currentFilteredColumn)) {
setColumnFilters(columnFilters.filter(f => f.id !== currentFilteredColumn))
}
clearCurrentFilter()

setCurrentFilteredColumn(newFilterCol)
setSearchQuery("")
setDateStart("")
setDateEnd("")
}}
Expand Down

0 comments on commit 151c6b5

Please sign in to comment.