Skip to content

Commit 508f8eb

Browse files
authored
[DataGrid] Wait for external model updates before resetting pagination after sort/filter (#20162)
1 parent 0286dd7 commit 508f8eb

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

packages/x-data-grid/src/hooks/features/pagination/useGridPaginationModel.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client';
22
import * as React from 'react';
3+
import debounce from '@mui/utils/debounce';
34
import { RefObject } from '@mui/x-internals/types';
45
import { isDeepEqual } from '@mui/x-internals/isDeepEqual';
56
import { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
@@ -276,6 +277,11 @@ export const useGridPaginationModel = (
276277
}
277278
}, [apiRef]);
278279

280+
const debouncedNavigateToStart = React.useMemo(
281+
() => debounce(navigateToStart, 0),
282+
[navigateToStart],
283+
);
284+
279285
/**
280286
* Resets the page only if the active items or quick filter has changed from the last time.
281287
* This is to avoid resetting the page when the filter model is changed
@@ -295,15 +301,15 @@ export const useGridPaginationModel = (
295301
}
296302

297303
previousFilterModel.current = currentActiveFilters;
298-
navigateToStart();
304+
debouncedNavigateToStart();
299305
},
300-
[apiRef, navigateToStart],
306+
[apiRef, debouncedNavigateToStart],
301307
);
302308

303309
useGridEvent(apiRef, 'viewportInnerSizeChange', handleUpdateAutoPageSize);
304310
useGridEvent(apiRef, 'paginationModelChange', handlePaginationModelChange);
305311
useGridEvent(apiRef, 'rowCountChange', handleRowCountChange);
306-
useGridEvent(apiRef, 'sortModelChange', navigateToStart);
312+
useGridEvent(apiRef, 'sortModelChange', debouncedNavigateToStart);
307313
useGridEvent(apiRef, 'filterModelChange', handleFilterModelChange);
308314

309315
/**

packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ describe('<DataGrid /> - Pagination', () => {
609609
});
610610
});
611611

612-
it('should reset page to 0 and scroll to top if sort or filter is applied', () => {
612+
it('should reset page to 0 and scroll to top if sort or filter is applied', async () => {
613613
render(
614614
<BaselineTestCase
615615
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 50 }, rowCount: 0 } }}
@@ -630,7 +630,9 @@ describe('<DataGrid /> - Pagination', () => {
630630
apiRef.current!.sortColumn('id', 'asc');
631631
});
632632
// page is reset to 0 after sorting
633-
expect(apiRef.current!.state.pagination.paginationModel.page).to.equal(0);
633+
await waitFor(() => {
634+
expect(apiRef.current!.state.pagination.paginationModel.page).to.equal(0);
635+
});
634636
expect(apiRef.current!.getScrollPosition().top).to.equal(0);
635637

636638
// scroll but stay on the same page
@@ -642,7 +644,9 @@ describe('<DataGrid /> - Pagination', () => {
642644
act(() => {
643645
apiRef.current!.sortColumn('id', 'desc');
644646
});
645-
expect(apiRef.current!.getScrollPosition().top).to.equal(0);
647+
await waitFor(() => {
648+
expect(apiRef.current!.getScrollPosition().top).to.equal(0);
649+
});
646650

647651
// move to the next page again and scroll
648652
act(() => {
@@ -664,7 +668,9 @@ describe('<DataGrid /> - Pagination', () => {
664668
});
665669

666670
// page and scroll position are reset filtering
667-
expect(apiRef.current!.state.pagination.paginationModel.page).to.equal(0);
671+
await waitFor(() => {
672+
expect(apiRef.current!.state.pagination.paginationModel.page).to.equal(0);
673+
});
668674
expect(apiRef.current!.getScrollPosition().top).to.equal(0);
669675
});
670676

0 commit comments

Comments
 (0)