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

Submit Form with javascript does not trigger onSubmit #833

Closed
tomitrescak opened this issue Nov 11, 2016 · 1 comment
Closed

Submit Form with javascript does not trigger onSubmit #833

tomitrescak opened this issue Nov 11, 2016 · 1 comment
Labels

Comments

@tomitrescak
Copy link
Contributor

Is it possible to submit SUI form via javascript?
I tried pretty much everything I could but the form is not launching the onSubmit.
The only thing that works is to find the submit button and click() on it.

I tried

<Form name="myForm" .... />
and
document.myForm.submit();

But this does not trigger the onSubmit behaviour.

WHY I need it? I have very large forms and I have keyboard shortcuts that allow to save the form.

@tomitrescak tomitrescak changed the title Submit Form Submit Form with javascript does not trigger onSubmit Nov 11, 2016
@levithomason
Copy link
Member

levithomason commented Nov 11, 2016

I've never tried submitting React forms like that, but apparently, React does not call onSubmit when you try to submit the form through the DOM. This makes some sense, as you are trying to invoke a virtual DOM method (onSubmit) using the real DOM form and API. I am betting this doesn't work because the real DOM is only the output of the component, not the actual component itself. Here is a vanilla React example that shows React simply does not support this:

http://codepen.io/levithomason/pen/JbGpwR

I think the more "React way" of doing this would be to use controlled form controls that hoist their values into the component state or app state using their onChange listener. Then, your onSubmit handler just uses the data from state to do what it needs to do. Finally, you can invoke what ever function you passed to onSubmit directly and achieve the same results as a submission.

Hope that helps!

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

No branches or pull requests

2 participants