-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[DataGridPremium] Fix valueFormatter
issues when rowGroupingColumnMode="single"
#18967
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
Changes from 4 commits
544d820
42d1edf
1136985
a1b7290
83a6d8a
3280d42
8ac6f7b
0feb796
3f870f6
a5dba6b
ab4a886
f7c69da
75f18d8
ee8ce52
b067218
2d9d78d
4979c6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,8 @@ import { | |
GRID_ROW_GROUPING_SINGLE_GROUPING_FIELD, | ||
} from './gridRowGroupingUtils'; | ||
import { gridRowGroupingSanitizedModelSelector } from './gridRowGroupingSelector'; | ||
import type { GridRowGroupingModel } from './gridRowGroupingInterfaces'; | ||
import type { DataGridPremiumProcessedProps } from '../../../models/dataGridPremiumProps'; | ||
|
||
const GROUPING_COL_DEF_DEFAULT_PROPERTIES: Omit<GridColDef, 'field'> = { | ||
...GRID_STRING_COL_DEF, | ||
|
@@ -110,13 +112,58 @@ const groupedByColValueFormatter: ( | |
return value; | ||
}; | ||
|
||
const getGroupingCriteriaProperties = (groupedByColDef: GridColDef, applyHeaderName: boolean) => { | ||
function getGroupingCriteriaProperties( | ||
groupedByColDef: GridColDef, | ||
rowGroupingColumnMode: 'single', | ||
rowGroupingModel: GridRowGroupingModel, | ||
columnsLookup: GridColumnRawLookup, | ||
): Partial<GridColDef>; | ||
function getGroupingCriteriaProperties( | ||
groupedByColDef: GridColDef, | ||
rowGroupingColumnMode: 'multiple', | ||
): Partial<GridColDef>; | ||
function getGroupingCriteriaProperties( | ||
groupedByColDef: GridColDef, | ||
rowGroupingColumnMode: DataGridPremiumProcessedProps['rowGroupingColumnMode'], | ||
rowGroupingModel: GridRowGroupingModel = [], | ||
columnsLookup: GridColumnRawLookup = {}, | ||
): Partial<GridColDef> { | ||
let valueFormatter: GridColDef['valueFormatter'] | undefined; | ||
|
||
if (rowGroupingColumnMode === 'single' && rowGroupingModel.length > 1) { | ||
// In single column grouping mode, the `valueFormatter` of the grouping column uses | ||
// value formatters from original columns for each of the grouping criteria | ||
Comment on lines
+134
to
+135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the only way valueFormatter could work in |
||
const originalColDefs: Map<string, GridColDef> = new Map(); | ||
rowGroupingModel.forEach((field) => { | ||
const colDef = columnsLookup[field]; | ||
originalColDefs.set(field, colDef); | ||
}); | ||
valueFormatter = (value, row, column, apiRef) => { | ||
const rowId = gridRowIdSelector(apiRef, row); | ||
const rowNode = gridRowNodeSelector(apiRef, rowId); | ||
if (rowNode.type === 'group') { | ||
const originalColDef = originalColDefs.get(rowNode.groupingField!)!; | ||
|
||
if (originalColDef.type === 'singleSelect') { | ||
// the default valueFormatter of a singleSelect colDef won't work with the grouping column values | ||
return value; | ||
} | ||
const columnValueFormatter = originalColDef.valueFormatter; | ||
if (typeof columnValueFormatter === 'function') { | ||
return columnValueFormatter(value as never, row, column, apiRef); | ||
} | ||
} | ||
return value; | ||
}; | ||
} else { | ||
valueFormatter = groupedByColDef.valueFormatter | ||
? groupedByColValueFormatter(groupedByColDef) | ||
: undefined; | ||
} | ||
|
||
const properties: Partial<GridColDef> = { | ||
sortable: groupedByColDef.sortable, | ||
filterable: groupedByColDef.filterable, | ||
valueFormatter: groupedByColDef.valueFormatter | ||
? groupedByColValueFormatter(groupedByColDef) | ||
: undefined, | ||
valueFormatter, | ||
valueOptions: isSingleSelectColDef(groupedByColDef) ? groupedByColDef.valueOptions : undefined, | ||
sortComparator: (v1, v2, cellParams1, cellParams2) => { | ||
// We only want to sort the groups of the current grouping criteria | ||
|
@@ -134,12 +181,13 @@ const getGroupingCriteriaProperties = (groupedByColDef: GridColDef, applyHeaderN | |
filterOperators: groupedByColDef.filterOperators, | ||
}; | ||
|
||
const applyHeaderName = !(rowGroupingColumnMode === 'single' && rowGroupingModel.length > 1); | ||
if (applyHeaderName) { | ||
properties.headerName = groupedByColDef.headerName ?? groupedByColDef.field; | ||
} | ||
|
||
return properties; | ||
}; | ||
} | ||
|
||
interface CreateGroupingColDefMonoCriteriaParams { | ||
columnsLookup: GridColumnRawLookup; | ||
|
@@ -253,11 +301,11 @@ export const createGroupingColDefForOneGroupingCriteria = ({ | |
// By default, we apply the sorting / filtering on the groups of this column's grouping criteria based on the properties of `groupedColDef`. | ||
let sourceProperties: Partial<GridColDef>; | ||
if (mainGroupingCriteria && mainGroupingCriteria === groupingCriteria) { | ||
sourceProperties = getGroupingCriteriaProperties(groupedByColDef, true); | ||
sourceProperties = getGroupingCriteriaProperties(groupedByColDef, 'multiple'); | ||
} else if (leafColDef) { | ||
sourceProperties = getLeafProperties(leafColDef); | ||
} else { | ||
sourceProperties = getGroupingCriteriaProperties(groupedByColDef, true); | ||
sourceProperties = getGroupingCriteriaProperties(groupedByColDef, 'multiple'); | ||
} | ||
|
||
// The properties that can't be overridden with `colDefOverride` | ||
|
@@ -281,7 +329,7 @@ interface CreateGroupingColDefSeveralCriteriaParams { | |
/** | ||
* The fields from which we are grouping the rows. | ||
*/ | ||
rowGroupingModel: string[]; | ||
rowGroupingModel: GridRowGroupingModel; | ||
/** | ||
* The col def properties the user wants to override. | ||
* This value comes `prop.groupingColDef`. | ||
|
@@ -379,13 +427,20 @@ export const createGroupingColDefForAllGroupingCriteria = ({ | |
// By default, we apply the sorting / filtering on the groups of the top level grouping criteria based on the properties of `columnsLookup[orderedGroupedByFields[0]]`. | ||
let sourceProperties: Partial<GridColDef>; | ||
if (mainGroupingCriteria && rowGroupingModel.includes(mainGroupingCriteria)) { | ||
sourceProperties = getGroupingCriteriaProperties(columnsLookup[mainGroupingCriteria], true); | ||
sourceProperties = getGroupingCriteriaProperties( | ||
columnsLookup[mainGroupingCriteria], | ||
'single', | ||
rowGroupingModel, | ||
columnsLookup, | ||
); | ||
} else if (leafColDef) { | ||
sourceProperties = getLeafProperties(leafColDef); | ||
} else { | ||
sourceProperties = getGroupingCriteriaProperties( | ||
columnsLookup[rowGroupingModel[0]], | ||
rowGroupingModel.length === 1, | ||
'single', | ||
rowGroupingModel, | ||
columnsLookup, | ||
); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we update the docs at https://mui.com/x/react-data-grid/row-grouping/#using-groupingvaluegetter-for-complex-grouping-value to state that
Date
is also allowed as a grouping value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Also updated the demo for
groupingValueGetter
, because nowgroupingValueGetter
is not necessary if you havevalueGetter
, unless you want to use a different value for grouping than the one in the cell.https://deploy-preview-18967--material-ui-x.netlify.app/x/react-data-grid/row-grouping/#using-groupingvaluegetter-for-complex-grouping-value
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that is a nice addition