Skip to content

Commit

Permalink
refactor: simplify multiple filter logic in tanstack table
Browse files Browse the repository at this point in the history
  • Loading branch information
matalex412 committed Aug 22, 2024
1 parent 2e20edf commit 7d215c8
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions components/TanstackTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,14 @@ export default function TanstackTable<T>({
const FooterWrapper = isLowWidth ? Flex : Grid

const addFilter = () => {
setPrevFilters([...prevFilters, { id: currentFilteredColumn, value: searchQuery }])
table.setColumnFilters([...prevFilters, { id: currentFilteredColumn, value: searchQuery }])
const newFilters = [...prevFilters, { id: currentFilteredColumn, value: searchQuery }]
setPrevFilters(newFilters)
setSearchQuery("")
}

const deleteFilter = (index: number) => {
setPrevFilters(prevFilters.filter((_, i) => i !== index))
table.setColumnFilters([
...prevFilters.filter((_, i) => i !== index),
{ id: currentFilteredColumn, value: searchQuery },
])
setColumnFilters([...columnFilters.filter((_, i) => i !== index)])
}

return (
Expand All @@ -150,7 +147,7 @@ export default function TanstackTable<T>({
}}
onChange={e => {
setSearchQuery(e.target.value)
table.setColumnFilters([...prevFilters, { id: currentFilteredColumn, value: e.target.value }])
setColumnFilters([...prevFilters, { id: currentFilteredColumn, value: e.target.value }])
}}
value={searchQuery}
>
Expand Down

0 comments on commit 7d215c8

Please sign in to comment.