Skip to content

Commit 9e275c4

Browse files
authored
fix(DataTable): debounce global filter in Customers demo to improve performance (#8461)
1 parent 8ee8924 commit 9e275c4

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

components/doc/datatable/samples/customersdoc.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { MultiSelect } from '@/components/lib/multiselect/MultiSelect';
1515
import { ProgressBar } from '@/components/lib/progressbar/ProgressBar';
1616
import { Slider } from '@/components/lib/slider/Slider';
1717
import { Tag } from '@/components/lib/tag/Tag';
18-
import React, { useState } from 'react';
18+
import React, { useState, useEffect } from 'react';
19+
import { useDebounce } from '@/components/lib/hooks/useDebounce';
1920
import { CustomerService } from '../../../../service/CustomerService';
2021

2122
export const CustomersDoc = (props) => {
@@ -32,6 +33,7 @@ export const CustomersDoc = (props) => {
3233
activity: { value: null, matchMode: FilterMatchMode.BETWEEN }
3334
});
3435
const [globalFilterValue, setGlobalFilterValue] = useState('');
36+
const debouncedGlobalFilterValue = useDebounce(globalFilterValue, 300);
3537
const [representatives] = useState([
3638
{ name: 'Amy Elsner', image: 'amyelsner.png' },
3739
{ name: 'Anna Fali', image: 'annafali.png' },
@@ -46,6 +48,16 @@ export const CustomersDoc = (props) => {
4648
]);
4749
const [statuses] = useState(['unqualified', 'qualified', 'new', 'negotiation', 'renewal']);
4850

51+
useEffect(() => {
52+
setFilters((prevFilters) => ({
53+
...prevFilters,
54+
global: {
55+
...prevFilters.global,
56+
value: debouncedGlobalFilterValue
57+
}
58+
}));
59+
}, [debouncedGlobalFilterValue]);
60+
4961
const getSeverity = (status) => {
5062
switch (status) {
5163
case 'unqualified':
@@ -90,13 +102,7 @@ export const CustomersDoc = (props) => {
90102
};
91103

92104
const onGlobalFilterChange = (e) => {
93-
const value = e.target.value;
94-
let _filters = { ...filters };
95-
96-
_filters.global.value = value;
97-
98-
setFilters(_filters);
99-
setGlobalFilterValue(value);
105+
setGlobalFilterValue(e.target.value);
100106
};
101107

102108
const renderHeader = () => {

0 commit comments

Comments
 (0)