-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: onSubmitMeta and submission handling (#1189)
* feat(docs): onSubmitMeta and submission handling * docs: add link to config --------- Co-authored-by: Harry Whorlow <[email protected]> Co-authored-by: Corbin Crutchley <[email protected]>
- Loading branch information
1 parent
91b909f
commit 197a2a8
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
id: submission-handling | ||
title: Submission handling | ||
--- | ||
|
||
In a situation where you want to have multiple form submission types, for example, a form that has a button that navigates to a sub-form and another button that handles a standard submission, you can make use of the onSubmitMeta prop and the handleSubmit function overloads. | ||
|
||
# Basic Usage | ||
|
||
First you must define the default state of the form.onSubmitMeta prop: | ||
|
||
```tsx | ||
const form = useForm({ | ||
defaultValues: { | ||
firstName: 'Rick', | ||
}, | ||
// {} is the default value passed to `onSubmit`'s `meta` property | ||
onSubmitMeta: {} as { lastName: string }, | ||
onSubmit: async ({ value, meta }) => { | ||
|
||
// Do something with the values passed via handleSubmit | ||
console.log(`${value.firstName} - ${meta}`) | ||
}, | ||
}) | ||
``` | ||
|
||
Note: the default state of onSubmitMeta is `never`, so if the prop is not provided and you try to access it in `handleSubmit`, or `onSubmit` it will error. | ||
|
||
Then when you call `onSubmit` you can provide it the predefined meta like so: | ||
|
||
```tsx | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
form.handleSubmit({ | ||
lastName: 'Astley', | ||
}) | ||
}} | ||
> | ||
</form> | ||
``` |