From 2aa6306b47f951ab389e9ad95bfb4711bcaac321 Mon Sep 17 00:00:00 2001 From: sai6855 Date: Sun, 2 Jun 2024 20:08:54 +0530 Subject: [PATCH 1/5] Fix pagination when `pagination={undefined}` --- packages/x-data-grid/src/DataGrid/useDataGridProps.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/x-data-grid/src/DataGrid/useDataGridProps.ts b/packages/x-data-grid/src/DataGrid/useDataGridProps.ts index 89ace1f6d061d..d15e68b7921fd 100644 --- a/packages/x-data-grid/src/DataGrid/useDataGridProps.ts +++ b/packages/x-data-grid/src/DataGrid/useDataGridProps.ts @@ -108,6 +108,7 @@ export const useDataGridProps = (inProps: DataGridP () => ({ ...DATA_GRID_PROPS_DEFAULT_VALUES, ...themedProps, + paginationMode: themedProps.paginationMode ?? DATA_GRID_PROPS_DEFAULT_VALUES.paginationMode, localeText, slots, ...DATA_GRID_FORCED_PROPS, From b7e28c25a51e4f4f76eb1df6b314e2866d548c48 Mon Sep 17 00:00:00 2001 From: sai6855 Date: Sun, 2 Jun 2024 21:03:53 +0530 Subject: [PATCH 2/5] add test --- packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx b/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx index a245d201ee943..4353afb97b498 100644 --- a/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx @@ -751,4 +751,10 @@ describe(' - Pagination', () => { ].join('\n'), ); }); + + it('should not log an error if paginationMode is set to undefined', () => { + expect(() => { + render(); + }).not.toErrorDev(); + }); }); From a7f4bea7240e25e81c811a4345ad362093078861 Mon Sep 17 00:00:00 2001 From: sai6855 Date: Mon, 3 Jun 2024 22:07:10 +0530 Subject: [PATCH 3/5] refactor --- .../src/DataGrid/useDataGridProps.ts | 17 +++++++++-- .../x-data-grid/src/tests/DataGrid.test.tsx | 28 ++++++++++++++++++- .../src/tests/pagination.DataGrid.test.tsx | 6 ---- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/packages/x-data-grid/src/DataGrid/useDataGridProps.ts b/packages/x-data-grid/src/DataGrid/useDataGridProps.ts index d15e68b7921fd..2af3393e1ea14 100644 --- a/packages/x-data-grid/src/DataGrid/useDataGridProps.ts +++ b/packages/x-data-grid/src/DataGrid/useDataGridProps.ts @@ -104,15 +104,26 @@ export const useDataGridProps = (inProps: DataGridP [themedProps.slots], ); + const injectDefaultProps = React.useMemo(() => { + return ( + Object.keys(DATA_GRID_PROPS_DEFAULT_VALUES) as Array< + keyof DataGridPropsWithDefaultValues + > + ).reduce((acc, key) => { + // @ts-ignore + acc[key] = themedProps[key] ?? DATA_GRID_PROPS_DEFAULT_VALUES[key]; + return acc; + }, {} as DataGridPropsWithDefaultValues); + }, [themedProps]); + return React.useMemo>( () => ({ - ...DATA_GRID_PROPS_DEFAULT_VALUES, ...themedProps, - paginationMode: themedProps.paginationMode ?? DATA_GRID_PROPS_DEFAULT_VALUES.paginationMode, + ...injectDefaultProps, localeText, slots, ...DATA_GRID_FORCED_PROPS, }), - [themedProps, localeText, slots], + [themedProps, localeText, slots, injectDefaultProps], ); }; diff --git a/packages/x-data-grid/src/tests/DataGrid.test.tsx b/packages/x-data-grid/src/tests/DataGrid.test.tsx index 17011096eb2df..1f20d566931df 100644 --- a/packages/x-data-grid/src/tests/DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/DataGrid.test.tsx @@ -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); @@ -62,4 +62,30 @@ describe('', () => { , ); }); + + 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( + { + // @ts-ignore + acc[key] = undefined; + return acc; + }, {})} + />, + ), + ).not.toErrorDev(); + }); }); diff --git a/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx b/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx index 4353afb97b498..a245d201ee943 100644 --- a/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/pagination.DataGrid.test.tsx @@ -751,10 +751,4 @@ describe(' - Pagination', () => { ].join('\n'), ); }); - - it('should not log an error if paginationMode is set to undefined', () => { - expect(() => { - render(); - }).not.toErrorDev(); - }); }); From caf78ab74c748afc87cf0c407c515f6f7c1afa5a Mon Sep 17 00:00:00 2001 From: sai6855 Date: Mon, 3 Jun 2024 23:10:07 +0530 Subject: [PATCH 4/5] fix test --- packages/x-data-grid/src/tests/DataGrid.test.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/x-data-grid/src/tests/DataGrid.test.tsx b/packages/x-data-grid/src/tests/DataGrid.test.tsx index 1f20d566931df..f058407dc45fb 100644 --- a/packages/x-data-grid/src/tests/DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/DataGrid.test.tsx @@ -74,18 +74,22 @@ describe('', () => { { field: 'col1', headerName: 'Column 1', width: 150 }, { field: 'col2', headerName: 'Column 2', width: 150 }, ]; - expect( + expect(() => { render( { + {...( + 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(); + ); + }).not.toErrorDev(); }); }); From 63f3a7cf2bad74bb5b648f55d9d7638e4121593f Mon Sep 17 00:00:00 2001 From: Rom Grk Date: Wed, 3 Jul 2024 23:05:37 -0400 Subject: [PATCH 5/5] ci: run