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

[DataGridPremium] Make row grouping work with negative filtering #16004

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Expand Up @@ -55,6 +55,7 @@ interface FilterRowTreeFromTreeDataParams {
isRowMatchingFilters: GridAggregatedFilterItemApplier | null;
filterModel: GridFilterModel;
apiRef: React.MutableRefObject<GridPrivateApiPremium>;
rowGroupingColumnMode: 'single' | 'multiple';
}

/**
Expand All @@ -79,11 +80,36 @@ const shouldApplyFilterItemOnGroup = (columnField: string, node: GridGroupNode)
export const filterRowTreeFromGroupingColumns = (
params: FilterRowTreeFromTreeDataParams,
): Omit<GridFilterState, 'filterModel'> => {
const { apiRef, rowTree, isRowMatchingFilters, filterModel } = params;
const { apiRef, rowTree, isRowMatchingFilters, filterModel, rowGroupingColumnMode } = params;
const filteredRowsLookup: Record<GridRowId, boolean> = {};
const filteredChildrenCountLookup: Record<GridRowId, number> = {};
const filteredDescendantCountLookup: Record<GridRowId, number> = {};
const filterCache = {};
const isFilterItemAppliedOnGroup = (node: GridTreeNode): boolean => {
if (node.type === 'group' && node.isAutoGenerated) {
const groupFilterItems: string[] = [];
const leafFilterItems: string[] = [];
filterModel.items.forEach((item) => {
if (isGroupingColumn(item.field)) {
groupFilterItems.push(item.field);
} else {
leafFilterItems.push(item.field);
}
});
const groupingField =
rowGroupingColumnMode === 'multiple'
? getRowGroupingFieldFromGroupingCriteria(node.groupingField!)
: GRID_ROW_GROUPING_SINGLE_GROUPING_FIELD;

const shouldApplyFilterOnGroupingField =
groupingField === groupFilterItems[groupFilterItems.length - 1];

if (shouldApplyFilterOnGroupingField && leafFilterItems.length === 0) {
return true;
}
}
return false;
};

const filterTreeNode = (
node: GridTreeNode,
Expand All @@ -102,15 +128,23 @@ export const filterRowTreeFromGroupingColumns = (
node.type === 'group' && node.isAutoGenerated
? (columnField: string) => shouldApplyFilterItemOnGroup(columnField, node)
: undefined;

const row = apiRef.current.getRow(node.id);
isRowMatchingFilters(row, shouldApplyItem, filterResults);
const allResults = [...ancestorsResults, filterResults];
isPassingFiltering = passFilterLogic(
allResults.map((result) => result.passingFilterItems),
allResults.map((result) => result.passingQuickFilterValues),
filterModel,
params.apiRef,
filterCache,
);
} else {
isPassingFiltering = true;
}

let filteredChildrenCount = 0;
let filteredDescendantCount = 0;

if (node.type === 'group') {
node.children.forEach((childId) => {
const childNode = rowTree[childId];
Expand All @@ -126,20 +160,10 @@ export const filterRowTreeFromGroupingColumns = (
});
}

if (isPassingFiltering === false) {
if (node.type === 'group') {
// If node has children - it's passing if at least one child passes filters
isPassingFiltering = filteredDescendantCount > 0;
} else {
const allResults = [...ancestorsResults, filterResults];
isPassingFiltering = passFilterLogic(
allResults.map((result) => result.passingFilterItems),
allResults.map((result) => result.passingQuickFilterValues),
filterModel,
params.apiRef,
filterCache,
);
}
const filterItemOnlyAppliedOnGroup = isFilterItemAppliedOnGroup(node);
if (node.type === 'group' && !filterItemOnlyAppliedOnGroup) {
// If node has children - it's passing if at least one child passes filters
isPassingFiltering = filteredDescendantCount > 0;
}

filteredRowsLookup[node.id] = isPassingFiltering;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ export const useGridRowGroupingPreProcessors = (
isRowMatchingFilters: params.isRowMatchingFilters,
filterModel: params.filterModel,
apiRef,
rowGroupingColumnMode: props.rowGroupingColumnMode,
});
},
[apiRef],
[apiRef, props.rowGroupingColumnMode],
);

const sortRows = React.useCallback<GridStrategyProcessor<'sorting'>>(
Expand Down
Loading