Skip to content

Commit

Permalink
Use enums
Browse files Browse the repository at this point in the history
  • Loading branch information
MBilalShafi committed Oct 18, 2024
1 parent 6ee149a commit ba0ab94
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { GridGroupingColumnLeafCell } from '../../../components/GridGroupingColu
import {
getRowGroupingFieldFromGroupingCriteria,
GRID_ROW_GROUPING_SINGLE_GROUPING_FIELD,
ROW_GROUPING_STRATEGY_DEFAULT,
ROW_GROUPING_STRATEGY_DATA_SOURCE,
RowGroupingStrategy,
} from './gridRowGroupingUtils';
import { gridRowGroupingSanitizedModelSelector } from './gridRowGroupingSelector';

Expand Down Expand Up @@ -138,7 +137,7 @@ interface CreateGroupingColDefMonoCriteriaParams {
* This value comes `prop.groupingColDef`.
*/
colDefOverride: GridGroupingColDefOverride | null | undefined;
strategy?: typeof ROW_GROUPING_STRATEGY_DEFAULT | typeof ROW_GROUPING_STRATEGY_DATA_SOURCE;
strategy?: RowGroupingStrategy;
}

/**
Expand All @@ -149,14 +148,14 @@ export const createGroupingColDefForOneGroupingCriteria = ({
groupedByColDef,
groupingCriteria,
colDefOverride,
strategy = ROW_GROUPING_STRATEGY_DEFAULT,
strategy = RowGroupingStrategy.Default,
}: CreateGroupingColDefMonoCriteriaParams): GridColDef => {
const { leafField, mainGroupingCriteria, hideDescendantCount, ...colDefOverrideProperties } =
colDefOverride ?? {};
const leafColDef = leafField ? columnsLookup[leafField] : null;

const CriteriaCell =
strategy === ROW_GROUPING_STRATEGY_DEFAULT
strategy === RowGroupingStrategy.Default
? GridGroupingCriteriaCell
: GridDataSourceGroupingCriteriaCell;

Expand Down Expand Up @@ -269,7 +268,7 @@ interface CreateGroupingColDefSeveralCriteriaParams {
* This value comes `prop.groupingColDef`.
*/
colDefOverride: GridGroupingColDefOverride | null | undefined;
strategy?: typeof ROW_GROUPING_STRATEGY_DEFAULT | typeof ROW_GROUPING_STRATEGY_DATA_SOURCE;
strategy?: RowGroupingStrategy;
}

