Skip to content

Commit

Permalink
[DataGridPremium] Use valueGetter to get row group keys (#16016)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherniavskii authored Feb 20, 2025
1 parent 346e024 commit 4acc7e2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getRowGroupingCriteriaFromGroupingField,
isGroupingColumn,
GridStrategyGroup,
getRowValue,
} from '@mui/x-data-grid-pro/internals';
import { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps';
import {
Expand Down Expand Up @@ -231,7 +232,7 @@ export const getCellGroupingCriteria = ({
if (groupingRule.groupingValueGetter) {
key = groupingRule.groupingValueGetter(row[groupingRule.field] as never, row, colDef, apiRef);
} else {
key = row[groupingRule.field] as GridKeyValue | null | undefined;
key = getRowValue(row, colDef, apiRef) as GridKeyValue | null | undefined;
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ describe('<DataGridPremium /> - Row grouping', () => {
expect(getColumnValues(1)).to.deep.equal(['', '0', '3', '', '1', '4', '', '2']);
});

it('should not use valueGetter to group the rows when defined', () => {
it('should use valueGetter to group the rows when defined', () => {
render(
<Test
columns={[
Expand All @@ -1632,7 +1632,15 @@ describe('<DataGridPremium /> - Row grouping', () => {
defaultGroupingExpansionDepth={-1}
/>,
);
expect(getColumnValues(0)).to.deep.equal(['Cat A (3)', '', '', '', 'Cat B (2)', '', '']);
expect(getColumnValues(0)).to.deep.equal([
'value Cat A (3)',
'',
'',
'',
'value Cat B (2)',
'',
'',
]);
expect(getColumnValues(1)).to.deep.equal(['', '0', '1', '2', '', '3', '4']);
});

Expand Down
17 changes: 17 additions & 0 deletions packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RefObject } from '@mui/x-internals/types';
import {
GridAutoGeneratedGroupNode,
GridAutoGeneratedPinnedRowNode,
GridColDef,
GridFooterNode,
GridGroupNode,
GridRowId,
Expand All @@ -11,6 +12,7 @@ import {
GridRowTreeConfig,
GridSkeletonRowNode,
GridTreeNode,
GridValidRowModel,
} from '../../../models';
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { GridApiCommunity, GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
Expand Down Expand Up @@ -74,6 +76,21 @@ export const getRowIdFromRowModel = (
return id;
};

export const getRowValue = (
row: GridValidRowModel,
colDef: GridColDef,
apiRef: RefObject<GridApiCommunity>,
) => {
const field = colDef.field;

if (!colDef || !colDef.valueGetter) {
return row[field];
}

const value = row[colDef.field];
return colDef.valueGetter(value as never, row, colDef, apiRef);
};

export const createRowsInternalCache = ({
rows,
getRowId,
Expand Down
12 changes: 2 additions & 10 deletions packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { gridFocusCellSelector, gridTabIndexCellSelector } from '../focus/gridFo
import { DataGridProcessedProps } from '../../../models/props/DataGridProps';
import { gridListColumnSelector } from '../listView/gridListViewSelectors';
import { gridRowNodeSelector } from './gridRowsSelector';
import { getRowValue as getRowValueFn } from './gridRowsUtils';

export class MissingRowIdError extends Error {}

Expand Down Expand Up @@ -132,16 +133,7 @@ export function useGridParamsApi(
);

const getRowValue = React.useCallback<GridParamsApi['getRowValue']>(
(row, colDef) => {
const field = colDef.field;

if (!colDef || !colDef.valueGetter) {
return row[field];
}

const value = row[colDef.field];
return colDef.valueGetter(value as never, row, colDef, apiRef);
},
(row, colDef) => getRowValueFn(row, colDef, apiRef),
[apiRef],
);

Expand Down
6 changes: 5 additions & 1 deletion packages/x-data-grid/src/internals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ export type {
export { getTreeNodeDescendants, buildRootGroup } from '../hooks/features/rows/gridRowsUtils';
export { useGridRowsMeta, rowsMetaStateInitializer } from '../hooks/features/rows/useGridRowsMeta';
export { useGridParamsApi } from '../hooks/features/rows/useGridParamsApi';
export { getRowIdFromRowModel, GRID_ID_AUTOGENERATED } from '../hooks/features/rows/gridRowsUtils';
export {
getRowIdFromRowModel,
GRID_ID_AUTOGENERATED,
getRowValue,
} from '../hooks/features/rows/gridRowsUtils';
export {
gridAdditionalRowGroupsSelector,
gridPinnedRowsSelector,
Expand Down

0 comments on commit 4acc7e2

Please sign in to comment.