Skip to content

Commit eedaea7

Browse files
[DataGrid] Fix pagination state synchronization issue (#19290)
Co-authored-by: Michel Engelen <[email protected]>
1 parent e25895d commit eedaea7

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRenderer, act } from '@mui/internal-test-utils';
1+
import { createRenderer, act, waitFor } from '@mui/internal-test-utils';
22
import { getColumnValues } from 'test/utils/helperFn';
33
import * as React from 'react';
44
import { RefObject } from '@mui/x-internals/types';
@@ -111,4 +111,45 @@ describe('<DataGridPro /> - Pagination', () => {
111111
'MUI X: Usage of the `rowCount` prop with client side pagination (`paginationMode="client"`) has no effect. `rowCount` is only meant to be used with `paginationMode="server"`.',
112112
]);
113113
});
114+
115+
// Test for https://github.com/mui/mui-x/issues/19281
116+
it('should sync pagination prop with state properly', async () => {
117+
const columns = [{ field: 'name' }];
118+
const rows = [
119+
{ id: 1, name: 'Row 1' },
120+
{ id: 2, name: 'Row 2' },
121+
{ id: 3, name: 'Row 3' },
122+
{ id: 4, name: 'Row 4' },
123+
{ id: 5, name: 'Row 5' },
124+
{ id: 6, name: 'Row 6' },
125+
{ id: 7, name: 'Row 7' },
126+
{ id: 8, name: 'Row 8' },
127+
{ id: 9, name: 'Row 9' },
128+
{ id: 10, name: 'Row 10' },
129+
{ id: 11, name: 'Row 11' },
130+
];
131+
function TestComponent({ pagination = false }: { pagination?: boolean }) {
132+
return (
133+
<div style={{ width: 300, height: 500 }}>
134+
<DataGridPro
135+
columns={columns}
136+
rows={rows}
137+
initialState={{ pagination: { paginationModel: { pageSize: 5 } } }}
138+
pageSizeOptions={[5, 10]}
139+
pagination={pagination}
140+
disableVirtualization
141+
/>
142+
</div>
143+
);
144+
}
145+
146+
const { setProps } = render(<TestComponent />);
147+
expect(getColumnValues(0)).to.have.length(11);
148+
setProps({ pagination: true });
149+
150+
await waitFor(() => {
151+
const visibleValues = getColumnValues(0);
152+
expect(visibleValues).to.have.length(5);
153+
});
154+
});
114155
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ export const useGridPaginationModel = (
335335
apiRef.current.setState((state) => {
336336
const isEnabled = props.pagination === true;
337337
if (
338-
state.pagination.paginationMode === props.paginationMode ||
338+
state.pagination.paginationMode === props.paginationMode &&
339339
state.pagination.enabled === isEnabled
340340
) {
341341
return state;
@@ -346,7 +346,7 @@ export const useGridPaginationModel = (
346346
pagination: {
347347
...state.pagination,
348348
paginationMode: props.paginationMode,
349-
enabled: props.pagination === true,
349+
enabled: isEnabled,
350350
},
351351
};
352352
});

0 commit comments

Comments
 (0)