From 9042979b0f7b1f4847824fcad84019c1f549b7e2 Mon Sep 17 00:00:00 2001 From: Armin Mehinovic Date: Wed, 14 Aug 2024 09:18:59 +0200 Subject: [PATCH 1/2] Create another object for the state update to trigger cell selection event properly --- .../src/hooks/features/cellSelection/useGridCellSelection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/x-data-grid-premium/src/hooks/features/cellSelection/useGridCellSelection.ts b/packages/x-data-grid-premium/src/hooks/features/cellSelection/useGridCellSelection.ts index 5c4e3020ba61..d36c314e6292 100644 --- a/packages/x-data-grid-premium/src/hooks/features/cellSelection/useGridCellSelection.ts +++ b/packages/x-data-grid-premium/src/hooks/features/cellSelection/useGridCellSelection.ts @@ -146,7 +146,7 @@ export const useGridCellSelection = ( const rowsInRange = visibleRows.rows.slice(finalStartRowIndex, finalEndRowIndex + 1); const columnsInRange = visibleColumns.slice(finalStartColumnIndex, finalEndColumnIndex + 1); - const newModel = keepOtherSelected ? apiRef.current.getCellSelectionModel() : {}; + const newModel = keepOtherSelected ? { ...apiRef.current.getCellSelectionModel() } : {}; rowsInRange.forEach((row) => { if (!newModel[row.id]) { From 800d54f0e6a53b34558deb901752a4f123a1156c Mon Sep 17 00:00:00 2001 From: Armin Mehinovic Date: Wed, 21 Aug 2024 21:12:07 +0200 Subject: [PATCH 2/2] Add tests --- .../cellSelection.DataGridPremium.test.tsx | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx index 09efe492a468..1918719266a1 100644 --- a/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/cellSelection.DataGridPremium.test.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; -import { stub, SinonStub } from 'sinon'; +import { stub, SinonStub, spy } from 'sinon'; import { expect } from 'chai'; import { spyApi, getCell, grid } from 'test/utils/helperFn'; import { createRenderer, fireEvent, act, screen } from '@mui/internal-test-utils'; @@ -240,6 +240,43 @@ describe(' - Cell selection', () => { }); }); + describe('onCellSelectionModelChange', () => { + it('should update the selection state when a cell is selected', () => { + const onCellSelectionModelChange = spy(); + render( + , + ); + fireEvent.click(getCell(0, 0)); + + expect(onCellSelectionModelChange.callCount).to.equal(1); + expect(onCellSelectionModelChange.lastCall.args[0]).to.deep.equal({ '0': { id: true } }); + }); + + // Context: https://github.com/mui/mui-x/issues/14184 + it('should add the new cell selection range to the existing state', () => { + const onCellSelectionModelChange = spy(); + render( + , + ); + + // Add a new cell range to the selection + fireEvent.mouseDown(getCell(2, 0), { ctrlKey: true }); + fireEvent.mouseOver(getCell(3, 0), { ctrlKey: true }); + + expect(onCellSelectionModelChange.lastCall.args[0]).to.deep.equal({ + '0': { id: true }, + '2': { id: true }, + '3': { id: true }, + }); + }); + }); + describe('apiRef', () => { describe('selectCellRange', () => { it('should select all cells within the given arguments if end > start', () => {