Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGrid] Fix pagination when pagination={undefined} #13349

Merged
merged 5 commits into from
Jul 4, 2024
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
16 changes: 14 additions & 2 deletions packages/x-data-grid/src/DataGrid/useDataGridProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,26 @@ export const useDataGridProps = <R extends GridValidRowModel>(inProps: DataGridP
[themedProps.slots],
);

const injectDefaultProps = React.useMemo(() => {
return (
Object.keys(DATA_GRID_PROPS_DEFAULT_VALUES) as Array<
keyof DataGridPropsWithDefaultValues<any>
>
).reduce((acc, key) => {
// @ts-ignore
acc[key] = themedProps[key] ?? DATA_GRID_PROPS_DEFAULT_VALUES[key];
romgrk marked this conversation as resolved.
Show resolved Hide resolved
return acc;
}, {} as DataGridPropsWithDefaultValues<any>);
}, [themedProps]);

return React.useMemo<DataGridProcessedProps<R>>(
() => ({
...DATA_GRID_PROPS_DEFAULT_VALUES,
...themedProps,
...injectDefaultProps,
localeText,
slots,
...DATA_GRID_FORCED_PROPS,
}),
[themedProps, localeText, slots],
[themedProps, localeText, slots, injectDefaultProps],
);
};
32 changes: 31 additions & 1 deletion packages/x-data-grid/src/tests/DataGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { createRenderer } from '@mui/internal-test-utils';
import { expect } from 'chai';
import { DataGrid } from '@mui/x-data-grid';
import { DataGrid, DATA_GRID_PROPS_DEFAULT_VALUES } from '@mui/x-data-grid';

const isJSDOM = /jsdom/.test(window.navigator.userAgent);

Expand Down Expand Up @@ -62,4 +62,34 @@ describe('<DataGrid />', () => {
</div>,
);
});

it('should not cause unexpected behavior when props are explictly set to undefined', () => {
const rows = [
{ id: 'a', col1: 'Hello', col2: 'World' },
{ id: 'constructor', col1: 'DataGridPro', col2: 'is Awesome' },
{ id: 'hasOwnProperty', col1: 'MUI', col2: 'is Amazing' },
];

const columns = [
{ field: 'col1', headerName: 'Column 1', width: 150 },
{ field: 'col2', headerName: 'Column 2', width: 150 },
];
expect(() => {
render(
<DataGrid
{...(
Object.keys(DATA_GRID_PROPS_DEFAULT_VALUES) as Array<
keyof typeof DATA_GRID_PROPS_DEFAULT_VALUES
>
).reduce((acc, key) => {
// @ts-ignore
acc[key] = undefined;
return acc;
}, {})}
rows={rows}
columns={columns}
/>,
);
}).not.toErrorDev();
});
});