Skip to content

Commit

Permalink
[DataGrid] Fix onRowSelectionModelChange firing unnecessarily on in…
Browse files Browse the repository at this point in the history
…itial render (mui#14909)
  • Loading branch information
MBilalShafi authored Oct 11, 2024
1 parent 185dd93 commit ef0d5c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ describe('<DataGridPro /> - Row selection', () => {
expect(getCell(1, 0).querySelector('input')!).to.have.attr('data-indeterminate', 'true');
});

// Context: https://github.com/mui/mui-x/issues/14859
it('should not throw when controlling a selection model', () => {
function TestDataGrid() {
const [rowSelectionModel, setRowSelectionModel] = React.useState<GridRowSelectionModel>([]);
return (
<TreeDataGrid
rowSelectionModel={rowSelectionModel}
onRowSelectionModelChange={setRowSelectionModel}
/>
);
}
expect(() => {
render(<TestDataGrid />);
}).not.to.throw();
});

describe('prop: checkboxSelectionVisibleOnly = false', () => {
it('should select all rows of all pages if no row is selected', () => {
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,12 @@ 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 filteredRowsLookup = gridFilteredRowsLookupSelector(apiRef);

Expand Down Expand Up @@ -757,4 +761,10 @@ export const useGridRowSelection = (
React.useEffect(() => {
runIfRowSelectionIsEnabled(removeOutdatedSelection);
}, [removeOutdatedSelection, runIfRowSelectionIsEnabled]);

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

0 comments on commit ef0d5c7

Please sign in to comment.