-
-
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
[DataGridPremium] Fix valueFormatter
issues when rowGroupingColumnMode="single"
#18967
Conversation
Deploy preview: https://deploy-preview-18967--material-ui-x.netlify.app/ Updated pages: Bundle size report
|
// In single column grouping mode, the `valueFormatter` of the grouping column uses | ||
// value formatters from original columns for each of the grouping criteria |
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.
I think this is the only way valueFormatter could work in rowGroupingColumnMode="single"
.
This still fails for this reproduction code import * as React from 'react';
import { DataGridPremium, GridColDef, GridRowGroupingModel } from '@mui/x-data-grid-premium';
const columns: GridColDef[] = [
{ field: 'id', headerName: 'ID' },
{
field: 'transactionDate',
type: 'date',
headerName: 'Transaction Date',
valueGetter: (value) => new Date(value),
groupingValueGetter: (value) => value,
},
{ field: 'ticker', headerName: 'Ticker' },
];
const rowGroupingModel: GridRowGroupingModel = ['transactionDate'];
export default function GridGetPivotDerivedColumns() {
return (
<div style={{ width: '100%' }}>
<div style={{ height: 300, width: '100%' }}>
<DataGridPremium
rows={rows}
columns={columns}
initialState={{
rowGrouping: {
model: rowGroupingModel,
},
}}
/>
</div>
</div>
);
}
const rows = [
{
id: 1,
transactionDate: '2024-05-15',
ticker: 'AAPL',
price: 192.45,
volume: 5500,
type: 'stock',
},
]; IIUC the problem is that we are using We could return here WDYT? |
@arminmeh I'm not sure I understand your proposal, can you provide more details? Or feel free to push to this branch so we can give it a try |
I've made an example here it is still missing few pieces, but it covers the discussion here I have updated the
for easier testing. Running this on With the changes from the linked PR, the value from The problem is that, even though we say that We could either postpone this update for v9 or have another prop like IMO the updated behavior is better, because you use the same props for formatting leafs and group rows |
@arminmeh It still throws an error if you change the grouping model to this: const rowGroupingModel: GridRowGroupingModel = ['ticker', 'transactionDate']; I think we need this change to solve it.
How about adding optional |
const rowId = gridRowIdSelector(apiRef, row); | ||
const rowNode = gridRowNodeSelector(apiRef, rowId); | ||
if (rowNode.type === 'group') { | ||
const originalColDef = originalColDefs.get(rowNode.groupingField!)!; |
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.
Could this just be
columnsLookup[rowNode.groupingField]
so that you avoid building originalColDefs
?
Let's merge this one then to fix the user problem. |
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.
Still failing for
#18967 (comment)
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.
Still failing for
#18967 (comment)
field: 'composer', | ||
headerName: 'Composer', | ||
valueGetter: (value) => value.name, | ||
groupingValueGetter: (value) => value.name, |
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.
@arminmeh
I think I fixed all use cases, but now the valueGetter
is being called first, and the result is passed to groupingValueGetter
(if provided). What do you think about this approach?
This might break for someone, but it's kindof a bugfix.
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.
It could be confusing, since value
can either be a value from the row
or the return value
from the column's valueGetter
.
As mentioned before, the problem is that we are using groupingValueGetter()
to generate the group key and the column value.
Since the requirement is that the key is string | number | null | undefined
, it can't be a Date
, which is a requirement for the date column.
So, we could update the default formatter
https://github.com/mui/mui-x/blob/v8.10.2/packages/x-data-grid/src/colDef/gridDateColDef.ts#L30
not to check for Date
if the row is autogenerated.
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.
not to check for Date if the row is autogenerated
Hmm, I'll try that
headerName: 'Transaction Date', | ||
width: 140, | ||
valueGetter: (value) => new Date(value), | ||
groupingValueGetter: (value) => 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.
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 now groupingValueGetter
is not necessary if you have valueGetter
, 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
import { renderEditDateCell } from '../components/cell/GridEditDateCell'; | ||
import { gridRowIdSelector } from '../hooks/core/gridPropsSelectors'; | ||
import { isAutogeneratedRow } from '../hooks/features/rows/gridRowsUtils'; | ||
import { isGroupingColumn } from '../internals/utils/gridRowGroupingUtils'; |
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.
I had to remove this check because the formatter gets the original column definition.
Checking for autogenerated row should be enough.
Continuation of #18916 as it only fixed subset of the issues
Fixes #18333
Preview: https://deploy-preview-18967--material-ui-x.netlify.app/x/react-data-grid/pivoting/#derived-columns-in-pivot-mode