Skip to content

Commit

Permalink
[DataGrid] Improve row selection propagation trigger (mui#15184)
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi committed Nov 5, 2024
1 parent 4814c2f commit 410bfa5
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { GridSignature, useGridApiEventHandler } from '../../utils/useGridApiEve
import { useGridApiMethod } from '../../utils/useGridApiMethod';
import { useGridLogger } from '../../utils/useGridLogger';
import { useGridSelector } from '../../utils/useGridSelector';
import { gridRowMaximumTreeDepthSelector, gridRowTreeSelector } from '../rows/gridRowsSelector';
import {
gridRowsLookupSelector,
gridRowMaximumTreeDepthSelector,
gridRowTreeSelector,
} from '../rows/gridRowsSelector';
import {
gridRowSelectionStateSelector,
selectedGridRowsSelector,
Expand Down Expand Up @@ -81,6 +85,7 @@ export const useGridRowSelection = (
| 'checkboxSelectionVisibleOnly'
| 'pagination'
| 'paginationMode'
| 'filterMode'
| 'classes'
| 'keepNonExistentRowsSelected'
| 'rowSelection'
Expand Down Expand Up @@ -332,6 +337,13 @@ export const useGridRowSelection = (
} else {
newSelection = new Set();
}
const currentLookup = selectedIdsLookupSelector(apiRef);
if (
newSelection.length === Object.keys(currentLookup).length &&
newSelection.every((id) => currentLookup[id] === id)
) {
return;
}
} else {
newSelection = new Set(Object.values(selectedIdsLookupSelector(apiRef)));
const addRow = (rowId: GridRowId) => {
Expand Down Expand Up @@ -445,14 +457,22 @@ export const useGridRowSelection = (
return;
}
const currentSelection = gridRowSelectionStateSelector(apiRef.current.state);
const rowsLookup = gridRowsLookupSelector(apiRef);
const filteredRowsLookup = gridFilteredRowsLookupSelector(apiRef);

// We clone the existing object to avoid mutating the same object returned by the selector to others part of the project
const selectionLookup = { ...selectedIdsLookupSelector(apiRef) };

const isNonExistent = (id: GridRowId) => {
if (props.filterMode === 'server') {
return !rowsLookup[id];
}
return filteredRowsLookup[id] !== true;
};

let hasChanged = false;
currentSelection.forEach((id: GridRowId) => {
if (filteredRowsLookup[id] !== true) {
if (isNonExistent(id)) {
if (props.keepNonExistentRowsSelected) {
return;
}
Expand All @@ -479,9 +499,18 @@ export const useGridRowSelection = (
}
});

if (hasChanged || (isNestedData && !sortModelUpdated)) {
// For nested data, on row tree updation (filtering, adding rows, etc.) when the selection is
// not empty, we need to re-run scanning of the tree to propagate the selection changes
// Example: A parent whose de-selected children are filtered out should now be selected
const shouldReapplyPropagation =
isNestedData &&
!sortModelUpdated &&
props.rowSelectionPropagation?.parents &&
Object.keys(selectionLookup).length > 0;

if (hasChanged || shouldReapplyPropagation) {
const newSelection = Object.values(selectionLookup);
if (isNestedData) {
if (shouldReapplyPropagation) {
apiRef.current.selectRows(newSelection, true, true);
} else {
apiRef.current.setRowSelectionModel(newSelection);
Expand All @@ -493,6 +522,7 @@ export const useGridRowSelection = (
isNestedData,
props.rowSelectionPropagation?.parents,
props.keepNonExistentRowsSelected,
props.filterMode,
tree,
],
);
Expand Down

0 comments on commit 410bfa5

Please sign in to comment.