Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';
import * as React from 'react';
import debounce from '@mui/utils/debounce';
import { RefObject } from '@mui/x-internals/types';
import { isDeepEqual } from '@mui/x-internals/isDeepEqual';
import { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
Expand Down Expand Up @@ -276,6 +277,11 @@ export const useGridPaginationModel = (
}
}, [apiRef]);

const debouncedNavigateToStart = React.useMemo(
() => debounce(navigateToStart, 0),
[navigateToStart],
);

/**
* Resets the page only if the active items or quick filter has changed from the last time.
* This is to avoid resetting the page when the filter model is changed
Expand All @@ -295,15 +301,15 @@ export const useGridPaginationModel = (
}

previousFilterModel.current = currentActiveFilters;
navigateToStart();
debouncedNavigateToStart();
},
[apiRef, navigateToStart],
[apiRef, debouncedNavigateToStart],
);

useGridEvent(apiRef, 'viewportInnerSizeChange', handleUpdateAutoPageSize);
useGridEvent(apiRef, 'paginationModelChange', handlePaginationModelChange);
useGridEvent(apiRef, 'rowCountChange', handleRowCountChange);
useGridEvent(apiRef, 'sortModelChange', navigateToStart);
useGridEvent(apiRef, 'sortModelChange', debouncedNavigateToStart);
useGridEvent(apiRef, 'filterModelChange', handleFilterModelChange);

/**
Expand Down
14 changes: 10 additions & 4 deletions packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ describe('<DataGrid /> - Pagination', () => {
});
});

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

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

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

// page and scroll position are reset filtering
expect(apiRef.current!.state.pagination.paginationModel.page).to.equal(0);
await waitFor(() => {
expect(apiRef.current!.state.pagination.paginationModel.page).to.equal(0);
});
expect(apiRef.current!.getScrollPosition().top).to.equal(0);
});

Expand Down
Loading