Skip to content

Commit

Permalink
[DataGridPro] Fix selection propagation issue on initialization (mui#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi authored and LukasTy committed Dec 19, 2024
1 parent 6d9cf2f commit 18f43cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -68,6 +69,19 @@ describe('<DataGridPremium /> - Row selection', () => {
);
}

it('should auto select parents when controlling row selection model', () => {
const onRowSelectionModelChange = spy();
render(
<Test rowSelectionModel={[3, 4]} onRowSelectionModelChange={onRowSelectionModelChange} />,
);

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(<Test />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,18 @@ describe('<DataGridPro /> - Row selection', () => {
);
}

it('should not auto select parents when controlling row selection model', () => {
const onRowSelectionModelChange = spy();
render(
<SelectionPropagationGrid
rowSelectionModel={[2, 3, 4, 5, 6, 7]}
onRowSelectionModelChange={onRowSelectionModelChange}
/>,
);

expect(onRowSelectionModelChange.callCount).to.equal(0);
});

it('should select the parent only when selecting it', () => {
render(<SelectionPropagationGrid />);

Expand Down Expand Up @@ -695,6 +707,19 @@ describe('<DataGridPro /> - Row selection', () => {
);
}

it('should auto select parents when controlling row selection model', () => {
const onRowSelectionModelChange = spy();
render(
<SelectionPropagationGrid
rowSelectionModel={[2, 3, 4, 5, 6, 7]}
onRowSelectionModelChange={onRowSelectionModelChange}
/>,
);

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(<SelectionPropagationGrid />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -786,10 +782,4 @@ export const useGridRowSelection = (
React.useEffect(() => {
runIfRowSelectionIsEnabled(removeOutdatedSelection);
}, [removeOutdatedSelection, runIfRowSelectionIsEnabled]);

React.useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
}
}, []);
};

0 comments on commit 18f43cd

Please sign in to comment.