Skip to content

Commit bb24b9e

Browse files
michelengelenMBilalShaficherniavskii
authored
[DataGrid] Log error if rowCount is used with client-side pagination (#12448)
Co-authored-by: Bilal Shafi <bilalshafidev@gmail.com> Co-authored-by: Andrew Cherniavskyi <andrew@mui.com>
1 parent 7de42b2 commit bb24b9e

10 files changed

+65
-7
lines changed

docs/data/data-grid/pagination/pagination.md

+5
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ React.useEffect(() => {
120120
<DataGrid rowCount={rowCountState} />;
121121
```
122122

123+
:::warning
124+
The `rowCount` prop is used in server-side pagination mode to inform the DataGrid about the total number of rows in your dataset.
125+
This prop is ignored when the `paginationMode` is set to `client`, i.e. when the pagination is handled on the client-side.
126+
:::
127+
123128
{{"demo": "ServerPaginationGrid.js", "bg": "inline"}}
124129

125130
### Cursor implementation

packages/x-data-grid-pro/src/internals/propValidation.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { PropValidator, propValidatorsDataGrid } from '@mui/x-data-grid/internals';
1+
import {
2+
GridSignature,
3+
PropValidator,
4+
isNumber,
5+
propValidatorsDataGrid,
6+
} from '@mui/x-data-grid/internals';
27
import { DataGridProProcessedProps } from '../models/dataGridProProps';
38

49
export const propValidatorsDataGridPro: PropValidator<DataGridProProcessedProps>[] = [
@@ -18,4 +23,11 @@ export const propValidatorsDataGridPro: PropValidator<DataGridProProcessedProps>
1823
props.checkboxSelectionVisibleOnly &&
1924
'MUI X: The `checkboxSelectionVisibleOnly` prop has no effect when the pagination is not enabled.') ||
2025
undefined,
26+
(props) =>
27+
(props.signature !== GridSignature.DataGrid &&
28+
props.paginationMode === 'client' &&
29+
props.rowsLoadingMode !== 'server' &&
30+
isNumber(props.rowCount) &&
31+
'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"`.') ||
32+
undefined,
2133
];

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

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ describe('<DataGridPro /> - Events params', () => {
340340
sortingMode="server"
341341
filterMode="server"
342342
rowsLoadingMode="server"
343+
paginationMode="server"
343344
rowCount={50}
344345
/>,
345346
);

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

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ describe('<DataGridPro /> - Lazy loader', () => {
5151
sortingMode="server"
5252
filterMode="server"
5353
rowsLoadingMode="server"
54+
paginationMode="server"
5455
/>
5556
</div>
5657
);

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

+8
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ describe('<DataGridPro /> - Pagination', () => {
102102
expect(getColumnValues(0)).to.deep.equal(['0', '1']);
103103
});
104104
});
105+
106+
it('should log an error if rowCount is used with client-side pagination', () => {
107+
expect(() => {
108+
render(<DataGridPro rows={[]} columns={[]} paginationMode="client" rowCount={100} />);
109+
}).toErrorDev([
110+
'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"`.',
111+
]);
112+
});
105113
});

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,9 @@ describe('<DataGridPro /> - Rows', () => {
959959
it('should not show total row count in footer if `rowCount === rows.length`', () => {
960960
const { rows, columns } = getBasicGridData(10, 2);
961961
const rowCount = rows.length;
962-
render(<TestCase rows={rows} columns={columns} rowCount={rowCount} />);
962+
render(
963+
<TestCase rows={rows} columns={columns} rowCount={rowCount} paginationMode="server" />,
964+
);
963965

964966
const rowCountElement = document.querySelector<HTMLElement>(`.${gridClasses.rowCount}`);
965967
expect(rowCountElement!.textContent).to.equal(`Total Rows: ${rows.length}`);
@@ -968,7 +970,9 @@ describe('<DataGridPro /> - Rows', () => {
968970
it('should show total row count in footer if `rowCount !== rows.length`', () => {
969971
const { rows, columns } = getBasicGridData(10, 2);
970972
const rowCount = rows.length + 10;
971-
render(<TestCase rows={rows} columns={columns} rowCount={rowCount} />);
973+
render(
974+
<TestCase rows={rows} columns={columns} rowCount={rowCount} paginationMode="server" />,
975+
);
972976

973977
const rowCountElement = document.querySelector<HTMLElement>(`.${gridClasses.rowCount}`);
974978
expect(rowCountElement!.textContent).to.equal(`Total Rows: ${rows.length} of ${rowCount}`);
@@ -977,7 +981,9 @@ describe('<DataGridPro /> - Rows', () => {
977981
it('should update total row count in footer on `rowCount` prop change', () => {
978982
const { rows, columns } = getBasicGridData(10, 2);
979983
let rowCount = rows.length;
980-
const { setProps } = render(<TestCase rows={rows} columns={columns} rowCount={rowCount} />);
984+
const { setProps } = render(
985+
<TestCase rows={rows} columns={columns} rowCount={rowCount} paginationMode="server" />,
986+
);
981987
rowCount += 1;
982988
setProps({ rowCount });
983989

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

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ describe('<DataGridPro /> - State persistence', () => {
162162
page: FULL_INITIAL_STATE.pagination?.paginationModel?.page!,
163163
pageSize: FULL_INITIAL_STATE.pagination?.paginationModel?.pageSize!,
164164
}}
165+
paginationMode="server"
165166
rowCount={FULL_INITIAL_STATE.pagination?.rowCount}
166167
pinnedColumns={FULL_INITIAL_STATE.pinnedColumns}
167168
// Some portable states don't have a controllable model
@@ -187,6 +188,7 @@ describe('<DataGridPro /> - State persistence', () => {
187188
page: FULL_INITIAL_STATE.pagination?.paginationModel?.page!,
188189
pageSize: FULL_INITIAL_STATE.pagination?.paginationModel?.pageSize!,
189190
}}
191+
paginationMode="server"
190192
rowCount={FULL_INITIAL_STATE.pagination?.rowCount}
191193
pinnedColumns={FULL_INITIAL_STATE.pinnedColumns}
192194
// Some portable states don't have a controllable model

packages/x-data-grid/src/internals/utils/propValidation.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { isNumber } from '../../utils/utils';
12
import { DataGridProcessedProps } from '../../models/props/DataGridProps';
3+
import { GridSignature } from '../../hooks/utils/useGridApiEventHandler';
24

35
export type PropValidator<TProps> = (props: TProps) => string | undefined;
46

@@ -13,13 +15,19 @@ export const propValidatorsDataGrid: PropValidator<DataGridProcessedProps>[] = [
1315
'Please remove one of these two props.',
1416
].join('\n')) ||
1517
undefined,
18+
(props) =>
19+
(props.signature === GridSignature.DataGrid &&
20+
props.paginationMode === 'client' &&
21+
isNumber(props.rowCount) &&
22+
'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"`.') ||
23+
undefined,
1624
];
1725

18-
const warnedOnceMap = new Set();
26+
const warnedOnceCache = new Set();
1927
const warnOnce = (message: string) => {
20-
if (!warnedOnceMap.has(message)) {
28+
if (!warnedOnceCache.has(message)) {
2129
console.error(message);
22-
warnedOnceMap.add(message);
30+
warnedOnceCache.add(message);
2331
}
2432
};
2533

@@ -34,3 +42,7 @@ export const validateProps = <TProps>(props: TProps, validators: PropValidator<T
3442
}
3543
});
3644
};
45+
46+
export const clearWarningsCache = () => {
47+
warnedOnceCache.clear();
48+
};

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

+8
Original file line numberDiff line numberDiff line change
@@ -708,4 +708,12 @@ describe('<DataGrid /> - Pagination', () => {
708708
setProps({ rows: rows.slice(0, 2) });
709709
}).not.to.throw();
710710
});
711+
712+
it('should log an error if rowCount is used with client-side pagination', () => {
713+
expect(() => {
714+
render(<BaselineTestCase paginationMode="client" rowCount={100} />);
715+
}).toErrorDev([
716+
'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"`.',
717+
]);
718+
});
711719
});

test/utils/mochaHooks.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { LicenseInfo } from '@mui/x-license';
33
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGrid } from '@mui/x-data-grid';
44
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingDataGridPro } from '@mui/x-data-grid-pro';
55
import { unstable_resetCleanupTracking as unstable_resetCleanupTrackingTreeView } from '@mui/x-tree-view';
6+
import { clearWarningsCache } from '@mui/x-data-grid/internals';
67

78
export function createXMochaHooks(coreMochaHooks = {}) {
89
const mochaHooks = {
@@ -30,5 +31,7 @@ export function createXMochaHooks(coreMochaHooks = {}) {
3031
sinon.restore();
3132
});
3233

34+
mochaHooks.afterEach.push(clearWarningsCache);
35+
3336
return mochaHooks;
3437
}

0 commit comments

Comments
 (0)