Skip to content

Commit

Permalink
[DataGrid] Add columnGroupHeaderHeight prop for sizing column group…
Browse files Browse the repository at this point in the history
… headers (mui#14637)
  • Loading branch information
KenanYusuf authored and Arthur Balduini committed Sep 30, 2024
1 parent e8bc9a9 commit 24b566f
Show file tree
Hide file tree
Showing 22 changed files with 254 additions and 1 deletion.
65 changes: 65 additions & 0 deletions docs/data/data-grid/column-groups/GroupHeaderHeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';

const columns = [
{ field: 'id', headerName: 'ID', width: 90 },
{
field: 'firstName',
headerName: 'First name',
width: 150,
},
{
field: 'lastName',
headerName: 'Last name',
width: 150,
},
{
field: 'age',
headerName: 'Age',
type: 'number',
width: 110,
},
];

const rows = [
{ id: 1, lastName: 'Snow', firstName: 'Jon', age: 14 },
{ id: 2, lastName: 'Lannister', firstName: 'Cersei', age: 31 },
{ id: 3, lastName: 'Lannister', firstName: 'Jaime', age: 31 },
{ id: 4, lastName: 'Stark', firstName: 'Arya', age: 11 },
{ id: 5, lastName: 'Targaryen', firstName: 'Daenerys', age: null },
{ id: 6, lastName: 'Melisandre', firstName: null, age: 150 },
{ id: 7, lastName: 'Clifford', firstName: 'Ferrara', age: 44 },
{ id: 8, lastName: 'Frances', firstName: 'Rossini', age: 36 },
{ id: 9, lastName: 'Roxie', firstName: 'Harvey', age: 65 },
];

const columnGroupingModel = [
{
groupId: 'Internal',
description: '',
children: [{ field: 'id' }],
},
{
groupId: 'Basic info',
children: [
{
groupId: 'Full name',
children: [{ field: 'lastName' }, { field: 'firstName' }],
},
{ field: 'age' },
],
},
];

export default function GroupHeaderHeight() {
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={columns}
columnGroupingModel={columnGroupingModel}
columnGroupHeaderHeight={36}
/>
</div>
);
}
65 changes: 65 additions & 0 deletions docs/data/data-grid/column-groups/GroupHeaderHeight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as React from 'react';
import { DataGrid, GridColDef, GridColumnGroupingModel } from '@mui/x-data-grid';

const columns: GridColDef[] = [
{ field: 'id', headerName: 'ID', width: 90 },
{
field: 'firstName',
headerName: 'First name',
width: 150,
},
{
field: 'lastName',
headerName: 'Last name',
width: 150,
},
{
field: 'age',
headerName: 'Age',
type: 'number',
width: 110,
},
];

const rows = [
{ id: 1, lastName: 'Snow', firstName: 'Jon', age: 14 },
{ id: 2, lastName: 'Lannister', firstName: 'Cersei', age: 31 },
{ id: 3, lastName: 'Lannister', firstName: 'Jaime', age: 31 },
{ id: 4, lastName: 'Stark', firstName: 'Arya', age: 11 },
{ id: 5, lastName: 'Targaryen', firstName: 'Daenerys', age: null },
{ id: 6, lastName: 'Melisandre', firstName: null, age: 150 },
{ id: 7, lastName: 'Clifford', firstName: 'Ferrara', age: 44 },
{ id: 8, lastName: 'Frances', firstName: 'Rossini', age: 36 },
{ id: 9, lastName: 'Roxie', firstName: 'Harvey', age: 65 },
];

const columnGroupingModel: GridColumnGroupingModel = [
{
groupId: 'Internal',
description: '',
children: [{ field: 'id' }],
},
{
groupId: 'Basic info',
children: [
{
groupId: 'Full name',
children: [{ field: 'lastName' }, { field: 'firstName' }],
},
{ field: 'age' },
],
},
];

