-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Possible to prevent form submit on "enter" keyPress? #1597
Comments
Have you tried with https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation |
@juliankigwana yeah tried that, no change unfortunately |
I came across this issue when I was trying to make my Form accessible (pressing enter on a dropdown option shouldn't automatically submit the form, but choose the selected value). This is what I did to resolve it:
With this hack, users can still use the enter key to get the default behavior from a field, button, dropdown, etc.. |
^^ it might be nice if we could add this hack to the library -- maybe a prop on the form such as |
A boolean |
Exactly. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you. |
Prerequisites
Description
I am trying to prevent the form's onSubmit method from being called when I press the enter key inside of an input field in the form itself.
The use case for this is I have a custom field for arrays. I render only 1 input box with an "add" button. When a user types something into the input box and then hits the add button, I append the item to the formData/call the props.onChange and then render the formData as "buttons"/badges below the input field (instead of rendering multiple input fields for an array). Having the user click the add button works but I also wanted to update the formData if the user presses enter inside of that input box. What's happening is when a user presses enter, it does add the item to the formData BUT ALSO calls the form onSubmit method...which if the rest of the form isn't filled out, fails.
Steps to Reproduce
Code sample below:
Expected behavior
If a user is inside of an input for an "array" field and they press enter, I want to prevent form submission and instead add the item to formData
Actual behavior
If a user is inside of an input for an "array" field and they press enter, it adds the item to the formData but also does a form submission. Doing a
e.preventDefault()
doesn't seem to prevent the form submission. Is hijacking the keydown event in an eventListener the only way to do this or is there a possibility of introducing the onKeyPress as a prop in the library?Version
"react-jsonschema-form": "^1.8.1",
The text was updated successfully, but these errors were encountered: