Skip to content

MenuItem: custom onClick not fired when tag is button and user presses enter #131

@OliverJAsh

Description

@OliverJAsh

Reduced test case: https://stackblitz.com/edit/react-ts-sbkech?file=index.tsx

If you open the menu, focus the a, and then press enter, the custom onClick handler is fired as seen from the logs. ✅

If you open the menu, focus the button, and then press enter, the custom onClick handler is not fired as seen from the logs. ❌

I would expect onClick to work when the user presses enter on a button, just like how it works on an a.

My understanding of what is happening here:

  • MenuItem has some code that will preventDefault inside the keydown event if this condition is not met: this.props.tag === 'a' && this.props.href
  • therefore, in this case where tag is button, it will preventDefault
  • therefore there will be no click event after the keydown event finishes

Note: onClick is called if you actually click, as opposed to using the enter key.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions