Support "enter" keydown on button
See original GitHub issueIn a browser, enter keydown trigger the click event of the button.
It would be nice if the user-event library support this behavior.
You can test the browser behavior here: https://codesandbox.io/s/jovial-glitter-jy883?from-embed
A naive implementation would be to add a keyDown
function to user-event:
keyDown(element, options) {
fireEvent.keyDown(element, options);
if (element.tagName === "BUTTON") {
fireEvent.click(element);
}
}
Thank you,
Patrick
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Trigger a button click with JavaScript on the Enter key in a text ...
Problem with using keydown is that holding down enter will fire the event over and over again. if you attach to the keyup...
Read more >How To Trigger Button Click on Enter - W3Schools
Trigger a button click on keyboard "enter" with JavaScript. Trigger a Button Click on Enter. Press the "Enter" key inside the input field...
Read more >How to Trigger Button Click on Enter Key Press using JavaScript
Trigger button click on Enter key press on the keyboard. Use KeyboardEvent keyCode property to trigger button click for submitting form or ...
Read more >KeyboardEvent.key - Web APIs | MDN
A keydown event is first fired. If the key is held down further and the key produces a character key, then the event...
Read more >Trigger a button click on Enter key with JavaScript/jQuery
The idea is to bind an event handler to the keyup JavaScript event using jQuery's .keyup(handler) method and use that handler to check...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is now supported with
userEvent.type(button, '{enter}')
Feel free to correct me if there’s another way to do it, but I found
userEvent.type(button, '{enter}')
didn’t work for me today to fire the onClick event on a button (maybe this has changed since this ticket was implemented).I stripped it down to a regular old
button
and found focusing the button and usinguserEvent.keyboard('{enter}')
was the way to do it (still on user-event v 13.5, FYI):