From e22ac3243067458ec0b13fd4751facafabb46bc8 Mon Sep 17 00:00:00 2001 From: Jan Potoms <2109932+Janpot@users.noreply.github.com> Date: Tue, 14 May 2024 17:31:56 +0200 Subject: [PATCH] [DataGrid] Fix `rowModesModel` controlled prop (#13056) --- .../src/tests/rowEditing.DataGridPro.test.tsx | 9 +++++++++ .../src/hooks/features/editing/useGridRowEditing.ts | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx index fa67b53ae0f15..b510d3bc63fdb 100644 --- a/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx @@ -1260,6 +1260,15 @@ describe(' - Row editing', () => { expect(getCell(0, 1)).not.to.have.class('MuiDataGrid-cell--editing'); }); + it('should stop edit mode when rowModesModel empty', () => { + const { setProps } = render( + , + ); + expect(getCell(0, 1)).to.have.class('MuiDataGrid-cell--editing'); + setProps({ rowModesModel: {} }); + expect(getCell(0, 1)).not.to.have.class('MuiDataGrid-cell--editing'); + }); + it('should ignode modifications if ignoreModifications=true', async () => { const { setProps } = render( , 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 95882331db340..33932d80672f3 100644 --- a/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts +++ b/packages/x-data-grid/src/hooks/features/editing/useGridRowEditing.ts @@ -718,7 +718,9 @@ export const useGridRowEditing = ( const copyOfPrevRowModesModel = prevRowModesModel.current; prevRowModesModel.current = deepClone(rowModesModel); // Do a deep-clone because the attributes might be changed later - Object.entries(rowModesModel).forEach(([id, params]) => { + const ids = new Set([...Object.keys(rowModesModel), ...Object.keys(copyOfPrevRowModesModel)]); + Array.from(ids).forEach((id) => { + const params = rowModesModel[id] ?? { mode: GridRowModes.View }; const prevMode = copyOfPrevRowModesModel[id]?.mode || GridRowModes.View; const originalId = idToIdLookup[id] ?? id; if (params.mode === GridRowModes.Edit && prevMode === GridRowModes.View) {