Skip to content
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

Bug: Using dispatchEvent 'submit' CustomEvent failure to trigger form onSubmit #20151

Closed
EastblueOkay opened this issue Nov 3, 2020 · 5 comments

Comments

@EastblueOkay
Copy link

React version: 17+

Steps To Reproduce

1.form add onSubmit props
2.dispatchEvent a CustomEvent with 'submit' name

Link to code example: https://codesandbox.io/s/admiring-almeida-3b26k?file=/src/App.js

The current behavior

failure to trigger form onSubmit

The expected behavior

onSubmit can be successfully triggered until 17.

@EastblueOkay EastblueOkay added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Nov 3, 2020
@lsky-walt
Copy link

same too 🤩

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 3, 2020

Thanks for the report.

A submit event has to bubble: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit_event. React handled the non-bubbling case in 16 but I think the change in behavior is correct. What's the reason to not let this event bubble and use a CustomEvent instead of Event?

But you don't need to dispatch custom events. If you convert your <button type="button" /> to <button type="submit" /> and event.preventDefault() in your submit handler, you should have the same behavior.

@eps1lon eps1lon added Component: DOM Resolution: Needs More Information and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Nov 3, 2020
@EastblueOkay
Copy link
Author

@eps1lon Hi, my scenario is click on a button outside the form to trigger a submit.
The form is in the body of the modal box and the submit button is in the footer of the modal box.
That's why I need to trigger the form submission manually.

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 3, 2020

That's why I need to trigger the form submission manually.

I hope you're aware that dispatching the submit events does not trigger form validation. You could get a ref to the form and run form.requestSubmit() (for form validation) or form.submit() (no form validation).

What's the reason you need a non-bubbling CustomEvent?

Note: Though it still might be problematic that non-bubbling submit events don't work since submit events don't bubble by spec. Though they might in all the supported browsers.

@EastblueOkay
Copy link
Author

Thank you for your answer.
Looks like I was mistaken, I originally thought there was no reason for Summit to bubble.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants