Prevent enter key from submitting #2549
-
Hi, I'd like to prevent the enter key from submitting. I have a MUI Textfield that I'm using as an autocomplete. If the user hits enter with the text that's entered, it creates a custom item. However, the form is submitting whenever I hit enter, which prevents my component's desired functionality. I've scoured the web and nothing I've tried helps. The synthetic event passed to the onSubmit function on the form doesn't have a key or keycode so there's not way to know if the form was submitted using the enter key. |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 22 replies
-
i am afraid you will need to do something at |
Beta Was this translation helpful? Give feedback.
-
I removed onSubmit from the form element and removed the type="submit" from my button. I then used getValues({ nest: true }), formState: { isValid }, and triggerValidation() to handle what handleSubmit would have done. |
Beta Was this translation helpful? Give feedback.
-
For those of you working on a multi-step form where Here is the quick-fix I came up with :
Simply add an EDIT in 2023:
|
Beta Was this translation helpful? Give feedback.
-
I found the simplest solution. Just add on all your input fields or controllers |
Beta Was this translation helpful? Give feedback.
-
To prevent inconsistent behaviour related to text areas, I have extended the solution provided by @ceroy-ak as follows. I have added a check for the element from which the event was raised. If it's a Callback function const preventEnterKeySubmission = (e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const target = e.target as HTMLInputElement | HTMLTextAreaElement;
if (e.key === "Enter" && !["TEXTAREA"].includes(target.tagName)) {
e.preventDefault();
}
}; And use on the form: <form {...otherProps} onKeyPress={preventEnterKeySubmission} /> |
Beta Was this translation helpful? Give feedback.
-
I'm facing this same issue for a simple input field with autocomplete. Maybe possibility to add it when we init the form with useForm would be a good solution ? |
Beta Was this translation helpful? Give feedback.
-
In addition to prevent form getting submitted, I want my focus to shift to next element. Any suggestion? |
Beta Was this translation helpful? Give feedback.
-
This is without Typescript. No need to do anything to the form itself, but I put a function on each input. In your component:
On your input elements:
or
React Hook Form is smart, but it also cause a ton of other problems that are hard to solve, I think. Maybe I'm just to much a noob to get HOW much better it is than handling forms with standard React. |
Beta Was this translation helpful? Give feedback.
-
That's the solution that worked for me: const handleKeyDown = (e: KeyboardEvent<HTMLFormElement>) => {
const { key, target } = e
if (key !== "Enter" || target instanceof HTMLTextAreaElement) {
return
}
e.preventDefault()
} <form {...otherProps} onKeyDown={handleKeyDown} /> |
Beta Was this translation helpful? Give feedback.
-
I also had this problem and went with a different solution - posting in case this helps anyone. I have a form that includes a "tags" input where pressing enter creates a new tag. Pressing enter was creating a new tag AND submitting the form, and I ended up moving the offending input outside of the I'd be the first to admit this isn't a great solution, particularly considering accessibility, as semantically the input is of course part of the form. But preventing submit on Enter is also not good for accessibility. Between me and my teammates we couldn't think of a solution with no downside. My solution now looks like this
|
Beta Was this translation helpful? Give feedback.
-
In my case, i want to use the "click enter to submit" behavior, but the form value is old value, not the latest value updated by input event of input element. So i do this. onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
if (submitButtonRef.current) {
submitButtonRef.current.focus();
submitButtonRef.current.click();
}
}
}} it works for me |
Beta Was this translation helpful? Give feedback.
I removed onSubmit from the form element and removed the type="submit" from my button. I then used getValues({ nest: true }), formState: { isValid }, and triggerValidation() to handle what handleSubmit would have done.