Skip to content
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 column pinning with checkbox selection and row grouping #15908

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ export const useGridRowGroupingPreProcessors = (
const groupingColDefs = getGroupingColDefs(columnsState);
let newColumnFields: string[] = [];
const newColumnsLookup: GridColumnRawLookup = {};
const prevGroupingfields: string[] = [];

// We only keep the non-grouping columns
columnsState.orderedFields.forEach((field) => {
if (!isGroupingColumn(field)) {
if (isGroupingColumn(field)) {
prevGroupingfields.push(field);
} else {
newColumnFields.push(field);
newColumnsLookup[field] = columnsState.lookup[field];
}
Expand All @@ -135,14 +138,17 @@ export const useGridRowGroupingPreProcessors = (

newColumnsLookup[groupingColDef.field] = groupingColDef;
});
const startIndex = newColumnFields[0] === GRID_CHECKBOX_SELECTION_FIELD ? 1 : 0;
newColumnFields = [
...newColumnFields.slice(0, startIndex),
...groupingColDefs.map((colDef) => colDef.field),
...newColumnFields.slice(startIndex),
];

columnsState.orderedFields = newColumnFields;
if (prevGroupingfields.length !== groupingColDefs.length) {
const startIndex = newColumnFields[0] === GRID_CHECKBOX_SELECTION_FIELD ? 1 : 0;
newColumnFields = [
...newColumnFields.slice(0, startIndex),
...groupingColDefs.map((colDef) => colDef.field),
...newColumnFields.slice(startIndex),
];
columnsState.orderedFields = newColumnFields;
}

columnsState.lookup = newColumnsLookup;

return columnsState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2632,6 +2632,58 @@ describe('<DataGridPremium /> - Row grouping', () => {
});
});

describe('column pinning', () => {
it('should keep the checkbox selection column position after column is unpinned when groupingColumnMode = "single"', () => {
const { setProps } = render(
<Test
checkboxSelection
initialState={{ rowGrouping: { model: ['category1'] } }}
defaultGroupingExpansionDepth={-1}
/>,
);
const initialColumnOrder = ['', 'category1', 'id', 'category1', 'category2'];
expect(getColumnHeadersTextContent()).to.deep.equal(initialColumnOrder);
setProps({ pinnedColumns: { left: ['id'] } });
expect(getColumnHeadersTextContent()).to.deep.equal([
'id',
'',
'category1',
'category1',
'category2',
]);
setProps({ pinnedColumns: { left: [] } });
expect(getColumnHeadersTextContent()).to.deep.equal(initialColumnOrder);
});

it('should keep the checkbox selection column position after column is unpinned when groupingColumnMode = "multiple"', () => {
const { setProps } = render(
<Test
checkboxSelection
initialState={{ rowGrouping: { model: ['category1', 'category2'] } }}
rowGroupingColumnMode="multiple"
defaultGroupingExpansionDepth={-1}
/>,
);
const initialColumnOrder = ['', 'category1', 'category2', 'id', 'category1', 'category2'];
expect(getColumnHeadersTextContent()).to.deep.equal(initialColumnOrder);
setProps({
pinnedColumns: {
left: ['__row_group_by_columns_group_category2__', 'id'],
},
});
expect(getColumnHeadersTextContent()).to.deep.equal([
'category2',
'id',
'',
'category1',
'category1',
'category2',
]);
setProps({ pinnedColumns: { left: [] } });
expect(getColumnHeadersTextContent()).to.deep.equal(initialColumnOrder);
});
});

describe('apiRef: addRowGroupingCriteria', () => {
clock.withFakeTimers();

Expand Down
Loading