Skip to content

Commit

Permalink
fix(DataTableSearchInput): don't override pagination on refresh (#2730)
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Oct 1, 2024
1 parent 76c9664 commit cb4a232
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Table } from '@tanstack/react-table';
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useDebounce } from 'react-use';

import { BaseInput } from '@/common/components/+vendor/BaseInput';
Expand All @@ -23,6 +23,8 @@ export function DataTableSearchInput<TData>({ table }: DataTableSearchInputProps

const { hotkeyInputRef } = useSearchInputHotkey({ key: '/' });

const isFirstRender = useRef(true);

/**
* Listen for changes with column filter state and stay in sync. Otherwise,
* when the user presses the "Reset" button to reset all filters, our search
Expand All @@ -39,7 +41,20 @@ export function DataTableSearchInput<TData>({ table }: DataTableSearchInputProps
*/
useDebounce(
() => {
table.getColumn('title')?.setFilterValue(rawInputValue);
// Skip the effect on the first render. Otherwise, column filters
// be changed simply because the component mounted.
if (isFirstRender.current) {
isFirstRender.current = false;

return;
}

const currentFilterValue = (table.getColumn('title')?.getFilterValue() as string) ?? '';

// Only update the filter if the value has changed.
if (rawInputValue !== currentFilterValue) {
table.getColumn('title')?.setFilterValue(rawInputValue);
}
},
getDebounceDuration(rawInputValue),
[rawInputValue],
Expand Down

0 comments on commit cb4a232

Please sign in to comment.