From 7d215c8fa24491be8d46f899eefcecfc94ec66e9 Mon Sep 17 00:00:00 2001 From: Matthew Alex Date: Thu, 22 Aug 2024 12:15:07 +0100 Subject: [PATCH] refactor: simplify multiple filter logic in tanstack table --- components/TanstackTable.tsx | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/components/TanstackTable.tsx b/components/TanstackTable.tsx index 926b69ca..0a117266 100644 --- a/components/TanstackTable.tsx +++ b/components/TanstackTable.tsx @@ -122,17 +122,14 @@ export default function TanstackTable({ 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 ( @@ -150,7 +147,7 @@ export default function TanstackTable({ }} 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} >