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] Custom Header Keyboard Navigation #12616

Closed
mogrady88 opened this issue Mar 29, 2024 · 4 comments
Closed

[data grid] Custom Header Keyboard Navigation #12616

mogrady88 opened this issue Mar 29, 2024 · 4 comments
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: extend Logic customizability status: waiting for author Issue with insufficient information

Comments

@mogrady88
Copy link

mogrady88 commented Mar 29, 2024

Steps to reproduce

Link to live example: (required) https://mui.com/x/react-data-grid/column-header/#custom-header-renderer

Steps:

  1. duplicate the columns in the Custom Header Renderer
  2. Attempt to move left or right via the Arrow keys like you can in normal header cells
  3. Nothing happens / focused cell doesn't change?

Current behavior

I have a custom checkbox renderer made up of:

It is housed in a ColDef > renderHeader

Ive been trying everything I can to get this component to both respect the keyboard navigation (Right/Left arrow keys) as well as the keyboard events for the buttons and I cannot get it to work.

I thought it was an implementation issue on my part but then I discovered that this same thing happens in your docs in the Custom Header Renderer demo if you duplicate the columns.

// one of the methods used in TableHeaderCell

 const selectAllRows = () => {
    const rowIds = apiRef.current.getAllRowIds();
    rowIds.forEach(rowId => apiRef.current.selectRow(rowId));
    const newSelectedRows = apiRef.current.getSelectedRows();

    setSelectedRowCount(newSelectedRows.size);
  };
  
// collection of methods being passed to the TableHeaderCell below

 const tableHeaderCellProps = {
    deselectAll: deselectAllRows,
    rowCount: rows.length,
    selectAll: selectAllRows,
    selectPage: selectPageRows,
  };

// ColDef[]

[...otherColumns,
 {
        ...GRID_CHECKBOX_SELECTION_COL_DEF,
        align: 'start',
        field: 'custom',
        minWidth: 80,
        renderHeader: () => <TableHeaderCell {...tableHeaderCellProps} selectedRowCount={selectedRowCount} />,
        width: 80,
  },
 ]

Expected behavior

A custom header using headerRenderer should work by allowing for keyboard navigation across it unless you somehow focus into that cell. It should not get "stuck" in that cell when you're using Right/Left arrows to traverse the table

Context

A021E1B3-2F76-46BA-9B38-CD51B5CDB46A

Im trying to accomplish rendering a custom component in my checkboxSelection DataGrid. Keyboard events work for my custom TableHeaderCell but when it's thrown into the DataGrid I dont get any eventing coming through and the Right/Left arrows no longer work.

My custom component receives focus and I can tab through it but the Enter key doesnt fire any event against the custom component

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: data grid custom header keyboard navigation renderHeader checkbox

@mogrady88 mogrady88 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Mar 29, 2024
@zannager zannager added the component: data grid This is the name of the generic UI component, not the React module! label Apr 1, 2024
@michelengelen michelengelen changed the title DataGrid - Custom Header Keyboard Navigation [data grid] Custom Header Keyboard Navigation Apr 2, 2024
@michelengelen
Copy link
Member

Hey @mogrady88,
I tried replicating your described behaviour, but wasn't able to do so. It seems there is something more going on in your component that I am not aware of.

Could you please provide a minimal reproduction test case with the latest version. This would help a lot 👷.

A live example would be perfect. You can find a examples and guides on how to find templates in our docs with which you can provide examples for your specific use-case: Support - Bug reproduction.

Thank you! 🙇🏼

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

mogrady88 commented Apr 2, 2024

Hey Michel, you are right. The issue turned out to be my own. We're trying to implement a custom checkAll header with some additional functionality and it looks like there are some limitations around keyboard events. This source code reads like you short circuit keyboard events from custom renderHeader components unless its of type __check__.

Our current solution is to define a column with a field: 'custom' and then subscribe to columnHeaderKeyDown and manually handle the ArrowRight, ArrowLeft, and ArrowDown eventing. I havent seen any clean example of a DataGrid with checkboxSelection that includes a custom renderHeader

Do you have any advice on how to drill keyboard events to a ColDef with a field of __check__ and a renderHeader component? The simplest reproduction would be to add a renderHeader: () => <Button onClick={() => console.log('clicked'}>Click Me</Button>. If I navigate via keyboard to that header and try to activate the button with Space or Enter no event comes through and that onClick doesnt fire.

@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 2, 2024
@michelengelen
Copy link
Member

Well ... for the keyboard navigation to work you would need to handle key strokes inside the rendered header on your own. I did test it with a simple button and pressing space or enter did fire the onClick event just fine (screencast below).\

Screen.Recording.2024-04-03.at.12.11.57.mov

The underlying problem why the keyboard navigation is no longer working is that the button receives focus and traps it. The events for arrows need to be bubbled to the parent container, so that they can act upon it.

@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 labels Apr 3, 2024
Copy link

The issue has been inactive for 7 days and has been automatically closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! customization: extend Logic customizability status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

3 participants