Skip to content

[DataGrid] Show Column Name in Menu Icon if Header is Too Small #16971

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -35,6 +35,7 @@ export const ColumnHeaderMenuIcon = React.memo((props: ColumnHeaderMenuIconProps
const rootProps = useGridRootProps();
const ownerState = { ...props, classes: rootProps.classes };
const classes = useUtilityClasses(ownerState);
const divRef = React.useRef<HTMLDivElement>(null);

const handleMenuIconClick = React.useCallback(
(event: React.MouseEvent<HTMLButtonElement>) => {
Expand All @@ -46,11 +47,19 @@ export const ColumnHeaderMenuIcon = React.memo((props: ColumnHeaderMenuIconProps
);

const columnName = colDef.headerName ?? colDef.field;
const titleWidth =
divRef.current?.parentElement?.firstElementChild?.firstElementChild?.clientWidth;
const neededWidth =
divRef.current?.parentElement?.firstElementChild?.firstElementChild?.firstElementChild
?.scrollWidth ?? 1;
Comment on lines +52 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can break with custom header renderers, where the first element isn't necessarily a text block, or there are follow-up elements like in the docs example: Custom header renderer

const title =
apiRef.current.getLocaleText('columnMenuLabel') +
(titleWidth !== undefined && titleWidth < neededWidth / 2 ? ` (${columnName})` : '');

return (
<div className={classes.root}>
<div className={classes.root} ref={divRef}>
<rootProps.slots.baseTooltip
title={apiRef.current.getLocaleText('columnMenuLabel')}
title={title}
enterDelay={1000}
{...rootProps.slotProps?.baseTooltip}
>
Expand Down