/**
Expand All @@ -280,14 +279,14 @@ export const createGroupingColDefForAllGroupingCriteria = ({
columnsLookup,
rowGroupingModel,
colDefOverride,
strategy = ROW_GROUPING_STRATEGY_DEFAULT,
strategy = RowGroupingStrategy.Default,
}: CreateGroupingColDefSeveralCriteriaParams): GridColDef => {
const { leafField, mainGroupingCriteria, hideDescendantCount, ...colDefOverrideProperties } =
colDefOverride ?? {};
const leafColDef = leafField ? columnsLookup[leafField] : null;

const CriteriaCell =
strategy === ROW_GROUPING_STRATEGY_DEFAULT
strategy === RowGroupingStrategy.Default
? GridGroupingCriteriaCell
: GridDataSourceGroupingCriteriaCell;

Expand Down Expand Up @@ -374,7 +373,7 @@ export const createGroupingColDefForAllGroupingCriteria = ({
// The properties that can't be overridden with `colDefOverride`
const forcedProperties: Pick<GridColDef, 'field' | 'editable'> = {
field: GRID_ROW_GROUPING_SINGLE_GROUPING_FIELD,
...(strategy === ROW_GROUPING_STRATEGY_DEFAULT
...(strategy === RowGroupingStrategy.Default
? GROUPING_COL_DEF_FORCED_PROPERTIES_DEFAULT
: GROUPING_COL_DEF_FORCED_PROPERTIES_DATA_SOURCE),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import { GridPrivateApiPremium } from '../../../models/gridApiPremium';

export const GRID_ROW_GROUPING_SINGLE_GROUPING_FIELD = '__row_group_by_columns_group__';

export const ROW_GROUPING_STRATEGY_DEFAULT = 'grouping-columns';
export const ROW_GROUPING_STRATEGY_DATA_SOURCE = 'grouping-columns-data-source';
export enum RowGroupingStrategy {
Default = 'grouping-columns',
DataSource = 'grouping-columns-data-source',
}

export const getRowGroupingFieldFromGroupingCriteria = (groupingCriteria: string | null) => {
if (groupingCriteria === null) {
Expand Down Expand Up @@ -180,11 +182,11 @@ export const filterRowTreeFromGroupingColumns = (
export const getColDefOverrides = (
groupingColDefProp: DataGridPremiumProcessedProps['groupingColDef'],
fields: string[],
strategy?: typeof ROW_GROUPING_STRATEGY_DEFAULT | typeof ROW_GROUPING_STRATEGY_DATA_SOURCE,
strategy?: RowGroupingStrategy,
) => {
if (typeof groupingColDefProp === 'function') {
return groupingColDefProp({
groupingName: strategy ?? ROW_GROUPING_STRATEGY_DEFAULT,
groupingName: strategy ?? RowGroupingStrategy.Default,
fields,
});
}
Expand Down Expand Up @@ -214,7 +216,7 @@ export const setStrategyAvailability = (
};
}

const strategy = dataSource ? ROW_GROUPING_STRATEGY_DATA_SOURCE : ROW_GROUPING_STRATEGY_DEFAULT;
const strategy = dataSource ? RowGroupingStrategy.DataSource : RowGroupingStrategy.Default;

privateApiRef.current.setStrategyAvailability('rowTree', strategy, isAvailable);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
GridRowsPartialUpdates,
} from '@mui/x-data-grid-pro/internals';
import { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps';
import { ROW_GROUPING_STRATEGY_DATA_SOURCE, getGroupingRules } from './gridRowGroupingUtils';
import { getGroupingRules, RowGroupingStrategy } from './gridRowGroupingUtils';
import { GridPrivateApiPremium } from '../../../models/gridApiPremium';
import { gridRowGroupingSanitizedModelSelector } from './gridRowGroupingSelector';

Expand Down Expand Up @@ -75,7 +75,7 @@ export const useGridDataSourceRowGroupingPreProcessors = (
nodes: params.updates.rows.map(getRowTreeBuilderNode),
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: ROW_GROUPING_STRATEGY_DATA_SOURCE,
groupingName: RowGroupingStrategy.DataSource,
});
}

Expand All @@ -94,7 +94,7 @@ export const useGridDataSourceRowGroupingPreProcessors = (
previousTreeDepth: params.previousTreeDepths!,
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: ROW_GROUPING_STRATEGY_DATA_SOURCE,
groupingName: RowGroupingStrategy.DataSource,
});
},
[
Expand All @@ -119,20 +119,15 @@ export const useGridDataSourceRowGroupingPreProcessors = (

useGridRegisterStrategyProcessor(
apiRef,
ROW_GROUPING_STRATEGY_DATA_SOURCE,
RowGroupingStrategy.DataSource,
'rowTreeCreation',
createRowTreeForRowGrouping,
);
useGridRegisterStrategyProcessor(apiRef, RowGroupingStrategy.DataSource, 'filtering', filterRows);
useGridRegisterStrategyProcessor(apiRef, RowGroupingStrategy.DataSource, 'sorting', sortRows);
useGridRegisterStrategyProcessor(
apiRef,
ROW_GROUPING_STRATEGY_DATA_SOURCE,
'filtering',
filterRows,
);
useGridRegisterStrategyProcessor(apiRef, ROW_GROUPING_STRATEGY_DATA_SOURCE, 'sorting', sortRows);
useGridRegisterStrategyProcessor(
apiRef,
ROW_GROUPING_STRATEGY_DATA_SOURCE,
RowGroupingStrategy.DataSource,
'visibleRowsLookupCreation',
getVisibleRowsLookup,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps';
import {
getRowGroupingFieldFromGroupingCriteria,
ROW_GROUPING_STRATEGY_DEFAULT,
RowGroupingStrategy,
isGroupingColumn,
mergeStateWithRowGroupingModel,
setStrategyAvailability,
Expand Down Expand Up @@ -275,7 +275,7 @@ export const useGridRowGrouping = (

// Refresh the row tree creation strategy processing
// TODO: Add a clean way to re-run a strategy processing without publishing a private event
if (apiRef.current.getActiveStrategy('rowTree') === ROW_GROUPING_STRATEGY_DEFAULT) {
if (apiRef.current.getActiveStrategy('rowTree') === RowGroupingStrategy.Default) {
apiRef.current.publishEvent('activeStrategyProcessorChange', 'rowTreeCreation');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ import {
import {
filterRowTreeFromGroupingColumns,
getColDefOverrides,
ROW_GROUPING_STRATEGY_DEFAULT,
ROW_GROUPING_STRATEGY_DATA_SOURCE,
RowGroupingStrategy,
isGroupingColumn,
setStrategyAvailability,
getCellGroupingCriteria,
Expand All @@ -59,8 +58,8 @@ export const useGridRowGroupingPreProcessors = (
}

const strategy = props.unstable_dataSource
? ROW_GROUPING_STRATEGY_DATA_SOURCE
: ROW_GROUPING_STRATEGY_DEFAULT;
? RowGroupingStrategy.DataSource
: RowGroupingStrategy.Default;

const groupingColDefProp = props.groupingColDef;

Expand Down Expand Up @@ -191,7 +190,7 @@ export const useGridRowGroupingPreProcessors = (
nodes: params.updates.rows.map(getRowTreeBuilderNode),
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: ROW_GROUPING_STRATEGY_DEFAULT,
groupingName: RowGroupingStrategy.Default,
});
}

Expand All @@ -205,7 +204,7 @@ export const useGridRowGroupingPreProcessors = (
previousTreeDepth: params.previousTreeDepths!,
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: ROW_GROUPING_STRATEGY_DEFAULT,
groupingName: RowGroupingStrategy.Default,
});
},
[apiRef, props.defaultGroupingExpansionDepth, props.isGroupExpandedByDefault],
Expand Down Expand Up @@ -242,15 +241,15 @@ export const useGridRowGroupingPreProcessors = (
useGridRegisterPipeProcessor(apiRef, 'hydrateColumns', updateGroupingColumn);
useGridRegisterStrategyProcessor(
apiRef,
ROW_GROUPING_STRATEGY_DEFAULT,
RowGroupingStrategy.Default,
'rowTreeCreation',
createRowTreeForRowGrouping,
);
useGridRegisterStrategyProcessor(apiRef, ROW_GROUPING_STRATEGY_DEFAULT, 'filtering', filterRows);
useGridRegisterStrategyProcessor(apiRef, ROW_GROUPING_STRATEGY_DEFAULT, 'sorting', sortRows);
useGridRegisterStrategyProcessor(apiRef, RowGroupingStrategy.Default, 'filtering', filterRows);
useGridRegisterStrategyProcessor(apiRef, RowGroupingStrategy.Default, 'sorting', sortRows);
useGridRegisterStrategyProcessor(
apiRef,
ROW_GROUPING_STRATEGY_DEFAULT,
RowGroupingStrategy.Default,
'visibleRowsLookupCreation',
getVisibleRowsLookup,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ import {
} from '../../../utils/tree/models';
import { updateRowTree } from '../../../utils/tree/updateRowTree';
import { getVisibleRowsLookup } from '../../../utils/tree/utils';

const DATA_SOURCE_TREE_DATA_STRATEGY = 'dataSourceTreeData';
import { TreeDataStrategy } from '../treeData/gridTreeDataUtils';

export const useGridDataSourceTreeDataPreProcessors = (
privateApiRef: React.MutableRefObject<GridPrivateApiPro>,
Expand All @@ -53,7 +52,7 @@ export const useGridDataSourceTreeDataPreProcessors = (
const setStrategyAvailability = React.useCallback(() => {
privateApiRef.current.setStrategyAvailability(
'rowTree',
DATA_SOURCE_TREE_DATA_STRATEGY,
TreeDataStrategy.DataSource,
props.treeData && props.unstable_dataSource ? () => true : () => false,
);
}, [privateApiRef, props.treeData, props.unstable_dataSource]);
Expand All @@ -64,7 +63,7 @@ export const useGridDataSourceTreeDataPreProcessors = (
let colDefOverride: GridGroupingColDefOverride | null | undefined;
if (typeof groupingColDefProp === 'function') {
const params: GridGroupingColDefOverrideParams = {
groupingName: DATA_SOURCE_TREE_DATA_STRATEGY,
groupingName: TreeDataStrategy.DataSource,
fields: [],
};

Expand Down Expand Up @@ -171,7 +170,7 @@ export const useGridDataSourceTreeDataPreProcessors = (
nodes: params.updates.rows.map(getRowTreeBuilderNode),
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: DATA_SOURCE_TREE_DATA_STRATEGY,
groupingName: TreeDataStrategy.DataSource,
onDuplicatePath,
});
}
Expand All @@ -187,7 +186,7 @@ export const useGridDataSourceTreeDataPreProcessors = (
previousTreeDepth: params.previousTreeDepths!,
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: DATA_SOURCE_TREE_DATA_STRATEGY,
groupingName: TreeDataStrategy.DataSource,
});
},
[
Expand All @@ -212,25 +211,20 @@ export const useGridDataSourceTreeDataPreProcessors = (
useGridRegisterPipeProcessor(privateApiRef, 'hydrateColumns', updateGroupingColumn);
useGridRegisterStrategyProcessor(
privateApiRef,
DATA_SOURCE_TREE_DATA_STRATEGY,
TreeDataStrategy.DataSource,
'rowTreeCreation',
createRowTreeForTreeData,
);
useGridRegisterStrategyProcessor(
privateApiRef,
DATA_SOURCE_TREE_DATA_STRATEGY,
TreeDataStrategy.DataSource,
'filtering',
filterRows,
);
useGridRegisterStrategyProcessor(privateApiRef, TreeDataStrategy.DataSource, 'sorting', sortRows);
useGridRegisterStrategyProcessor(
privateApiRef,
DATA_SOURCE_TREE_DATA_STRATEGY,
'sorting',
sortRows,
);
useGridRegisterStrategyProcessor(
privateApiRef,
DATA_SOURCE_TREE_DATA_STRATEGY,
TreeDataStrategy.DataSource,
'visibleRowsLookupCreation',
getVisibleRowsLookup,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ interface FilterRowTreeFromTreeDataParams {
apiRef: React.MutableRefObject<GridPrivateApiPro>;
}

export const TREE_DATA_STRATEGY = 'tree-data';
export enum TreeDataStrategy {
Default = 'tree-data',
DataSource = 'tree-data-source',
}

/**
* A node is visible if one of the following criteria is met:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
GRID_TREE_DATA_GROUPING_COL_DEF_FORCED_PROPERTIES,
} from './gridTreeDataGroupColDef';
import { DataGridProProcessedProps } from '../../../models/dataGridProProps';
import { filterRowTreeFromTreeData, TREE_DATA_STRATEGY } from './gridTreeDataUtils';
import { filterRowTreeFromTreeData, TreeDataStrategy } from './gridTreeDataUtils';
import { GridPrivateApiPro } from '../../../models/gridApiPro';
import {
GridGroupingColDefOverride,
Expand Down Expand Up @@ -52,7 +52,7 @@ export const useGridTreeDataPreProcessors = (
const setStrategyAvailability = React.useCallback(() => {
privateApiRef.current.setStrategyAvailability(
'rowTree',
TREE_DATA_STRATEGY,
TreeDataStrategy.Default,
props.treeData && !props.unstable_dataSource ? () => true : () => false,
);
}, [privateApiRef, props.treeData, props.unstable_dataSource]);
Expand All @@ -63,7 +63,7 @@ export const useGridTreeDataPreProcessors = (
let colDefOverride: GridGroupingColDefOverride | null | undefined;
if (typeof groupingColDefProp === 'function') {
const params: GridGroupingColDefOverrideParams = {
groupingName: TREE_DATA_STRATEGY,
groupingName: TreeDataStrategy.Default,
fields: [],
};

Expand Down Expand Up @@ -158,7 +158,7 @@ export const useGridTreeDataPreProcessors = (
nodes: params.updates.rows.map(getRowTreeBuilderNode),
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: TREE_DATA_STRATEGY,
groupingName: TreeDataStrategy.Default,
onDuplicatePath,
});
}
Expand All @@ -173,7 +173,7 @@ export const useGridTreeDataPreProcessors = (
previousTreeDepth: params.previousTreeDepths!,
defaultGroupingExpansionDepth: props.defaultGroupingExpansionDepth,
isGroupExpandedByDefault: props.isGroupExpandedByDefault,
groupingName: TREE_DATA_STRATEGY,
groupingName: TreeDataStrategy.Default,
});
},
[props.getTreeDataPath, props.defaultGroupingExpansionDepth, props.isGroupExpandedByDefault],
Expand Down Expand Up @@ -211,15 +211,20 @@ export const useGridTreeDataPreProcessors = (
useGridRegisterPipeProcessor(privateApiRef, 'hydrateColumns', updateGroupingColumn);
useGridRegisterStrategyProcessor(
privateApiRef,
TREE_DATA_STRATEGY,
TreeDataStrategy.Default,
'rowTreeCreation',
createRowTreeForTreeData,
);
useGridRegisterStrategyProcessor(privateApiRef, TREE_DATA_STRATEGY, 'filtering', filterRows);
useGridRegisterStrategyProcessor(privateApiRef, TREE_DATA_STRATEGY, 'sorting', sortRows);
useGridRegisterStrategyProcessor(
privateApiRef,
TREE_DATA_STRATEGY,
TreeDataStrategy.Default,
'filtering',
filterRows,
);
useGridRegisterStrategyProcessor(privateApiRef, TreeDataStrategy.Default, 'sorting', sortRows);
useGridRegisterStrategyProcessor(
privateApiRef,
TreeDataStrategy.Default,
'visibleRowsLookupCreation',
getVisibleRowsLookup,
);
Expand Down
Loading

0 comments on commit ba0ab94

Please sign in to comment.