From 4a624b3c6fdaed5e6b76171f8d4456a5c63fdb06 Mon Sep 17 00:00:00 2001 From: Bilal Shafi Date: Mon, 25 Nov 2024 15:45:59 +0500 Subject: [PATCH] [DataGridPro] Fix selection propagation issue on initialization (#15461) --- .../rowSelection.DataGridPremium.test.tsx | 14 +++++++++++ .../tests/rowSelection.DataGridPro.test.tsx | 25 +++++++++++++++++++ .../rowSelection/useGridRowSelection.ts | 10 -------- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx index 88bb7b1dd69e..69a67346684e 100644 --- a/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/rowSelection.DataGridPremium.test.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import { act, createRenderer, fireEvent } from '@mui/internal-test-utils'; import { getCell } from 'test/utils/helperFn'; +import { spy } from 'sinon'; import { expect } from 'chai'; import { DataGridPremium, @@ -68,6 +69,19 @@ describe(' - Row selection', () => { ); } + it('should auto select parents when controlling row selection model', () => { + const onRowSelectionModelChange = spy(); + render( + , + ); + + expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal([ + 3, + 4, + 'auto-generated-row-category1/Cat B', + ]); + }); + it('should select all the children when selecting a parent', () => { render(); diff --git a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx index ad5d41c588c7..c9e93334b13a 100644 --- a/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/rowSelection.DataGridPro.test.tsx @@ -534,6 +534,18 @@ describe(' - Row selection', () => { ); } + it('should not auto select parents when controlling row selection model', () => { + const onRowSelectionModelChange = spy(); + render( + , + ); + + expect(onRowSelectionModelChange.callCount).to.equal(0); + }); + it('should select the parent only when selecting it', () => { render(); @@ -695,6 +707,19 @@ describe(' - Row selection', () => { ); } + it('should auto select parents when controlling row selection model', () => { + const onRowSelectionModelChange = spy(); + render( + , + ); + + expect(onRowSelectionModelChange.callCount).to.equal(2); // Dev mode calls twice + expect(onRowSelectionModelChange.lastCall.args[0]).to.deep.equal([2, 3, 4, 5, 6, 7, 1]); + }); + it('should select the parent only when selecting it', () => { render(); diff --git a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts index 7fc8dd13d2cc..2b67af5958ab 100644 --- a/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts +++ b/packages/x-data-grid/src/hooks/features/rowSelection/useGridRowSelection.ts @@ -450,12 +450,8 @@ export const useGridRowSelection = ( /* * EVENTS */ - const isFirstRender = React.useRef(true); const removeOutdatedSelection = React.useCallback( (sortModelUpdated = false) => { - if (isFirstRender.current) { - return; - } const currentSelection = gridRowSelectionStateSelector(apiRef.current.state); const rowsLookup = gridRowsLookupSelector(apiRef); const filteredRowsLookup = gridFilteredRowsLookupSelector(apiRef); @@ -786,10 +782,4 @@ export const useGridRowSelection = ( React.useEffect(() => { runIfRowSelectionIsEnabled(removeOutdatedSelection); }, [removeOutdatedSelection, runIfRowSelectionIsEnabled]); - - React.useEffect(() => { - if (isFirstRender.current) { - isFirstRender.current = false; - } - }, []); };