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
Click events are triggered with 'enter' keyPress events inside forms #3907
Comments
Isn't that default html behavior ? If you have an input and a button in the form, pressing enter will also click the button. |
How is this even close to default behaviour? An |
buttons are |
Apologies, rechecked the original code and this might have been caused by browser defaults. First button is missing the type directive, both FF and chrome probably assign a type submit by default which could explain this.
Only question that remains is, is it valid to only trigger the click on the first occurring button element and not both? |
Also - is there some way (on the event) to differentiate between a button click and an form enter keypress? Ideally I want to be able to hit enter and |
@zeroasterisk Read |
It's actually on the event. Not the target. The following follows the correct pattern to descern the difference between a click event and a return event on a button. // src/components/Button/Button.js
import React from 'react';
const handleClick = (event) => {
if (event.detail === 0) {
// a return event occured
}
// some type of click event occured
};
const Button = (props) => (
<button onClick={handleClick}>{props.label + ''}</button>
);
export default Button; |
Exactly. I guess since type="Submit" is the default, we have to ensure that type="button" which should be the default in my opinion! |
When an input element is in focus, it can be activated as a press |
onKeyPress={(e) => { e.key === "Enter" ? handleOk() : '' }} |
This makes no sense at all to be honest. I have a form. <form onSubmit={handleSubmit}>
<div>
<input name="1" id="1" />
<input name="2" id="2" />
</div>
<button onClick={changeRoute}>Back</button>
<button>Submit</button>
</form> When I press enter with focus set to input 1 or 2, changeRoute is called for some reason... <form onSubmit={handleSubmit}>
<div>
<input name="1" id="1" />
<input name="2" id="2" />
</div>
<button type="button" onClick={changeRoute}>Back</button> // <-- ADD TYPE="BUTTON"
<button>Submit</button>
</form> Why is handleSubmit called as I would expect first after adding type="button" to the back button? |
I also noticed in my case that setting |
Triggering a keypress
enter
event inside a forminput type=text
that has also has abutton
with a click listener without a definedtype
will result in a SyntheticMouseEventclick
on that button and hit the clickHandler. Adding atype
will prevent this behavior.The text was updated successfully, but these errors were encountered: