Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 - fix: fix issue where debounced filters were not working corre… #85

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/components/data/datagrid/datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export type DataGridProps = {

/**
* Gets called when a row value is filtered.
* This callback is debounced every 300 milliseconds.
* This callback is debounced every 100 milliseconds.
*/
onFilter?: (rowData: AttributeData) => void;

Expand Down Expand Up @@ -417,6 +417,7 @@ export const DataGrid: React.FC<DataGridProps> = ({
sortDirection={sortDirection}
sortField={sortField}
onFilter={handleFilter}
onPageChange={onPageChange}
onSort={handleSort}
/>

Expand Down Expand Up @@ -664,6 +665,7 @@ export type DataGridHeadingProps = {
sortDirection: "ASC" | "DESC" | undefined;
sortField: string | undefined;
onFilter: (data: AttributeData) => void;
onPageChange: DataGridProps["onPageChange"];
onSort: (field: TypedField) => void;
};

Expand All @@ -677,6 +679,7 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({
id,
labelFilterField,
onFilter,
onPageChange,
onSort,
renderableFields,
selectable,
Expand All @@ -685,6 +688,18 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({
sortField,
}) => {
const intl = useIntl();
const onFilterTimeoutRef = useRef<NodeJS.Timeout>();
const [filterState, setFilterState] = useState<AttributeData>();

// Debounce filter
useEffect(() => {
const handler = () => {
onPageChange?.(1);
onFilter(filterState || {});
};
onFilterTimeoutRef.current && clearTimeout(onFilterTimeoutRef.current);
setTimeout(handler, 100);
}, [filterState]);

return (
<thead className="mykn-datagrid__head" role="rowgroup">
Expand Down Expand Up @@ -768,7 +783,9 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({
const _data = filterTransform
? filterTransform(data)
: data;
onFilter(_data);

// Reset page on filter (length of dataset may change).
setFilterState(_data);
}}
/>
)}
Expand Down
4 changes: 2 additions & 2 deletions src/components/data/paginator/paginator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type PaginatorProps = React.HTMLAttributes<HTMLElement> & {
* If a `Promise` is returned and `labelLoading` is set: a spinner will be
* shown after during the "pending" state of the returned `Promise`.
*
* This callback is debounced every 300 milliseconds.
* This callback is debounced every 100 milliseconds.
*/
onPageChange?: (page: number) => Promise<unknown> | void;

Expand Down Expand Up @@ -143,7 +143,7 @@ export const Paginator: React.FC<PaginatorProps> = ({
onPageChangeTimeoutRef.current &&
clearTimeout(onPageChangeTimeoutRef.current);

onPageChangeTimeoutRef.current = setTimeout(handler, 300);
onPageChangeTimeoutRef.current = setTimeout(handler, 100);

return () => clearTimeout(onPageChangeTimeoutRef.current);
}, [pageState]);
Expand Down