Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataGridPro] Fix selection propagation issue on initialization #15461

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isFirstRender is not used anymore. it can be cleared from the file

with that
how does this affect #14909
since that was the only change added to address that issue

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, cleaned up isFirstRender.

how does this affect #14909

#15184 removed the root cause for #14909, isFirstRender ref was not needed anymore for that fix.

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;
}
}, []);
};