From a2c4746a16581315bda4228f28758d3daef008ff Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Tue, 3 Dec 2024 16:37:04 +0500 Subject: [PATCH] [DataGrid] Fix deselection not working with `isRowSelectable` (#15692) Co-authored-by: Armin Mehinovic <4390250+arminmeh@users.noreply.github.com> Co-authored-by: Kenan Yusuf --- .../migration-data-grid-v7/migration-data-grid-v7.md | 1 + .../components/columnSelection/GridHeaderCheckbox.tsx | 4 ++++ .../src/tests/rowSelection.DataGrid.test.tsx | 11 +++++++++++ 3 files changed, 16 insertions(+) diff --git a/docs/data/migration/migration-data-grid-v7/migration-data-grid-v7.md b/docs/data/migration/migration-data-grid-v7/migration-data-grid-v7.md index 5827d8c96f20b..0d9e259146c91 100644 --- a/docs/data/migration/migration-data-grid-v7/migration-data-grid-v7.md +++ b/docs/data/migration/migration-data-grid-v7/migration-data-grid-v7.md @@ -38,6 +38,7 @@ Below are described the steps you need to make to migrate from v7 to v8. - The default value of the `rowSelectionPropagation` prop has been changed to `{ parents: true, descendants: true }` which means that the selection will be propagated to the parents and descendants by default. To revert to the previous behavior, pass `rowSelectionPropagation={{ parents: false, descendants: false }}`. - The prop `indeterminateCheckboxAction` has been removed. Clicking on an indeterminate checkbox "selects" the unselected descendants. +- The "Select all" checkbox would now be checked when all the selectable rows are selected, ignoring rows that are not selectable because of the `isRowSelectable` prop. ### Changes to the public API diff --git a/packages/x-data-grid/src/components/columnSelection/GridHeaderCheckbox.tsx b/packages/x-data-grid/src/components/columnSelection/GridHeaderCheckbox.tsx index a3e7877b9b499..d20cf2cd870f3 100644 --- a/packages/x-data-grid/src/components/columnSelection/GridHeaderCheckbox.tsx +++ b/packages/x-data-grid/src/components/columnSelection/GridHeaderCheckbox.tsx @@ -68,10 +68,14 @@ const GridHeaderCheckbox = React.forwardRef>((acc, id) => { + if (!apiRef.current.isRowSelectable(id)) { + return acc; + } acc[id] = true; return acc; }, {}); }, [ + apiRef, rootProps.pagination, rootProps.checkboxSelectionVisibleOnly, paginatedVisibleRowIds, diff --git a/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx b/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx index fcf5f5924b595..c9018d6db8e3c 100644 --- a/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx +++ b/packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx @@ -671,6 +671,17 @@ describe(' - Row selection', () => { ); }).not.toErrorDev(); }); + + it('should set the "Select all" checkbox to selected state on clicking even when some rows are not selectable', () => { + render( + Number(id) % 2 === 0} + />, + ); + fireEvent.click(getColumnHeaderCell(0).querySelector('input')!); + expect(getColumnHeaderCell(0).querySelector('input')).to.have.property('checked', true); + }); }); describe('prop: rows', () => {