export default function GroupHeaderHeight() {
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
rows={rows}
columns={columns}
columnGroupingModel={columnGroupingModel}
columnGroupHeaderHeight={36}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<DataGrid
rows={rows}
columns={columns}
columnGroupingModel={columnGroupingModel}
columnGroupHeaderHeight={36}
/>
8 changes: 8 additions & 0 deletions docs/data/data-grid/column-groups/column-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ In addition to the required `groupId` and `children`, you can use the following

{{"demo": "CustomizationDemo.js", "bg": "inline"}}

## Group header height

By default, column group headers are the same height as [column headers](/x/react-data-grid/column-header/#header-height). This will be the default 56 pixels or a custom value set with the `columnHeaderHeight` prop.

The `columnGroupHeaderHeight` prop can be used to size column group headers independently of column headers.

{{"demo": "GroupHeaderHeight.js", "bg": "inline"}}

## Column reordering [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan')

By default, the columns that are part of a group cannot be dragged to outside their group.
Expand Down
28 changes: 28 additions & 0 deletions docs/data/data-grid/column-header/HeaderHeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';

const rows = [
{
id: 1,
username: '@MUI',
age: 20,
},
];

const columns = [
{
field: 'username',
headerName: 'Username',
description:
'The identification used by the person with access to the online service.',
},
{ field: 'age', headerName: 'Age' },
];

export default function HeaderHeight() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid columns={columns} rows={rows} columnHeaderHeight={36} />
</div>
);
}
28 changes: 28 additions & 0 deletions docs/data/data-grid/column-header/HeaderHeight.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import { DataGrid, GridColDef } from '@mui/x-data-grid';

const rows = [
{
id: 1,
username: '@MUI',
age: 20,
},
];

const columns: GridColDef[] = [
{
field: 'username',
headerName: 'Username',
description:
'The identification used by the person with access to the online service.',
},
{ field: 'age', headerName: 'Age' },
];

