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

[data grid] Context menu in column headers #12749

Closed
trbagley-gresham opened this issue Apr 11, 2024 · 5 comments · Fixed by #12768
Closed

[data grid] Context menu in column headers #12749

trbagley-gresham opened this issue Apr 11, 2024 · 5 comments · Fixed by #12768
Assignees
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! customization: extend Logic customizability support: premium standard Support request from a Premium standard plan user. https://mui.com/legal/technical-support-sla/

Comments

@trbagley-gresham
Copy link

trbagley-gresham commented Apr 11, 2024

Summary

Enhanced API for grid header customisation.

Examples

Related issue:
#6691

Motivation

We'd like to show the context menu when right clicking on a column header rather than clicking on the menu icon (three vertical dots). This would involve adding an onContextMenu event handler to each header which would open the filter panel. A frequent recommendation is to make use of the renderHeader property of GridColDef; however, this does not give full control over the header as illustrated in the screenshot below:
grid
If we add an event handler via renderHeader, only the coloured sections are clickable as opposed to the entire header.

Are there currently any props which would facilitate this? The closest implementation I have encountered can be seen in the MUI docs. If something similar to this is/was available for the header, I think it would address our requirements.

Search keywords: grid header customisation
Order ID: 45466

@trbagley-gresham trbagley-gresham added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Apr 11, 2024
@zannager zannager added component: data grid This is the name of the generic UI component, not the React module! support: premium standard Support request from a Premium standard plan user. https://mui.com/legal/technical-support-sla/ labels Apr 12, 2024
@cherniavskii cherniavskii self-assigned this Apr 12, 2024
@cherniavskii
Copy link
Member

Hi @trbagley-gresham
The suggested approach hear would be to pass the onContextMenu callback to the columnHeaders slot props:

const onContextMenu = React.useCallback((event: React.MouseEvent) => {
  const headerCell = (event.target as HTMLElement).closest(`[role="columnheader"]`);
  if (!headerCell) {
    return;
  }
  const field = headerCell.getAttribute('data-field');
  console.log('Context menu on column header:', field);
}, []);

// ...

<DataGrid
  rows={rows}
  columns={columns}
  slotProps={{
    columnHeaders: {
      onContextMenu,
    },
  }}
/>

But it doesn't work because of a bug on our side – we don't pass the slotProps to the columnHeaders slot in

<rootProps.slots.columnHeaders

Fortunately, you can override the columnHeaders to workaround this issue, here is a working demo:
https://stackblitz.com/edit/react-qkbqyt?file=Demo.tsx
Meanwhile, I'll fix the issue on our end.

Is this what you're looking for

@cherniavskii cherniavskii added customization: extend Logic customizability bug 🐛 Something doesn't work status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Apr 12, 2024
@cherniavskii cherniavskii changed the title [data grid] Header customisation [data grid] Context menu for column headers Apr 12, 2024
@trbagley-gresham
Copy link
Author

@cherniavskii thanks for the demo, this looks promising. I did try adding an onContextMenu handler to the header by passing a custom component via the columnHeaders slot prop. The problem I ran into was that I couldn't figure out a reliable way to find the correct element. Using closest() appears to solve this.

We'll need to obtain the GridColDef for the column header from which the event was fired. I think we'll be able to achieve this by extracting the value from data-field and looking it up somehow.

@cherniavskii cherniavskii changed the title [data grid] Context menu for column headers [data grid] Context menu on column headers Apr 12, 2024
@cherniavskii cherniavskii changed the title [data grid] Context menu on column headers [data grid] Context menu in column headers Apr 12, 2024
@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Apr 12, 2024
@cherniavskii
Copy link
Member

@trbagley-gresham

We'll need to obtain the GridColDef for the column header from which the event was fired. I think we'll be able to achieve this by extracting the value from data-field and looking it up somehow.

apiRef.current.getColumn would do the trick.

@trbagley-gresham
Copy link
Author

@cherniavskii perfect, thanks!

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer status: waiting for author Issue with insufficient information labels Apr 15, 2024
Copy link

⚠️ This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

@trbagley-gresham: How did we do? Your experience with our support team matters to us. If you have a moment, please share your thoughts in this short Support Satisfaction survey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! customization: extend Logic customizability support: premium standard Support request from a Premium standard plan user. https://mui.com/legal/technical-support-sla/
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants