diff --git a/src/components/data-table/advanced/data-table-advanced-toolbar.tsx b/src/components/data-table/advanced/data-table-advanced-toolbar.tsx index 4fa5761..c7c8a0f 100644 --- a/src/components/data-table/advanced/data-table-advanced-toolbar.tsx +++ b/src/components/data-table/advanced/data-table-advanced-toolbar.tsx @@ -254,6 +254,7 @@ export function DataTableAdvancedToolbar({ diff --git a/src/components/data-table/advanced/data-table-multi-filter.tsx b/src/components/data-table/advanced/data-table-multi-filter.tsx index 9cbd202..24c7833 100644 --- a/src/components/data-table/advanced/data-table-multi-filter.tsx +++ b/src/components/data-table/advanced/data-table-multi-filter.tsx @@ -40,6 +40,7 @@ import { useTableInstanceContext } from "../table-instance-provider" interface DataTableMultiFilterProps { allOptions: DataTableFilterOption[] options: DataTableFilterOption[] + selectedOptions: DataTableFilterOption[] setSelectedOptions: React.Dispatch< React.SetStateAction[]> > @@ -49,6 +50,7 @@ interface DataTableMultiFilterProps { export function DataTableMultiFilter({ allOptions, options, + selectedOptions, setSelectedOptions, defaultOpen, }: DataTableMultiFilterProps) { @@ -62,7 +64,7 @@ export function DataTableMultiFilter({ const [open, setOpen] = React.useState(defaultOpen) const [operator, setOperator] = React.useState( - currentOperator || dataTableConfig.logicalOperators[0] + currentOperator ?? dataTableConfig.logicalOperators[0] ) return ( @@ -88,6 +90,7 @@ export function DataTableMultiFilter({ i={i} option={option} allOptions={allOptions} + selectedOptions={selectedOptions} setSelectedOptions={setSelectedOptions} operator={operator} setOperator={setOperator} @@ -128,6 +131,7 @@ interface MultiFilterRowProps { i: number allOptions: DataTableFilterOption[] option: DataTableFilterOption + selectedOptions: DataTableFilterOption[] setSelectedOptions: React.Dispatch< React.SetStateAction[]> > @@ -143,6 +147,7 @@ export function MultiFilterRow({ i, option, allOptions, + selectedOptions, setSelectedOptions, operator, setOperator, @@ -168,8 +173,15 @@ export function MultiFilterRow({ (operator) => operator.value === option.filterOperator ) ?? comparisonOperators[0] + const [mounted, setMounted] = React.useState(false) + // Update query string React.useEffect(() => { + if (!mounted) { + setMounted(true) + return + } + if (option.options.length > 0) { // key=value1.value2.value3~operator const filterValues = option.filterValues ?? [] @@ -227,7 +239,7 @@ export function MultiFilterRow({ (operator) => searchParams.get("operator") === operator.value ) - if (operator) { + if (newOperator) { setOperator(newOperator) } // eslint-disable-next-line react-hooks/exhaustive-deps @@ -384,12 +396,21 @@ export function MultiFilterRow({ prev.filter((item) => item.id !== option.id) ) - const newSearchParams = createQueryString( - { - [String(option.value)]: null, - }, - searchParams + // Only remove the option with no filter value + // if there are other options for the same column + const optionsForSameColumn = selectedOptions.filter( + (o) => o.id !== option.id && o.filterValues?.length ) + if (optionsForSameColumn.length) return + + const paramsObj: Record = { + [String(option.value)]: null, + } + // Reset operator to "and" when the filter option being removed is the last one + if (selectedOptions.length === 1) { + paramsObj.operator = "and" + } + const newSearchParams = createQueryString(paramsObj, searchParams) router.push(`${pathname}?${newSearchParams}`, { scroll: false, }) diff --git a/src/components/date-range-picker.tsx b/src/components/date-range-picker.tsx index 48b19e5..80eeb50 100644 --- a/src/components/date-range-picker.tsx +++ b/src/components/date-range-picker.tsx @@ -76,10 +76,10 @@ export function DateRangePicker({ const pathname = usePathname() const searchParams = useSearchParams() - const [date, setDate] = React.useState(() => { - const fromParam = searchParams.get("from") - const toParam = searchParams.get("to") + const fromParam = searchParams.get("from") + const toParam = searchParams.get("to") + function calcDateRange() { let fromDay: Date | undefined let toDay: Date | undefined @@ -95,7 +95,11 @@ export function DateRangePicker({ from: fromParam ? new Date(fromParam) : fromDay, to: toParam ? new Date(toParam) : toDay, } - }) + } + + const [date, setDate] = React.useState(() => + calcDateRange() + ) // Update query string React.useEffect(() => { @@ -119,6 +123,14 @@ export function DateRangePicker({ // eslint-disable-next-line react-hooks/exhaustive-deps }, [date?.from, date?.to]) + React.useEffect(() => { + const dateRange = calcDateRange() + + setDate(dateRange) + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [fromParam, toParam]) + return (
@@ -126,8 +138,6 @@ export function DateRangePicker({