From ce28aa118099b1aa7f7427665c70d76a71145bb2 Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Wed, 20 Mar 2024 16:38:32 +0500 Subject: [PATCH] [DataGrid] Make `rows` an optional prop (#12478) --- docs/pages/x/api/data-grid/data-grid-premium.json | 8 ++++---- docs/pages/x/api/data-grid/data-grid-pro.json | 8 ++++---- docs/pages/x/api/data-grid/data-grid.json | 8 ++++---- .../src/DataGridPremium/DataGridPremium.tsx | 3 ++- .../src/models/dataGridPremiumProps.ts | 5 +++-- .../x-data-grid-pro/src/DataGridPro/DataGridPro.tsx | 3 ++- .../x-data-grid-pro/src/models/dataGridProProps.ts | 7 ++++--- .../src/tests/rows.DataGridPro.test.tsx | 3 ++- packages/x-data-grid/src/DataGrid/DataGrid.tsx | 3 ++- .../x-data-grid/src/DataGrid/useDataGridProps.ts | 1 + .../x-data-grid/src/models/props/DataGridProps.ts | 13 +++++++------ 11 files changed, 35 insertions(+), 27 deletions(-) diff --git a/docs/pages/x/api/data-grid/data-grid-premium.json b/docs/pages/x/api/data-grid/data-grid-premium.json index 25c4610c05eb..9ce3955ee724 100644 --- a/docs/pages/x/api/data-grid/data-grid-premium.json +++ b/docs/pages/x/api/data-grid/data-grid-premium.json @@ -4,10 +4,6 @@ "type": { "name": "arrayOf", "description": "Array<object>" }, "required": true }, - "rows": { - "type": { "name": "arrayOf", "description": "Array<object>" }, - "required": true - }, "aggregationFunctions": { "type": { "name": "object" }, "default": "GRID_AGGREGATION_FUNCTIONS" @@ -563,6 +559,10 @@ "rowModesModel": { "type": { "name": "object" } }, "rowPositionsDebounceMs": { "type": { "name": "number" }, "default": "166" }, "rowReordering": { "type": { "name": "bool" }, "default": "false" }, + "rows": { + "type": { "name": "arrayOf", "description": "Array<object>" }, + "default": "[]" + }, "rowSelection": { "type": { "name": "bool" }, "default": "true" }, "rowSelectionModel": { "type": { diff --git a/docs/pages/x/api/data-grid/data-grid-pro.json b/docs/pages/x/api/data-grid/data-grid-pro.json index 7b50ed4ccad7..f85d38ad7148 100644 --- a/docs/pages/x/api/data-grid/data-grid-pro.json +++ b/docs/pages/x/api/data-grid/data-grid-pro.json @@ -4,10 +4,6 @@ "type": { "name": "arrayOf", "description": "Array<object>" }, "required": true }, - "rows": { - "type": { "name": "arrayOf", "description": "Array<object>" }, - "required": true - }, "apiRef": { "type": { "name": "shape", "description": "{ current: object }" } }, "aria-label": { "type": { "name": "string" } }, "aria-labelledby": { "type": { "name": "string" } }, @@ -501,6 +497,10 @@ "rowModesModel": { "type": { "name": "object" } }, "rowPositionsDebounceMs": { "type": { "name": "number" }, "default": "166" }, "rowReordering": { "type": { "name": "bool" }, "default": "false" }, + "rows": { + "type": { "name": "arrayOf", "description": "Array<object>" }, + "default": "[]" + }, "rowSelection": { "type": { "name": "bool" }, "default": "true" }, "rowSelectionModel": { "type": { diff --git a/docs/pages/x/api/data-grid/data-grid.json b/docs/pages/x/api/data-grid/data-grid.json index d9ba3743b0d7..ac8d1f022003 100644 --- a/docs/pages/x/api/data-grid/data-grid.json +++ b/docs/pages/x/api/data-grid/data-grid.json @@ -4,10 +4,6 @@ "type": { "name": "arrayOf", "description": "Array<object>" }, "required": true }, - "rows": { - "type": { "name": "arrayOf", "description": "Array<object>" }, - "required": true - }, "apiRef": { "type": { "name": "shape", "description": "{ current: object }" } }, "aria-label": { "type": { "name": "string" } }, "aria-labelledby": { "type": { "name": "string" } }, @@ -417,6 +413,10 @@ "rowHeight": { "type": { "name": "number" }, "default": "52" }, "rowModesModel": { "type": { "name": "object" } }, "rowPositionsDebounceMs": { "type": { "name": "number" }, "default": "166" }, + "rows": { + "type": { "name": "arrayOf", "description": "Array<object>" }, + "default": "[]" + }, "rowSelection": { "type": { "name": "bool" }, "default": "true" }, "rowSelectionModel": { "type": { diff --git a/packages/x-data-grid-premium/src/DataGridPremium/DataGridPremium.tsx b/packages/x-data-grid-premium/src/DataGridPremium/DataGridPremium.tsx index 9007fa80d653..80010e177699 100644 --- a/packages/x-data-grid-premium/src/DataGridPremium/DataGridPremium.tsx +++ b/packages/x-data-grid-premium/src/DataGridPremium/DataGridPremium.tsx @@ -926,8 +926,9 @@ DataGridPremiumRaw.propTypes = { rowReordering: PropTypes.bool, /** * Set of rows of type [[GridRowsProp]]. + * @default [] */ - rows: PropTypes.arrayOf(PropTypes.object).isRequired, + rows: PropTypes.arrayOf(PropTypes.object), /** * If `false`, the row selection mode is disabled. * @default true diff --git a/packages/x-data-grid-premium/src/models/dataGridPremiumProps.ts b/packages/x-data-grid-premium/src/models/dataGridPremiumProps.ts index ee3b030e670e..ebba8956e323 100644 --- a/packages/x-data-grid-premium/src/models/dataGridPremiumProps.ts +++ b/packages/x-data-grid-premium/src/models/dataGridPremiumProps.ts @@ -38,7 +38,7 @@ export interface DataGridPremiumPropsWithComplexDefaultValueBeforeProcessing */ export interface DataGridPremiumProps extends Omit< - Partial & + Partial> & DataGridPremiumPropsWithComplexDefaultValueBeforeProcessing & DataGridPremiumPropsWithoutDefaultValue, DataGridPremiumForcedPropsKey @@ -64,7 +64,8 @@ export type DataGridPremiumForcedPropsKey = 'signature'; * None of the entry of this interface should be optional, they all have default values and `DataGridProps` already applies a `Partial` for the public interface. * The controlled model do not have a default value at the prop processing level, so they must be defined in `DataGridOtherProps`. */ -export interface DataGridPremiumPropsWithDefaultValue extends DataGridProPropsWithDefaultValue { +export interface DataGridPremiumPropsWithDefaultValue + extends DataGridProPropsWithDefaultValue { /** * If `true`, the cell selection mode is enabled. * @default false diff --git a/packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx b/packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx index ca490f839706..5ee1b16e5ca0 100644 --- a/packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx +++ b/packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx @@ -818,8 +818,9 @@ DataGridProRaw.propTypes = { rowReordering: PropTypes.bool, /** * Set of rows of type [[GridRowsProp]]. + * @default [] */ - rows: PropTypes.arrayOf(PropTypes.object).isRequired, + rows: PropTypes.arrayOf(PropTypes.object), /** * If `false`, the row selection mode is disabled. * @default true diff --git a/packages/x-data-grid-pro/src/models/dataGridProProps.ts b/packages/x-data-grid-pro/src/models/dataGridProProps.ts index de56c3be465e..e7425e47b4ac 100644 --- a/packages/x-data-grid-pro/src/models/dataGridProProps.ts +++ b/packages/x-data-grid-pro/src/models/dataGridProProps.ts @@ -41,7 +41,7 @@ interface DataGridProPropsWithComplexDefaultValueBeforeProcessing */ export interface DataGridProProps extends Omit< - Partial & + Partial> & DataGridProPropsWithComplexDefaultValueBeforeProcessing & DataGridProPropsWithoutDefaultValue, DataGridProForcedPropsKey @@ -56,7 +56,7 @@ interface DataGridProPropsWithComplexDefaultValueAfterProcessing * The props of the `DataGridPro` component after the pre-processing phase. */ export interface DataGridProProcessedProps - extends DataGridProPropsWithDefaultValue, + extends DataGridProPropsWithDefaultValue, DataGridProPropsWithComplexDefaultValueAfterProcessing, Omit, 'componentsProps'> {} @@ -67,7 +67,8 @@ export type DataGridProForcedPropsKey = 'signature'; * None of the entry of this interface should be optional, they all have default values and `DataGridProps` already applies a `Partial` for the public interface * The controlled model do not have a default value at the prop processing level, so they must be defined in `DataGridOtherProps` */ -export interface DataGridProPropsWithDefaultValue extends DataGridPropsWithDefaultValues { +export interface DataGridProPropsWithDefaultValue + extends DataGridPropsWithDefaultValues { /** * Set the area in `px` at the bottom of the grid viewport where onRowsScrollEnd is called. * @default 80 diff --git a/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx index 0433592c536b..d4a7fb6d0747 100644 --- a/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rows.DataGridPro.test.tsx @@ -21,13 +21,14 @@ import { GridApi, gridFocusCellSelector, gridClasses, + GridValidRowModel, } from '@mui/x-data-grid-pro'; import { useBasicDemoData, getBasicGridData } from '@mui/x-data-grid-generator'; const isJSDOM = /jsdom/.test(window.navigator.userAgent); describe(' - Rows', () => { - let baselineProps: DataGridProProps; + let baselineProps: DataGridProProps & { rows: GridValidRowModel }; const { clock, render } = createRenderer({ clock: 'fake' }); diff --git a/packages/x-data-grid/src/DataGrid/DataGrid.tsx b/packages/x-data-grid/src/DataGrid/DataGrid.tsx index 013d06d2cf2c..3ae35b011893 100644 --- a/packages/x-data-grid/src/DataGrid/DataGrid.tsx +++ b/packages/x-data-grid/src/DataGrid/DataGrid.tsx @@ -680,8 +680,9 @@ DataGridRaw.propTypes = { rowPositionsDebounceMs: PropTypes.number, /** * Set of rows of type [[GridRowsProp]]. + * @default [] */ - rows: PropTypes.arrayOf(PropTypes.object).isRequired, + rows: PropTypes.arrayOf(PropTypes.object), /** * If `false`, the row selection mode is disabled. * @default true diff --git a/packages/x-data-grid/src/DataGrid/useDataGridProps.ts b/packages/x-data-grid/src/DataGrid/useDataGridProps.ts index af9812ee7466..3423bb66f36d 100644 --- a/packages/x-data-grid/src/DataGrid/useDataGridProps.ts +++ b/packages/x-data-grid/src/DataGrid/useDataGridProps.ts @@ -33,6 +33,7 @@ export const DATA_GRID_PROPS_DEFAULT_VALUES: DataGridPropsWithDefaultValues = { checkboxSelectionVisibleOnly: false, columnBufferPx: 150, rowBufferPx: 150, + rows: [], rowSelection: true, disableColumnFilter: false, disableColumnMenu: false, diff --git a/packages/x-data-grid/src/models/props/DataGridProps.ts b/packages/x-data-grid/src/models/props/DataGridProps.ts index eca76ffebad0..b21d37e1a123 100644 --- a/packages/x-data-grid/src/models/props/DataGridProps.ts +++ b/packages/x-data-grid/src/models/props/DataGridProps.ts @@ -45,7 +45,7 @@ export interface GridExperimentalFeatures { * The props users can give to the `DataGrid` component. */ export type DataGridProps = Omit< - Partial & + Partial> & DataGridPropsWithComplexDefaultValueBeforeProcessing & DataGridPropsWithoutDefaultValue, DataGridForcedPropsKey @@ -105,7 +105,7 @@ export interface DataGridPropsWithComplexDefaultValueBeforeProcessing { * The controlled model do not have a default value at the prop processing level, so they must be defined in `DataGridOtherProps` * TODO: add multiSortKey */ -export interface DataGridPropsWithDefaultValues { +export interface DataGridPropsWithDefaultValues { /** * If `true`, the Data Grid height is dynamic and follow the number of rows in the Data Grid. * @default false @@ -275,6 +275,11 @@ export interface DataGridPropsWithDefaultValues { * @default "client" */ paginationMode: GridFeatureMode; + /** + * Set of rows of type [[GridRowsProp]]. + * @default [] + */ + rows: GridRowsProp; /** * Sets the height in pixel of a row in the Data Grid. * @default 52 @@ -723,10 +728,6 @@ export interface DataGridPropsWithoutDefaultValue; /** * The initial state of the DataGrid. * The data in it will be set in the state on initialization but will not be controlled.