Skip to content

Commit 6b2d796

Browse files
committed
[DataGridPremium] Fix leafField to have correct focus value (#7950)
1 parent b15ede4 commit 6b2d796

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

packages/grid/x-data-grid-premium/src/hooks/features/rowGrouping/createGroupingColDef.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ export const createGroupingColDefForOneGroupingCriteria = ({
178178
const leafParams: GridRenderCellParams = {
179179
...params.api.getCellParams(params.id, leafField!),
180180
api: params.api,
181+
hasFocus: params.hasFocus,
181182
};
182183
if (leafColDef.renderCell) {
183184
return leafColDef.renderCell(leafParams);
@@ -299,6 +300,7 @@ export const createGroupingColDefForAllGroupingCriteria = ({
299300
const leafParams: GridRenderCellParams = {
300301
...params.api.getCellParams(params.id, leafField!),
301302
api: params.api,
303+
hasFocus: params.hasFocus,
302304
};
303305
if (leafColDef.renderCell) {
304306
return leafColDef.renderCell(leafParams);

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as React from 'react';
22
// @ts-ignore Remove once the test utils are typed
3-
import { createRenderer, fireEvent, screen, act } from '@mui/monorepo/test/utils';
3+
import { createRenderer, fireEvent, screen, act, userEvent } from '@mui/monorepo/test/utils';
44
import {
55
getColumnHeaderCell,
66
getColumnHeadersTextContent,
77
getColumnValues,
8+
getCell,
89
} from 'test/utils/helperFn';
910
import { expect } from 'chai';
1011
import {
@@ -826,6 +827,33 @@ describe('<DataGridPremium /> - Row Grouping', () => {
826827
'Custom leaf',
827828
]);
828829
});
830+
831+
// See https://github.com/mui/mui-x/issues/7949
832+
it('should correctly pass `hasFocus` to `renderCell` defined on the leafColDef', () => {
833+
const renderIdCell = spy((params) => `Focused: ${params.hasFocus}`);
834+
835+
render(
836+
<Test
837+
columns={[
838+
{
839+
field: 'id',
840+
type: 'number',
841+
renderCell: renderIdCell,
842+
},
843+
{
844+
field: 'category1',
845+
},
846+
]}
847+
initialState={{ rowGrouping: { model: ['category1'] } }}
848+
groupingColDef={{ leafField: 'id' }}
849+
defaultGroupingExpansionDepth={-1}
850+
/>,
851+
);
852+
853+
userEvent.mousePress(getCell(1, 0));
854+
expect(renderIdCell.lastCall.firstArg.field).to.equal('id');
855+
expect(getCell(1, 0)).to.have.text('Focused: true');
856+
});
829857
});
830858

831859
describe('prop: groupColDef.headerName', () => {

0 commit comments

Comments
 (0)