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

[DataGrid] Improve row selection propagation trigger #15184

Merged
merged 1 commit into from
Nov 4, 2024
Merged
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
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 @@ -331,6 +336,13 @@ export const useGridRowSelection = (
} else {
newSelection = [];
}
const currentLookup = selectedIdsLookupSelector(apiRef);
if (
newSelection.length === Object.keys(currentLookup).length &&
newSelection.every((id) => currentLookup[id] === id)
) {
return;
}
} else {
// We clone the existing object to avoid mutating the same object returned by the selector to others part of the project
const selectionLookup = {
Expand Down Expand Up @@ -449,14 +461,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];
Copy link
Member Author

Choose a reason for hiding this comment

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

There is no filteredRowsLookup when filterMode='server'

}
return filteredRowsLookup[id] !== true;
};

let hasChanged = false;
currentSelection.forEach((id: GridRowId) => {
if (filteredRowsLookup[id] !== true) {
if (isNonExistent(id)) {
if (props.keepNonExistentRowsSelected) {
return;
}
Expand All @@ -483,9 +503,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 @@ -497,6 +526,7 @@ export const useGridRowSelection = (
isNestedData,
props.rowSelectionPropagation?.parents,
props.keepNonExistentRowsSelected,
props.filterMode,
tree,
],
);
Expand Down
Loading