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

Imperative form submit method #11801

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

bombillazo
Copy link

@bombillazo bombillazo commented Apr 19, 2024

Hello.

We've created a patch for this library, which has worked wonderfully. This change allows users to submit forms imperatively with a new submit function returned by the form hook, a feature that users have long requested (#566). This is done by giving the form component a unique ID that is stored within the form context of the RHF instance.

Previously, one had to restructure their components by wrapping items and buttons into the form context to gain access to the handleSubmit function and trigger it manually. Now, we can do this:

import { useForm } from 'react-hook-form';

export const MyComponent: FC = () => {
  const form = useForm({ 
    id: 'my-new-form', // optional 
  });

  const onSubmit = () => {
  // some summit logic
  }

  return (
    <>
      <Button
        onClick={() => {
          form.submit();
        }}>
        Submit
      </Button>
      <form
        id={form.id}
        onSubmit={handleSubmit(onSubmit)}>
        ...
      </form>
    </>
    );

}

The beauty is that one can trigger the form from any component with the form.id, which is now returned by the useFormContext(); hook. You can also specify your own ID value for the form if you want to have control over it, but if not then RHF will generate a UUID.

Copy link

codesandbox bot commented Apr 19, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@bombillazo
Copy link
Author

It's failing due to an increased bundle size, up to 10.23KB. Not sure how to incorporate the UUID generation without affecting bundle size.

@bombillazo
Copy link
Author

Hey @bluebill1049 , I believe this is a feature that could benefit many users as well, I'd love your feedback for it when possible.

@bluebill1049
Copy link
Member

bluebill1049 commented May 8, 2024

hi, could we just do this?

<form onSubmit={handleSubmit(....)}>
    <input type="text" name="name" />
</form>

<input type="submit" form="myform" value="Update"/>

@bombillazo
Copy link
Author

bombillazo commented May 10, 2024

Hey @bluebill1049 , that assumes we only want to trigger a submit with a button click. But we may want to submit a form programmatically by other means. Also, having the submit input/button outside the form only works when setting up the form with the proper ID (this PR takes care of that).

By adding id and the submit function to the form context, you can access them both to call submit from any component that does not have direct access to the form object. With this change, any logic can trigger a submit event.

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

Successfully merging this pull request may close these issues.

None yet

2 participants