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

Allow programmatically submit #17

Closed
gustavoguichard opened this issue May 6, 2022 · 9 comments
Closed

Allow programmatically submit #17

gustavoguichard opened this issue May 6, 2022 · 9 comments
Labels
enhancement New feature or request

Comments

@gustavoguichard
Copy link
Contributor

No description provided.

@felipefreitag felipefreitag added the enhancement New feature or request label Jul 15, 2022
@azzzy
Copy link

azzzy commented Sep 24, 2022

I also need this in order to be able to reset the form.
Maybe a low hanging fruit will be to simply add a an optional Ref<HtmlFormElement> prop to the component? This won't lead to a breaking change, unlike using forwardRef. This is how I am monkey-patching at the moment.
form-ref.txt

@danielweinmann
Copy link
Contributor

@azzzy, have you tried using the onTransition prop for resetting the form? We do that in this example.

Since we added onTransition I haven't had any other use case where accessing the form imperatively is necessary. How about your use cases?

@azzzy
Copy link

azzzy commented Oct 8, 2022

@danielweinmann, this works nicely, somehow I missed it in the documentation. Thanks for the hint!

@diogob
Copy link
Contributor

diogob commented Nov 7, 2022

@danielweinmann talking to @gustavoguichard we found a use case for this, when one wants to imperatively trigger the form submission (let's say when a verification code is typed). But instead of relaying the form ref we could expose a submit function for the form children function. Thoughts?

@diogob
Copy link
Contributor

diogob commented Nov 14, 2022

We just found another use case for programmatically submitting the form: submit confirmation using a dialog.

@diogob diogob changed the title Allow passing ref to Form and forwarding it to component Allow programmatically submit Nov 14, 2022
@paulm17
Copy link

paulm17 commented Dec 31, 2022

I needed this ability myself. I have a complex component where it has a dialog modal that has it's own onClick methods and where the react-hook-form resides in it's body. There is no way to connect the forms handleSubmit and modal onClick methods.

I have a really old version of remix-forms which I forked a while ago and not had the time to update it. Well, because it works so well. 😄

All I ended up doing was the following to the Form component for it to support a ref property:

function Form<Schema extends SomeZodObject>(
  {
    mode = "onSubmit",
    ...
    ...props
  }: FormProps<Schema>,
  ref,
) {
   return (
    <form
      ref={ref}
      ...
    />
}

export default forwardRef(Form)

Then the following to execute programmatically:

if (formRef.current) {
   ;(formRef.current as EventTarget).dispatchEvent(
      new Event("submit", { cancelable: true, bubbles: true }),
      )
}

@diogob
Copy link
Contributor

diogob commented Jan 3, 2023

@paulm17 I'm curious to know if the solution in #136 would solve your use case. We have exposed a submit function that triggers the submit event, but that is only being exposed in the form children function.

@paulm17
Copy link

paulm17 commented Jan 3, 2023

@diogob Although I took a look at the solution in #136 I am not quite sure. That said, once I have finished the current work that I am going through. I will be bringing up my fork of remix-forms to the latest and then I'll see where things stand. Maybe in a week or so, I'll then be able to comment here on any improvements or it's perfect for my use case. 😄.

Many thanks for being reactive and making a PR so quickly for this amazing library. Looking forward to comes next in '23. 🚀

@diogob
Copy link
Contributor

diogob commented Jan 5, 2023

I'm closing this issue since #136 exposes a submit function to the children function and it was released in v1.5.0.

@paulm17 feel free to open a new issue in case you need access to submit outside of the children function.

@diogob diogob closed this as completed Jan 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants