From a63885dde604fd97501095d6edefe8b9df40f7d5 Mon Sep 17 00:00:00 2001 From: Andrew Cherniavskyi Date: Fri, 27 Dec 2024 20:26:27 +0100 Subject: [PATCH 1/3] update test --- .../src/tests/rowGrouping.DataGridPremium.test.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx index 6a0979cff342c..8014e93a6948a 100644 --- a/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx @@ -1605,7 +1605,7 @@ describe(' - 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( - 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']); }); From 799faa8408f9a26030665d826526adfd7faec374 Mon Sep 17 00:00:00 2001 From: Andrew Cherniavskyi Date: Fri, 27 Dec 2024 20:27:00 +0100 Subject: [PATCH 2/3] extract getValue function --- .../src/hooks/features/rows/gridRowsUtils.ts | 17 +++++++++++++++++ .../src/hooks/features/rows/useGridParamsApi.ts | 12 ++---------- packages/x-data-grid/src/internals/index.ts | 6 +++++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts b/packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts index a56b68bddcdba..33ce39dd1cae9 100644 --- a/packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts +++ b/packages/x-data-grid/src/hooks/features/rows/gridRowsUtils.ts @@ -2,6 +2,7 @@ import * as React from 'react'; import { GridAutoGeneratedGroupNode, GridAutoGeneratedPinnedRowNode, + GridColDef, GridFooterNode, GridGroupNode, GridRowId, @@ -11,6 +12,7 @@ import { GridRowTreeConfig, GridSkeletonRowNode, GridTreeNode, + GridValidRowModel, } from '../../../models'; import { DataGridProcessedProps } from '../../../models/props/DataGridProps'; import { GridApiCommunity, GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity'; @@ -74,6 +76,21 @@ export const getRowIdFromRowModel = ( return id; }; +export const getRowValue = ( + row: GridValidRowModel, + colDef: GridColDef, + apiRef: React.MutableRefObject, +) => { + 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, diff --git a/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts b/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts index 0b37235679826..918360d1733f3 100644 --- a/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts +++ b/packages/x-data-grid/src/hooks/features/rows/useGridParamsApi.ts @@ -13,6 +13,7 @@ import { useGridApiMethod } from '../../utils/useGridApiMethod'; import { gridFocusCellSelector, gridTabIndexCellSelector } from '../focus/gridFocusStateSelector'; import { DataGridProcessedProps } from '../../../models/props/DataGridProps'; import { gridListColumnSelector } from '../listView/gridListViewSelectors'; +import { getRowValue as getRowValueFn } from './gridRowsUtils'; export class MissingRowIdError extends Error {} @@ -118,16 +119,7 @@ export function useGridParamsApi( ); const getRowValue = React.useCallback( - (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], ); diff --git a/packages/x-data-grid/src/internals/index.ts b/packages/x-data-grid/src/internals/index.ts index 60848709e1927..0a96160632332 100644 --- a/packages/x-data-grid/src/internals/index.ts +++ b/packages/x-data-grid/src/internals/index.ts @@ -101,7 +101,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, From f7bee1109e270df47b629446c247918e7b02c42b Mon Sep 17 00:00:00 2001 From: Andrew Cherniavskyi Date: Fri, 27 Dec 2024 20:27:22 +0100 Subject: [PATCH 3/3] use valueGetter for grouping keys --- .../src/hooks/features/rowGrouping/gridRowGroupingUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/x-data-grid-premium/src/hooks/features/rowGrouping/gridRowGroupingUtils.ts b/packages/x-data-grid-premium/src/hooks/features/rowGrouping/gridRowGroupingUtils.ts index 353b51936bd4a..95621030ed52c 100644 --- a/packages/x-data-grid-premium/src/hooks/features/rowGrouping/gridRowGroupingUtils.ts +++ b/packages/x-data-grid-premium/src/hooks/features/rowGrouping/gridRowGroupingUtils.ts @@ -20,6 +20,7 @@ import { getRowGroupingCriteriaFromGroupingField, isGroupingColumn, GridStrategyGroup, + getRowValue, } from '@mui/x-data-grid-pro/internals'; import { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps'; import { @@ -230,7 +231,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 {