Skip to content

Commit

Permalink
[DataGridPremium] Fix onCellSelectionModelChange not triggered when…
Browse files Browse the repository at this point in the history
… additional cell range is selected (#14199)
  • Loading branch information
arminmeh authored Aug 21, 2024
1 parent 3fb2093 commit 7aaecf5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -240,6 +240,43 @@ describe('<DataGridPremium /> - Cell selection', () => {
});
});

describe('onCellSelectionModelChange', () => {
it('should update the selection state when a cell is selected', () => {
const onCellSelectionModelChange = spy();
render(
<TestDataGridSelection
cellSelectionModel={{}}
onCellSelectionModelChange={onCellSelectionModelChange}
/>,
);
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(
<TestDataGridSelection
cellSelectionModel={{ '0': { id: true } }}
onCellSelectionModelChange={onCellSelectionModelChange}
/>,
);

// 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', () => {
Expand Down

0 comments on commit 7aaecf5

Please sign in to comment.