Skip to content

Commit 8e9f301

Browse files
authored
[DataGridPremium] Update column state correctly when grouping mode is updated with one grouping column (#17069)
1 parent 57e6192 commit 8e9f301

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

packages/x-data-grid-premium/src/hooks/features/rowGrouping/useGridRowGroupingPreProcessors.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,10 @@ export const useGridRowGroupingPreProcessors = (
117117
const groupingColDefs = getGroupingColDefs(columnsState);
118118
let newColumnFields: string[] = [];
119119
const newColumnsLookup: GridColumnRawLookup = {};
120-
const prevGroupingfields: string[] = [];
121120

122121
// We only keep the non-grouping columns
123122
columnsState.orderedFields.forEach((field) => {
124-
if (isGroupingColumn(field)) {
125-
prevGroupingfields.push(field);
126-
} else {
123+
if (!isGroupingColumn(field)) {
127124
newColumnFields.push(field);
128125
newColumnsLookup[field] = columnsState.lookup[field];
129126
}
@@ -140,16 +137,19 @@ export const useGridRowGroupingPreProcessors = (
140137
newColumnsLookup[groupingColDef.field] = groupingColDef;
141138
});
142139

143-
if (prevGroupingfields.length !== groupingColDefs.length) {
144-
const startIndex = newColumnFields[0] === GRID_CHECKBOX_SELECTION_FIELD ? 1 : 0;
145-
newColumnFields = [
146-
...newColumnFields.slice(0, startIndex),
147-
...groupingColDefs.map((colDef) => colDef.field),
148-
...newColumnFields.slice(startIndex),
149-
];
150-
columnsState.orderedFields = newColumnFields;
151-
}
140+
const checkBoxFieldIndex = newColumnFields.findIndex(
141+
(field) => field === GRID_CHECKBOX_SELECTION_FIELD,
142+
);
143+
const checkBoxColumn =
144+
checkBoxFieldIndex !== -1 ? newColumnFields.splice(checkBoxFieldIndex, 1) : [];
145+
146+
newColumnFields = [
147+
...checkBoxColumn,
148+
...groupingColDefs.map((colDef) => colDef.field),
149+
...newColumnFields,
150+
];
152151

152+
columnsState.orderedFields = newColumnFields;
153153
columnsState.lookup = newColumnsLookup;
154154

155155
return columnsState;

packages/x-data-grid-premium/src/tests/rowGrouping.DataGridPremium.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,34 @@ describe('<DataGridPremium /> - Row grouping', () => {
468468
]);
469469
});
470470

471+
// https://github.com/mui/mui-x/issues/17046
472+
it('should support rowGroupingColumnMode switch with one grouping column', () => {
473+
const { setProps } = render(
474+
<Test
475+
rowGroupingModel={['category1']}
476+
rowGroupingColumnMode="multiple"
477+
defaultGroupingExpansionDepth={-1}
478+
/>,
479+
);
480+
481+
expect(getColumnHeadersTextContent()).to.deep.equal([
482+
'category1',
483+
'id',
484+
'category1',
485+
'category2',
486+
]);
487+
expect(getColumnValues(0)).to.deep.equal(['Cat A (3)', '', '', '', 'Cat B (2)', '', '']);
488+
489+
setProps({ rowGroupingColumnMode: 'single' });
490+
expect(getColumnHeadersTextContent()).to.deep.equal([
491+
'category1',
492+
'id',
493+
'category1',
494+
'category2',
495+
]);
496+
expect(getColumnValues(0)).to.deep.equal(['Cat A (3)', '', '', '', 'Cat B (2)', '', '']);
497+
});
498+
471499
it('should respect the model grouping order when rowGroupingColumnMode = "single"', () => {
472500
render(
473501
<Test

0 commit comments

Comments
 (0)