Skip to content

Commit fb46cdd

Browse files
fix(mui): prevent DataGrid pagination reset (#7105)
Co-authored-by: Alican Erdurmaz <[email protected]>
1 parent 3edacb0 commit fb46cdd

File tree

3 files changed

+89
-6
lines changed

3 files changed

+89
-6
lines changed

.changeset/khaki-tigers-refuse.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@refinedev/mui": patch
3+
---
4+
5+
fix: Prevent DataGrid Pagination reset. #7099
6+
7+
Fixed an issue in useDataGrid in which the sortModel sent in response was constantly receiving a new Object, even though the content of it was not changing
8+
9+
Fixes #7099

packages/mui/src/hooks/useDataGrid/index.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,65 @@ describe("useDataGrid Hook", () => {
307307
);
308308
});
309309
});
310+
311+
it("should not change sortModel when page changes", async () => {
312+
const { result } = renderHook(
313+
() =>
314+
useDataGrid({
315+
resource: "posts",
316+
sorters: {
317+
initial: [
318+
{
319+
field: "title",
320+
order: "asc",
321+
},
322+
],
323+
},
324+
}),
325+
{
326+
wrapper: TestWrapper({
327+
dataProvider: MockJSONServer,
328+
resources: [{ name: "posts" }],
329+
}),
330+
},
331+
);
332+
333+
await waitFor(() => {
334+
expect(result.current.tableQuery.isSuccess).toBeTruthy();
335+
});
336+
337+
// Capture initial sortModel
338+
const initialSortModel = result.current.dataGridProps.sortModel;
339+
expect(initialSortModel).toEqual([
340+
{
341+
field: "title",
342+
sort: "asc",
343+
},
344+
]);
345+
346+
// Change page using onPaginationModelChange from dataGridPaginationValues
347+
await act(async () => {
348+
result.current.dataGridProps.onPaginationModelChange!(
349+
{
350+
page: 3,
351+
pageSize: 25,
352+
},
353+
{} as any,
354+
);
355+
});
356+
357+
await waitFor(() => {
358+
expect(result.current.currentPage).toBe(4);
359+
});
360+
361+
// sortModel should remain the same object reference after page change
362+
const sortModelAfterPageChange = result.current.dataGridProps.sortModel;
363+
expect(sortModelAfterPageChange).toBe(initialSortModel);
364+
expect(sortModelAfterPageChange).toEqual([
365+
{
366+
field: "title",
367+
sort: "asc",
368+
},
369+
]);
370+
});
310371
});

packages/mui/src/hooks/useDataGrid/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
useLiveMode,
44
useTable as useTableCore,
55
type BaseRecord,
6+
type CrudFilter,
67
type CrudFilters,
8+
type CrudSort,
79
type HttpError,
810
type Pagination,
911
type Prettify,
@@ -120,6 +122,9 @@ export type UseDataGridReturnType<
120122
*
121123
*/
122124

125+
const defaultPermanentFilter: CrudFilter[] = [];
126+
const defaultPermanentSort: CrudSort[] = [];
127+
123128
export function useDataGrid<
124129
TQueryFnData extends BaseRecord = BaseRecord,
125130
TError extends HttpError = HttpError,
@@ -207,8 +212,10 @@ export function useDataGrid<
207212
(sortersFromProp?.mode || "server") === "server";
208213
const isPaginationEnabled = (pagination?.mode ?? "server") !== "off";
209214

210-
const preferredPermanentSorters = sortersFromProp?.permanent ?? [];
211-
const preferredPermanentFilters = filtersFromProp?.permanent ?? [];
215+
const preferredPermanentSorters =
216+
sortersFromProp?.permanent ?? defaultPermanentSort;
217+
const preferredPermanentFilters =
218+
filtersFromProp?.permanent ?? defaultPermanentFilter;
212219

213220
const handlePageChange = (page: number) => {
214221
if (isPaginationEnabled) {
@@ -303,6 +310,14 @@ export function useDataGrid<
303310
});
304311
};
305312

313+
const transformedSortModel = useMemo(
314+
() =>
315+
transformCrudSortingToSortModel(
316+
differenceWith(sorters, preferredPermanentSorters, isEqual),
317+
),
318+
[sorters, preferredPermanentSorters],
319+
);
320+
306321
return {
307322
tableQuery,
308323
dataGridProps: {
@@ -312,9 +327,7 @@ export function useDataGrid<
312327
rowCount,
313328
...dataGridPaginationValues(),
314329
sortingMode: isServerSideSortingEnabled ? "server" : "client",
315-
sortModel: transformCrudSortingToSortModel(
316-
differenceWith(sorters, preferredPermanentSorters, isEqual),
317-
),
330+
sortModel: transformedSortModel,
318331
onSortModelChange: handleSortModelChange,
319332
filterMode: isServerSideFilteringEnabled ? "server" : "client",
320333
filterModel: transformCrudFiltersToFilterModel(
@@ -328,7 +341,7 @@ export function useDataGrid<
328341
return [key, (value as any).type];
329342
}),
330343
);
331-
const isStateChanged = !isEqual(newColumnsTypes, columnsTypes);
344+
const isStateChanged = !isEqual(newColumnsTypes, columnsTypes.current);
332345

333346
if (isStateChanged) {
334347
columnsTypes.current = newColumnsTypes;

0 commit comments

Comments
 (0)