export default function HeaderHeight() {
return (
<div style={{ height: 250, width: '100%' }}>
<DataGrid columns={columns} rows={rows} columnHeaderHeight={36} />
</div>
);
}
1 change: 1 addition & 0 deletions docs/data/data-grid/column-header/HeaderHeight.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<DataGrid columns={columns} rows={rows} columnHeaderHeight={36} />
8 changes: 8 additions & 0 deletions docs/data/data-grid/column-header/column-header.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ const columns: GridColDef[] = [

{{"demo": "RenderHeaderGrid.js", "bg": "inline"}}

## Header height

By default, column headers have a height of 56 pixels. This matches the height from the [Material Design guidelines](https://m2.material.io/components/data-tables).

The `columnHeaderHeight` prop can be used to override the default value.

{{"demo": "HeaderHeight.js", "bg": "inline"}}

## Styling header

You can check the [styling header](/x/react-data-grid/style/#styling-column-headers) section for more information.
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-premium.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } },
"clipboardCopyCellDelimiter": { "type": { "name": "string" }, "default": "'\\t'" },
"columnBufferPx": { "type": { "name": "number" }, "default": "150" },
"columnGroupHeaderHeight": { "type": { "name": "number" } },
"columnHeaderHeight": { "type": { "name": "number" }, "default": "56" },
"columnVisibilityModel": { "type": { "name": "object" } },
"defaultGroupingExpansionDepth": { "type": { "name": "number" }, "default": "0" },
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid-pro.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } },
"clipboardCopyCellDelimiter": { "type": { "name": "string" }, "default": "'\\t'" },
"columnBufferPx": { "type": { "name": "number" }, "default": "150" },
"columnGroupHeaderHeight": { "type": { "name": "number" } },
"columnHeaderHeight": { "type": { "name": "number" }, "default": "56" },
"columnVisibilityModel": { "type": { "name": "object" } },
"defaultGroupingExpansionDepth": { "type": { "name": "number" }, "default": "0" },
Expand Down
1 change: 1 addition & 0 deletions docs/pages/x/api/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } },
"clipboardCopyCellDelimiter": { "type": { "name": "string" }, "default": "'\\t'" },
"columnBufferPx": { "type": { "name": "number" }, "default": "150" },
"columnGroupHeaderHeight": { "type": { "name": "number" } },
"columnHeaderHeight": { "type": { "name": "number" }, "default": "56" },
"columnVisibilityModel": { "type": { "name": "object" } },
"density": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
"columnBufferPx": {
"description": "Column region in pixels to render before/after the viewport"
},
"columnGroupHeaderHeight": {
"description": "Sets the height in pixels of the column group headers in the Data Grid. Inherits the <code>columnHeaderHeight</code> value if not set."
},
"columnHeaderHeight": {
"description": "Sets the height in pixel of the column headers in the Data Grid."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"columnBufferPx": {
"description": "Column region in pixels to render before/after the viewport"
},
"columnGroupHeaderHeight": {
"description": "Sets the height in pixels of the column group headers in the Data Grid. Inherits the <code>columnHeaderHeight</code> value if not set."
},
"columnHeaderHeight": {
"description": "Sets the height in pixel of the column headers in the Data Grid."
},
Expand Down
3 changes: 3 additions & 0 deletions docs/translations/api-docs/data-grid/data-grid/data-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"columnBufferPx": {
"description": "Column region in pixels to render before/after the viewport"
},
"columnGroupHeaderHeight": {
"description": "Sets the height in pixels of the column group headers in the Data Grid. Inherits the <code>columnHeaderHeight</code> value if not set."
},
"columnHeaderHeight": {
"description": "Sets the height in pixel of the column headers in the Data Grid."
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ DataGridPremiumRaw.propTypes = {
* @default 150
*/
columnBufferPx: PropTypes.number,
/**
* Sets the height in pixels of the column group headers in the Data Grid.
* Inherits the `columnHeaderHeight` value if not set.
*/
columnGroupHeaderHeight: PropTypes.number,
columnGroupingModel: PropTypes.arrayOf(PropTypes.object),
/**
* Sets the height in pixel of the column headers in the Data Grid.
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid-pro/src/DataGridPro/DataGridPro.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ DataGridProRaw.propTypes = {
* @default 150
*/
columnBufferPx: PropTypes.number,
/**
* Sets the height in pixels of the column group headers in the Data Grid.
* Inherits the `columnHeaderHeight` value if not set.
*/
columnGroupHeaderHeight: PropTypes.number,
columnGroupingModel: PropTypes.arrayOf(PropTypes.object),
/**
* Sets the height in pixel of the column headers in the Data Grid.
Expand Down
5 changes: 5 additions & 0 deletions packages/x-data-grid/src/DataGrid/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ DataGridRaw.propTypes = {
* @default 150
*/
columnBufferPx: PropTypes.number,
/**
* Sets the height in pixels of the column group headers in the Data Grid.
* Inherits the `columnHeaderHeight` value if not set.
*/
columnGroupHeaderHeight: PropTypes.number,
columnGroupingModel: PropTypes.arrayOf(PropTypes.object),
/**
* Sets the height in pixel of the column headers in the Data Grid.
Expand Down
1 change: 1 addition & 0 deletions packages/x-data-grid/src/components/GridRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ GridRow.propTypes = {
height: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
}).isRequired,
groupHeaderHeight: PropTypes.number.isRequired,
hasScrollX: PropTypes.bool.isRequired,
hasScrollY: PropTypes.bool.isRequired,
headerFilterHeight: PropTypes.number.isRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ export const useGridColumnHeaders = (props: UseGridColumnHeadersProps) => {
depth={depth}
isLastColumn={headerInfo.colIndex === visibleColumns.length - headerInfo.fields.length}
maxDepth={headerGroupingMaxDepth}
height={dimensions.headerHeight}
height={dimensions.groupHeaderHeight}
hasFocus={hasFocus}
tabIndex={tabIndex}
pinnedPosition={pinnedPosition}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export interface GridDimensions {
* Height of one column header.
*/
headerHeight: number;
/**
* Height of one column group header.
*/
groupHeaderHeight: number;
/**
* Height of header filters.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type RootProps = Pick<
| 'rowHeight'
| 'resizeThrottleMs'
| 'columnHeaderHeight'
| 'columnGroupHeaderHeight'
| 'headerFilterHeight'
>;

Expand All @@ -59,6 +60,7 @@ const EMPTY_DIMENSIONS: GridDimensions = {
hasScrollY: false,
scrollbarSize: 0,
headerHeight: 0,
groupHeaderHeight: 0,
headerFilterHeight: 0,
rowWidth: 0,
rowHeight: 0,
Expand Down Expand Up @@ -92,6 +94,9 @@ export function useGridDimensions(
const densityFactor = useGridSelector(apiRef, gridDensityFactorSelector);
const rowHeight = Math.floor(props.rowHeight * densityFactor);
const headerHeight = Math.floor(props.columnHeaderHeight * densityFactor);
const groupHeaderHeight = Math.floor(
(props.columnGroupHeaderHeight ?? props.columnHeaderHeight) * densityFactor,
);
const headerFilterHeight = Math.floor(
(props.headerFilterHeight ?? props.columnHeaderHeight) * densityFactor,
);
Expand Down Expand Up @@ -248,6 +253,7 @@ export function useGridDimensions(
hasScrollY,
scrollbarSize,
headerHeight,
groupHeaderHeight,
headerFilterHeight,
rowWidth,
rowHeight,
Expand Down Expand Up @@ -275,6 +281,7 @@ export function useGridDimensions(
rowsMeta.currentPageTotalHeight,
rowHeight,
headerHeight,
groupHeaderHeight,
headerFilterHeight,
columnsTotalWidth,
headersTotalHeight,
Expand Down
Loading

0 comments on commit 24b566f

Please sign in to comment.