From 5bd1534a4c9b23c971b9a88fe6c475f1a6ad2698 Mon Sep 17 00:00:00 2001 From: michel Date: Wed, 31 Jul 2024 10:05:57 +0200 Subject: [PATCH 1/8] added field value resetting to default values based on field type to row editing --- .../features/editing/useGridRowEditing.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts index fe07ea384681a..ffd83f6f0a8c9 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts @@ -433,7 +433,28 @@ export const useGridRowEditing = ( let newValue = apiRef.current.getCellValue(id, field); if (fieldToFocus === field && (deleteValue || initialValue)) { - newValue = deleteValue ? '' : initialValue; + if (deleteValue) { + const fieldType = apiRef.current.getColumn(field).type; + switch (fieldType) { + case 'boolean': + newValue = false; + break; + case 'date': + case 'dateTime': + case 'number': + newValue = undefined; + break; + case 'singleSelect': + newValue = null; + break; + case 'string': + default: + newValue = ''; + break; + } + } else if (initialValue) { + newValue = initialValue; + } } acc[field] = { From 74562a26cb55f412a20cc1b287766106e7d1839b Mon Sep 17 00:00:00 2001 From: michel Date: Wed, 31 Jul 2024 10:22:23 +0200 Subject: [PATCH 2/8] refactored using a param object --- .../src/tests/keyboard.DataGrid.test.tsx | 126 +++++++++++++----- 1 file changed, 92 insertions(+), 34 deletions(-) diff --git a/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx b/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx index 27c196256ecde..64da322dd75f0 100644 --- a/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx @@ -848,6 +848,7 @@ describe(' - Keyboard', () => { function setupTest( rows: Record[], columns: GridColDef[], + editMode: DataGridProps['editMode'], ) { const valueSetterMock = spy>( (value, row, column) => { @@ -861,23 +862,26 @@ describe(' - Keyboard', () => { column.valueSetter = valueSetterMock; }); - render(); + render(); return { valueSetterMock }; } - function testResetValue( - keyType: 'Delete' | 'Backspace', - field: string, - type: GridColType, - value: string | number | Date | boolean, - ) { + type TestResetValueParams = { + editMode: DataGridProps['editMode']; + keyType: 'Delete' | 'Backspace'; + field: string; + type: GridColType; + value: string | number | Date | boolean; + }; + + function testResetValue({ editMode, keyType, field, type, value }: TestResetValueParams) { const columns: GridColDef[] = [ { field: 'id', editable: true }, { field, editable: true, type }, ]; const rows = [{ id: 1, [field]: value }]; - const { valueSetterMock } = setupTest(rows, columns); + const { valueSetterMock } = setupTest(rows, columns, editMode); const cell = getCell(0, 1); cell.focus(); @@ -889,83 +893,137 @@ describe(' - Keyboard', () => { }; } + const defaultParams: TestResetValueParams = { + editMode: 'cell', + keyType: 'Backspace', + field: 'name', + type: 'string', + value: 'John Doe', + }; + it(`should reset value on Backspace key press for number type`, () => { - const { cell, deletedValue } = testResetValue('Backspace', 'age', 'number', 24); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'age', + type: 'number', + value: 24, + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(undefined); }); it(`should reset value on Backspace key press for date type`, () => { - const { cell, deletedValue } = testResetValue('Backspace', 'birthdate', 'date', new Date()); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'birthdate', + type: 'date', + value: new Date(), + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(undefined); }); it(`should reset value on Backspace key press for dateTime type`, () => { - const { cell, deletedValue } = testResetValue( - 'Backspace', - 'appointment', - 'dateTime', - new Date(), - ); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'appointment', + type: 'dateTime', + value: new Date(), + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(undefined); }); it(`should reset value on Backspace key press for boolean type`, () => { - const { cell, deletedValue } = testResetValue('Backspace', 'isVerified', 'boolean', true); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'isVerified', + type: 'boolean', + value: true, + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(false); }); it(`should reset value on Backspace key press for singleSelect type`, () => { - const { cell, deletedValue } = testResetValue( - 'Backspace', - 'status', - 'singleSelect', - 'active', - ); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'status', + type: 'singleSelect', + value: 'active', + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(null); }); it(`should reset value on Delete key press for string type`, () => { - const { cell, deletedValue } = testResetValue('Delete', 'name', 'string', 'John Doe'); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'name', + type: 'string', + value: 'John Doe', + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(''); }); it(`should reset value on Delete key press for number type`, () => { - const { cell, deletedValue } = testResetValue('Delete', 'age', 'number', 24); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'age', + type: 'number', + value: 24, + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(undefined); }); it(`should reset value on Delete key press for date type`, () => { - const { cell, deletedValue } = testResetValue('Delete', 'birthdate', 'date', new Date()); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'birthdate', + type: 'date', + value: new Date(), + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(undefined); }); it(`should reset value on Delete key press for dateTime type`, () => { - const { cell, deletedValue } = testResetValue( - 'Delete', - 'appointment', - 'dateTime', - new Date(), - ); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'appointment', + type: 'dateTime', + value: new Date(), + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(undefined); }); it(`should reset value on Delete key press for boolean type`, () => { - const { cell, deletedValue } = testResetValue('Delete', 'isVerified', 'boolean', true); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'isVerified', + type: 'boolean', + value: true, + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(false); }); it(`should reset value on Delete key press for singleSelect type`, () => { - const { cell, deletedValue } = testResetValue('Delete', 'status', 'singleSelect', 'active'); + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'status', + type: 'singleSelect', + value: 'active', + }); expect(cell).to.equal(''); expect(deletedValue).to.equal(null); }); From 290224f4e00e60e45561f790737d4c0632c51cd7 Mon Sep 17 00:00:00 2001 From: michel Date: Wed, 31 Jul 2024 10:32:05 +0200 Subject: [PATCH 3/8] enhanced tests to use cell and row edit modes as well as Backspace and Delete keytypes --- .../src/tests/keyboard.DataGrid.test.tsx | 216 +++++++----------- 1 file changed, 84 insertions(+), 132 deletions(-) diff --git a/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx b/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx index 64da322dd75f0..d4be59dc09612 100644 --- a/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/keyboard.DataGrid.test.tsx @@ -893,139 +893,91 @@ describe(' - Keyboard', () => { }; } - const defaultParams: TestResetValueParams = { - editMode: 'cell', - keyType: 'Backspace', - field: 'name', - type: 'string', - value: 'John Doe', - }; - - it(`should reset value on Backspace key press for number type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - field: 'age', - type: 'number', - value: 24, - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(undefined); - }); - - it(`should reset value on Backspace key press for date type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - field: 'birthdate', - type: 'date', - value: new Date(), - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(undefined); - }); - - it(`should reset value on Backspace key press for dateTime type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - field: 'appointment', - type: 'dateTime', - value: new Date(), - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(undefined); - }); - - it(`should reset value on Backspace key press for boolean type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - field: 'isVerified', - type: 'boolean', - value: true, - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(false); - }); - - it(`should reset value on Backspace key press for singleSelect type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - field: 'status', - type: 'singleSelect', - value: 'active', - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(null); - }); - - it(`should reset value on Delete key press for string type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - keyType: 'Delete', - field: 'name', - type: 'string', - value: 'John Doe', - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(''); - }); - - it(`should reset value on Delete key press for number type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - keyType: 'Delete', - field: 'age', - type: 'number', - value: 24, - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(undefined); - }); - - it(`should reset value on Delete key press for date type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - keyType: 'Delete', - field: 'birthdate', - type: 'date', - value: new Date(), - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(undefined); - }); - - it(`should reset value on Delete key press for dateTime type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - keyType: 'Delete', - field: 'appointment', - type: 'dateTime', - value: new Date(), - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(undefined); - }); - - it(`should reset value on Delete key press for boolean type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - keyType: 'Delete', - field: 'isVerified', - type: 'boolean', - value: true, + const testWithEditmodeAndKeytype = ({ + editMode, + keyType, + }: Pick) => { + describe(`editMode="${editMode}" and ${keyType} key`, () => { + const defaultParams: TestResetValueParams = { + editMode, + keyType, + field: 'name', + type: 'string', + value: 'John Doe', + }; + + it(`should reset value for string type`, () => { + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + keyType: 'Delete', + field: 'name', + type: 'string', + value: 'John Doe', + }); + expect(cell).to.equal(''); + expect(deletedValue).to.equal(''); + }); + + it(`should reset value for number type`, () => { + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'age', + type: 'number', + value: 24, + }); + expect(cell).to.equal(''); + expect(deletedValue).to.equal(undefined); + }); + + it(`should reset value for date type`, () => { + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'birthdate', + type: 'date', + value: new Date(), + }); + expect(cell).to.equal(''); + expect(deletedValue).to.equal(undefined); + }); + + it(`should reset value dateTime type`, () => { + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'appointment', + type: 'dateTime', + value: new Date(), + }); + expect(cell).to.equal(''); + expect(deletedValue).to.equal(undefined); + }); + + it(`should reset value boolean type`, () => { + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'isVerified', + type: 'boolean', + value: true, + }); + expect(cell).to.equal(''); + expect(deletedValue).to.equal(false); + }); + + it(`should reset value singleSelect type`, () => { + const { cell, deletedValue } = testResetValue({ + ...defaultParams, + field: 'status', + type: 'singleSelect', + value: 'active', + }); + expect(cell).to.equal(''); + expect(deletedValue).to.equal(null); + }); }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(false); - }); + }; - it(`should reset value on Delete key press for singleSelect type`, () => { - const { cell, deletedValue } = testResetValue({ - ...defaultParams, - keyType: 'Delete', - field: 'status', - type: 'singleSelect', - value: 'active', - }); - expect(cell).to.equal(''); - expect(deletedValue).to.equal(null); - }); + testWithEditmodeAndKeytype({ editMode: 'cell', keyType: 'Delete' }); + testWithEditmodeAndKeytype({ editMode: 'cell', keyType: 'Backspace' }); + testWithEditmodeAndKeytype({ editMode: 'row', keyType: 'Delete' }); + testWithEditmodeAndKeytype({ editMode: 'row', keyType: 'Backspace' }); }); }); From b349526fad7ec62bab06f9dbe3895b4e6a6c4241 Mon Sep 17 00:00:00 2001 From: michel Date: Fri, 6 Sep 2024 12:28:45 +0200 Subject: [PATCH 4/8] moved getting a default cell value to a new API method --- .../features/editing/useGridCellEditing.ts | 19 +--------------- .../features/editing/useGridRowEditing.ts | 19 +--------------- .../hooks/features/rows/useGridParamsApi.ts | 22 +++++++++++++++++++ .../src/models/api/gridParamsApi.ts | 6 +++++ 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts index fa6afb47afafa..c28df707e6641 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts @@ -337,24 +337,7 @@ export const useGridCellEditing = ( let newValue = apiRef.current.getCellValue(id, field); if (deleteValue) { - const fieldType = apiRef.current.getColumn(field).type; - switch (fieldType) { - case 'boolean': - newValue = false; - break; - case 'date': - case 'dateTime': - case 'number': - newValue = undefined; - break; - case 'singleSelect': - newValue = null; - break; - case 'string': - default: - newValue = ''; - break; - } + newValue = apiRef.current.getDefaultCellValue(field); } else if (initialValue) { newValue = initialValue; } diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts index ffd83f6f0a8c9..53fbc3f32e556 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts @@ -434,24 +434,7 @@ export const useGridRowEditing = ( let newValue = apiRef.current.getCellValue(id, field); if (fieldToFocus === field && (deleteValue || initialValue)) { if (deleteValue) { - const fieldType = apiRef.current.getColumn(field).type; - switch (fieldType) { - case 'boolean': - newValue = false; - break; - case 'date': - case 'dateTime': - case 'number': - newValue = undefined; - break; - case 'singleSelect': - newValue = null; - break; - case 'string': - default: - newValue = ''; - break; - } + newValue = apiRef.current.getDefaultCellValue(field); } else if (initialValue) { newValue = initialValue; } diff --git a/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts b/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts index 165ba3ee211c0..d48ee5ca08cb1 100644 --- a/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts +++ b/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts @@ -106,6 +106,27 @@ export function useGridParamsApi(apiRef: React.MutableRefObject( + (field) => { + const colDef = apiRef.current.getColumn(field); + + switch (colDef.type) { + case 'boolean': + return false; + case 'date': + case 'dateTime': + case 'number': + return undefined; + case 'singleSelect': + return null; + case 'string': + default: + return ''; + } + }, + [apiRef], + ); + const getRowValue = React.useCallback( (row, colDef) => { const field = colDef.field; @@ -164,6 +185,7 @@ export function useGridParamsApi(apiRef: React.MutableRefObject(id: GridRowId, field: string) => V; + /** + * Gets the default value of a cell for the given `field`. + * @param {string} field The column field. + * @returns {'' | false | null | undefined} The default cell value. + */ + getDefaultCellValue: (field: string) => '' | false | null | undefined; /** * Gets the cell value. * Use it instead of `getCellValue` for better performance if you have `row` and `colDef`. From 066cfca537629c9895c30111033a9552a93f130a Mon Sep 17 00:00:00 2001 From: michel Date: Mon, 9 Sep 2024 10:02:23 +0200 Subject: [PATCH 5/8] generated new docs files --- docs/pages/x/api/data-grid/grid-api.json | 4 ++++ docs/translations/api-docs/data-grid/grid-api.json | 3 +++ 2 files changed, 7 insertions(+) diff --git a/docs/pages/x/api/data-grid/grid-api.json b/docs/pages/x/api/data-grid/grid-api.json index 5f5202edf84f0..43a5226da4515 100644 --- a/docs/pages/x/api/data-grid/grid-api.json +++ b/docs/pages/x/api/data-grid/grid-api.json @@ -111,6 +111,10 @@ "required": true, "isPremiumPlan": true }, + "getDefaultCellValue": { + "type": { "description": "(field: string) => '' | false | null | undefined" }, + "required": true + }, "getExpandedDetailPanels": { "type": { "description": "() => GridRowId[]" }, "required": true, diff --git a/docs/translations/api-docs/data-grid/grid-api.json b/docs/translations/api-docs/data-grid/grid-api.json index fd478d19f9542..ef8fb47b58fde 100644 --- a/docs/translations/api-docs/data-grid/grid-api.json +++ b/docs/translations/api-docs/data-grid/grid-api.json @@ -65,6 +65,9 @@ "getDataAsExcel": { "description": "Returns the grid data as an exceljs workbook.
This method is used internally by exportDataAsExcel." }, + "getDefaultCellValue": { + "description": "Gets the default value of a cell for the given field." + }, "getExpandedDetailPanels": { "description": "Returns the rows whose detail panel is open." }, "getFilterState": { "description": "Returns the filter state for the given filter model without applying it to the data grid." From cbf23bb03154efcfd300c10c20bb7029d6966f3a Mon Sep 17 00:00:00 2001 From: michel Date: Tue, 10 Sep 2024 09:11:50 +0200 Subject: [PATCH 6/8] moved function out of API into a scoped function specifically for editing --- docs/pages/x/api/data-grid/grid-api.json | 4 ---- .../api-docs/data-grid/grid-api.json | 3 --- .../features/editing/getDefaultCellValue.ts | 17 ++++++++++++++ .../features/editing/useGridCellEditing.ts | 3 ++- .../features/editing/useGridRowEditing.ts | 3 ++- .../hooks/features/rows/useGridParamsApi.ts | 22 ------------------- .../src/models/api/gridParamsApi.ts | 6 ----- 7 files changed, 21 insertions(+), 37 deletions(-) create mode 100644 packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts diff --git a/docs/pages/x/api/data-grid/grid-api.json b/docs/pages/x/api/data-grid/grid-api.json index 43a5226da4515..5f5202edf84f0 100644 --- a/docs/pages/x/api/data-grid/grid-api.json +++ b/docs/pages/x/api/data-grid/grid-api.json @@ -111,10 +111,6 @@ "required": true, "isPremiumPlan": true }, - "getDefaultCellValue": { - "type": { "description": "(field: string) => '' | false | null | undefined" }, - "required": true - }, "getExpandedDetailPanels": { "type": { "description": "() => GridRowId[]" }, "required": true, diff --git a/docs/translations/api-docs/data-grid/grid-api.json b/docs/translations/api-docs/data-grid/grid-api.json index ef8fb47b58fde..fd478d19f9542 100644 --- a/docs/translations/api-docs/data-grid/grid-api.json +++ b/docs/translations/api-docs/data-grid/grid-api.json @@ -65,9 +65,6 @@ "getDataAsExcel": { "description": "Returns the grid data as an exceljs workbook.
This method is used internally by exportDataAsExcel." }, - "getDefaultCellValue": { - "description": "Gets the default value of a cell for the given field." - }, "getExpandedDetailPanels": { "description": "Returns the rows whose detail panel is open." }, "getFilterState": { "description": "Returns the filter state for the given filter model without applying it to the data grid." diff --git a/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts b/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts new file mode 100644 index 0000000000000..76718e2ca5bfd --- /dev/null +++ b/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts @@ -0,0 +1,17 @@ +import { GridColDef } from '../../../models/colDef/gridColDef'; + +export const getDefaultCellValue = (colDef: GridColDef): '' | null | undefined | false => { + switch (colDef.type) { + case 'boolean': + return false; + case 'date': + case 'dateTime': + case 'number': + return undefined; + case 'singleSelect': + return null; + case 'string': + default: + return ''; + } +}; diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts index c28df707e6641..4ef5f842f78e2 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts @@ -39,6 +39,7 @@ import { GridCellEditStartReasons, GridCellEditStopReasons, } from '../../../models/params/gridEditCellParams'; +import { getDefaultCellValue } from './getDefaultCellValue'; export const useGridCellEditing = ( apiRef: React.MutableRefObject, @@ -337,7 +338,7 @@ export const useGridCellEditing = ( let newValue = apiRef.current.getCellValue(id, field); if (deleteValue) { - newValue = apiRef.current.getDefaultCellValue(field); + newValue = getDefaultCellValue(apiRef.current.getColumn(field)); } else if (initialValue) { newValue = initialValue; } diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts index 53fbc3f32e556..b06615582981d 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts @@ -46,6 +46,7 @@ import { GridRowEditStartReasons, } from '../../../models/params/gridRowParams'; import { GRID_ACTIONS_COLUMN_TYPE } from '../../../colDef'; +import { getDefaultCellValue } from './getDefaultCellValue'; export const useGridRowEditing = ( apiRef: React.MutableRefObject, @@ -434,7 +435,7 @@ export const useGridRowEditing = ( let newValue = apiRef.current.getCellValue(id, field); if (fieldToFocus === field && (deleteValue || initialValue)) { if (deleteValue) { - newValue = apiRef.current.getDefaultCellValue(field); + newValue = getDefaultCellValue(apiRef.current.getColumn(field)); } else if (initialValue) { newValue = initialValue; } diff --git a/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts b/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts index 2d6ad40aaa0ca..b663d9700a783 100644 --- a/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts +++ b/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts @@ -107,27 +107,6 @@ export function useGridParamsApi(apiRef: React.MutableRefObject( - (field) => { - const colDef = apiRef.current.getColumn(field); - - switch (colDef.type) { - case 'boolean': - return false; - case 'date': - case 'dateTime': - case 'number': - return undefined; - case 'singleSelect': - return null; - case 'string': - default: - return ''; - } - }, - [apiRef], - ); - const getRowValue = React.useCallback( (row, colDef) => { const field = colDef.field; @@ -186,7 +165,6 @@ export function useGridParamsApi(apiRef: React.MutableRefObject(id: GridRowId, field: string) => V; - /** - * Gets the default value of a cell for the given `field`. - * @param {string} field The column field. - * @returns {'' | false | null | undefined} The default cell value. - */ - getDefaultCellValue: (field: string) => '' | false | null | undefined; /** * Gets the cell value. * Use it instead of `getCellValue` for better performance if you have `row` and `colDef`. From 95515151d1f90460130e65b063ebd7791109cf56 Mon Sep 17 00:00:00 2001 From: Michel Engelen <32863416+michelengelen@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:54:46 +0200 Subject: [PATCH 7/8] Update packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts Co-authored-by: Bilal Shafi Signed-off-by: Michel Engelen <32863416+michelengelen@users.noreply.github.com> --- .../src/hooks/features/editing/getDefaultCellValue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts b/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts index 76718e2ca5bfd..8b3df8808b130 100644 --- a/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts +++ b/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts @@ -1,6 +1,6 @@ import { GridColDef } from '../../../models/colDef/gridColDef'; -export const getDefaultCellValue = (colDef: GridColDef): '' | null | undefined | false => { +export const getDefaultCellValue = (colDef: GridColDef) => { switch (colDef.type) { case 'boolean': return false; From e104b28ae24cc9b1cd1248ef5c0656f62fd69a28 Mon Sep 17 00:00:00 2001 From: michel Date: Tue, 17 Sep 2024 14:59:00 +0200 Subject: [PATCH 8/8] renamed file to utils.ts --- .../src/hooks/features/editing/useGridCellEditing.ts | 2 +- .../x-data-grid/src/hooks/features/editing/useGridRowEditing.ts | 2 +- .../hooks/features/editing/{getDefaultCellValue.ts => utils.ts} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename packages/x-data-grid/src/hooks/features/editing/{getDefaultCellValue.ts => utils.ts} (100%) diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts index 4ef5f842f78e2..490a975aad8e2 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridCellEditing.ts @@ -39,7 +39,7 @@ import { GridCellEditStartReasons, GridCellEditStopReasons, } from '../../../models/params/gridEditCellParams'; -import { getDefaultCellValue } from './getDefaultCellValue'; +import { getDefaultCellValue } from './utils'; export const useGridCellEditing = ( apiRef: React.MutableRefObject, diff --git a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts index b06615582981d..ea29ab7229297 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts @@ -46,7 +46,7 @@ import { GridRowEditStartReasons, } from '../../../models/params/gridRowParams'; import { GRID_ACTIONS_COLUMN_TYPE } from '../../../colDef'; -import { getDefaultCellValue } from './getDefaultCellValue'; +import { getDefaultCellValue } from './utils'; export const useGridRowEditing = ( apiRef: React.MutableRefObject, diff --git a/packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts b/packages/x-data-grid/src/hooks/features/editing/utils.ts similarity index 100% rename from packages/x-data-grid/src/hooks/features/editing/getDefaultCellValue.ts rename to packages/x-data-grid/src/hooks/features/editing/utils.ts