Skip to content

Commit

Permalink
[DataGrid] Deselect selected row on click (#15509)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Cherniavskyi <[email protected]>
  • Loading branch information
k-rajat19 and cherniavskii authored Dec 10, 2024
1 parent 3a67270 commit 1b53854
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -534,13 +534,13 @@ export const useGridRowSelection = (
const isMultipleSelectionDisabled =
!checkboxSelection && !hasCtrlKey && !isKeyboardEvent(event);
const resetSelection = !canHaveMultipleSelection || isMultipleSelectionDisabled;
const selectedRowsCount = apiRef.current.getSelectedRows().size;

const isSelected = apiRef.current.isRowSelected(id);

if (resetSelection) {
apiRef.current.selectRow(id, !isMultipleSelectionDisabled ? !isSelected : true, true);
if (canHaveMultipleSelection && selectedRowsCount > 1 && !hasCtrlKey) {
apiRef.current.selectRow(id, true, resetSelection);
} else {
apiRef.current.selectRow(id, !isSelected, false);
const isSelected = apiRef.current.isRowSelected(id);
apiRef.current.selectRow(id, !isSelected, resetSelection);
}
},
[apiRef, canHaveMultipleSelection, checkboxSelection],
Expand Down
4 changes: 2 additions & 2 deletions packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ describe('<DataGrid /> - Row selection', () => {
expect(getSelectedRowIds()).to.deep.equal([1]);
});

it(`should not deselect the selected row on click WITHOUT ctrl or meta pressed`, () => {
it(`should deselect the selected row on click`, () => {
render(<TestDataGridSelection />);
fireEvent.click(getCell(0, 0));
expect(getSelectedRowIds()).to.deep.equal([0]);
fireEvent.click(getCell(0, 0));
expect(getSelectedRowIds()).to.deep.equal([0]);
expect(getSelectedRowIds()).to.deep.equal([]);
});

['metaKey', 'ctrlKey'].forEach((key) => {
Expand Down

0 comments on commit 1b53854

Please sign in to comment.