Skip to content

Commit c223068

Browse files
authored
Merge branch 'master' into release-charts-premium
2 parents dee7366 + a0cbf5f commit c223068

File tree

9 files changed

+1938
-2267
lines changed

9 files changed

+1938
-2267
lines changed

docs/data/data-grid/pivoting/GridGetPivotDerivedColumns.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ const getPivotDerivedColumns = (column) => {
88
{
99
field: `${field}-year`,
1010
headerName: `${column.headerName} (Year)`,
11-
valueGetter: (value, row) => new Date(row[field]).getFullYear(),
11+
valueGetter: (_, row) => new Date(row[field]).getFullYear(),
1212
},
1313
{
1414
field: `${field}-month`,
1515
headerName: `${column.headerName} (Month)`,
16-
valueGetter: (value, row) =>
17-
`M${`${new Date(row[field]).getMonth() + 1}`.padStart(2, '0')}`,
16+
type: 'number',
17+
valueGetter: (_, row) => new Date(row[field]).getMonth(),
18+
valueFormatter: (month) =>
19+
new Date(0, month).toLocaleString(undefined, { month: 'long' }),
1820
},
1921
];
2022
}
@@ -71,7 +73,7 @@ export default function GridGetPivotDerivedColumns() {
7173
getPivotDerivedColumns={getPivotDerivedColumns}
7274
initialState={{
7375
pivoting: {
74-
enabled: false,
76+
enabled: true,
7577
model: pivotModel,
7678
},
7779
sidebar: {

docs/data/data-grid/pivoting/GridGetPivotDerivedColumns.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ const getPivotDerivedColumns: DataGridPremiumProps['getPivotDerivedColumns'] = (
1616
{
1717
field: `${field}-year`,
1818
headerName: `${column.headerName} (Year)`,
19-
valueGetter: (value, row) => new Date(row[field]).getFullYear(),
19+
valueGetter: (_, row) => new Date(row[field]).getFullYear(),
2020
},
2121

2222
{
2323
field: `${field}-month`,
2424
headerName: `${column.headerName} (Month)`,
25-
valueGetter: (value, row) =>
26-
`M${`${new Date(row[field]).getMonth() + 1}`.padStart(2, '0')}`,
25+
type: 'number',
26+
valueGetter: (_, row) => new Date(row[field]).getMonth(),
27+
valueFormatter: (month) =>
28+
new Date(0, month).toLocaleString(undefined, { month: 'long' }),
2729
},
2830
];
2931
}
@@ -81,7 +83,7 @@ export default function GridGetPivotDerivedColumns() {
8183
getPivotDerivedColumns={getPivotDerivedColumns}
8284
initialState={{
8385
pivoting: {
84-
enabled: false,
86+
enabled: true,
8587
model: pivotModel,
8688
},
8789
sidebar: {

docs/data/data-grid/pivoting/pivoting.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ The **Transaction Date** column is represented by additional columns in pivot mo
123123
Use the `getPivotDerivedColumns` prop to customize derived columns.
124124
This prop is called for each original column and returns an array of derived columns, or `undefined` if no derived columns are needed.
125125

126+
:::success
127+
To sort the derived columns by a value different than the column header name—to display months of the year, define both `valueGetter` and `valueFormatter` for the derived column.
128+
:::
129+
126130
{{"demo": "GridGetPivotDerivedColumns.js", "bg": "inline", "defaultCodeOpen": true}}
127131

128132
## Sticky column groups

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"@mui/internal-markdown": "^2.0.6",
9494
"@mui/internal-test-utils": "catalog:",
9595
"@mui/material": "catalog:",
96-
"@mui/monorepo": "github:mui/material-ui#6c9df3c694eb89755d6687f555553916f3b4c189",
96+
"@mui/monorepo": "github:mui/material-ui#318f6ffee1bb664f7171ca9293d3548dbafe5913",
9797
"@mui/utils": "catalog:",
9898
"@next/eslint-plugin-next": "15.4.4",
9999
"@octokit/plugin-retry": "^8.0.1",

packages/x-data-grid-premium/src/hooks/features/pivoting/utils.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
GridColDef,
33
GridColumnGroup,
4-
GridColumnGroupingModel,
54
GridColumnNode,
65
GridRowModel,
76
isLeaf,
@@ -19,6 +18,11 @@ import { isGroupingColumn } from '../rowGrouping';
1918
import type { GridPivotingPropsOverrides, GridPivotModel } from './gridPivotingInterfaces';
2019
import { defaultGetAggregationPosition } from '../aggregation/gridAggregationUtils';
2120

21+
interface GridColumnGroupPivoting extends Omit<GridColumnGroup, 'children'> {
22+
rawHeaderName: string;
23+
children: GridColumnGroupPivoting[];
24+
}
25+
2226
const columnGroupIdSeparator = '>->';
2327

2428
export const isPivotingAvailable = (
@@ -74,7 +78,7 @@ export const getInitialColumns = (
7478
};
7579

7680
function sortColumnGroups(
77-
columnGroups: GridColumnNode[],
81+
columnGroups: GridColumnGroupPivoting[],
7882
pivotModelColumns: GridPivotModel['columns'],
7983
depth = 0,
8084
) {
@@ -97,7 +101,7 @@ function sortColumnGroups(
97101
}
98102
return (
99103
(sort === 'asc' ? 1 : -1) *
100-
gridStringOrNumberComparator(a.headerName, b.headerName, {} as any, {} as any)
104+
gridStringOrNumberComparator(a.rawHeaderName, b.rawHeaderName, {} as any, {} as any)
101105
);
102106
});
103107
}
@@ -176,8 +180,8 @@ export const getPivotedData = ({
176180

177181
const aggregationModel: GridAggregationModel = {};
178182

179-
const columnGroupingModel: GridColumnGroupingModel = [];
180-
const columnGroupingModelLookup = new Map<string, GridColumnGroup>();
183+
const columnGroupingModel: GridColumnGroupPivoting[] = [];
184+
const columnGroupingModelLookup = new Map<string, GridColumnGroupPivoting>();
181185

182186
let newRows: GridRowModel[] = [];
183187

@@ -213,13 +217,15 @@ export const getPivotedData = ({
213217
colValue = String(colValue);
214218
}
215219

220+
const formattedHeaderName = apiRef.current.getRowFormattedValue(row, column) || colValue;
216221
columnGroupPath.push(colValue);
217222
const groupId = columnGroupPath.join(columnGroupIdSeparator);
218223

219224
if (!columnGroupingModelLookup.has(groupId)) {
220-
const columnGroup: GridColumnGroupingModel[number] = {
225+
const columnGroup: GridColumnGroupPivoting = {
221226
groupId,
222-
headerName: colValue,
227+
headerName: formattedHeaderName,
228+
rawHeaderName: colValue,
223229
children: [],
224230
};
225231
columnGroupingModelLookup.set(groupId, columnGroup);

packages/x-date-pickers/src/PickersLayout/PickersLayout.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,28 @@ export const PickersLayoutRoot = styled('div', {
3737
[`& .${pickersLayoutClasses.actionBar}`]: { gridColumn: '1 / 4', gridRow: 3 },
3838
variants: [
3939
{
40-
props: { pickerOrientation: 'landscape' },
40+
props: { pickerOrientation: 'landscape', hasShortcuts: false },
4141
style: {
4242
[`& .${pickersLayoutClasses.toolbar}`]: {
4343
gridColumn: 1,
44-
gridRow: '2 / 3',
44+
gridRow: '1 / 3',
4545
},
46-
[`.${pickersLayoutClasses.shortcuts}`]: { gridColumn: '2 / 4', gridRow: 1 },
4746
},
4847
},
4948
{
50-
props: { pickerOrientation: 'landscape', layoutDirection: 'rtl' },
49+
props: {
50+
pickerOrientation: 'landscape',
51+
hasShortcuts: true,
52+
},
5153
style: {
5254
[`& .${pickersLayoutClasses.toolbar}`]: {
53-
gridColumn: 3,
55+
gridColumn: '2 / 4',
56+
gridRow: 1,
57+
maxWidth: 'max-content',
58+
},
59+
[`& .${pickersLayoutClasses.shortcuts}`]: {
60+
gridColumn: 1,
61+
gridRow: 2,
5462
},
5563
},
5664
},
@@ -65,7 +73,7 @@ export const PickersLayoutRoot = styled('div', {
6573
},
6674
},
6775
{
68-
props: { pickerOrientation: 'portrait', layoutDirection: 'rtl' },
76+
props: { hasShortcuts: true, layoutDirection: 'rtl' },
6977
style: {
7078
[`& .${pickersLayoutClasses.shortcuts}`]: {
7179
gridColumn: 4,

packages/x-date-pickers/src/PickersLayout/PickersLayout.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export interface PickerLayoutOwnerState extends PickerOwnerState {
4141
* Is equal to "rtl" when the layout is in right-to-left direction.
4242
*/
4343
layoutDirection: 'ltr' | 'rtl';
44+
/**
45+
* Whether the layout should display the shortcuts panel or not.
46+
* This flag is used to adjust the layout accordingly.
47+
*/
48+
hasShortcuts: boolean;
4449
}
4550

4651
export interface ExportedPickersLayoutSlotProps<TValue extends PickerValidValue> {

packages/x-date-pickers/src/PickersLayout/usePickerLayout.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useRtl } from '@mui/system/RtlProvider';
66
import { PickersActionBar } from '../PickersActionBar';
77
import { PickerLayoutOwnerState, PickersLayoutProps, SubComponents } from './PickersLayout.types';
88
import { getPickersLayoutUtilityClass, PickersLayoutClasses } from './pickersLayoutClasses';
9-
import { PickersShortcuts } from '../PickersShortcuts';
9+
import { PickersShortcuts, PickersShortcutsProps } from '../PickersShortcuts';
1010
import { BaseToolbarProps } from '../internals/models/props/toolbar';
1111
import { PickerValidValue } from '../internals/models';
1212
import { usePickerPrivateContext } from '../internals/hooks/usePickerPrivateContext';
@@ -48,7 +48,7 @@ const usePickerLayout = <TValue extends PickerValidValue>(
4848
const { children, slots, slotProps, classes: classesProp } = props;
4949

5050
const ownerState = React.useMemo<PickerLayoutOwnerState>(
51-
() => ({ ...pickerOwnerState, layoutDirection: isRtl ? 'rtl' : 'ltr' }),
51+
() => ({ ...pickerOwnerState, layoutDirection: isRtl ? 'rtl' : 'ltr', hasShortcuts: false }),
5252
[pickerOwnerState, isRtl],
5353
);
5454
const classes = useUtilityClasses(classesProp, ownerState);
@@ -94,7 +94,10 @@ const usePickerLayout = <TValue extends PickerValidValue>(
9494
externalSlotProps: slotProps?.shortcuts,
9595
className: classes.shortcuts,
9696
ownerState,
97-
});
97+
}) as PickersShortcutsProps<TValue>;
98+
99+
const hasShortcuts = Array.isArray(shortcutsProps?.items) && shortcutsProps.items.length > 0;
100+
98101
const shortcuts = view && !!Shortcuts ? <Shortcuts {...shortcutsProps} /> : null;
99102

100103
return {
@@ -103,7 +106,7 @@ const usePickerLayout = <TValue extends PickerValidValue>(
103106
tabs,
104107
actionBar,
105108
shortcuts,
106-
ownerState,
109+
ownerState: { ...ownerState, hasShortcuts },
107110
};
108111
};
109112

0 commit comments

Comments
 (0)