Skip to content

Commit 197a2a8

Browse files
harry-whorlowHarry Whorlowcrutchcorn
authored
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]>
1 parent 91b909f commit 197a2a8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

docs/config.json

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@
109109
"label": "Listeners",
110110
"to": "framework/react/guides/listeners"
111111
},
112+
{
113+
"label": "Submission Handling",
114+
"to": "framework/react/guides/submission-handling"
115+
},
112116
{
113117
"label": "UI Libraries",
114118
"to": "framework/react/guides/ui-libraries"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
id: submission-handling
3+
title: Submission handling
4+
---
5+
6+
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.
7+
8+
# Basic Usage
9+
10+
First you must define the default state of the form.onSubmitMeta prop:
11+
12+
```tsx
13+
const form = useForm({
14+
defaultValues: {
15+
firstName: 'Rick',
16+
},
17+
// {} is the default value passed to `onSubmit`'s `meta` property
18+
onSubmitMeta: {} as { lastName: string },
19+
onSubmit: async ({ value, meta }) => {
20+
21+
// Do something with the values passed via handleSubmit
22+
console.log(`${value.firstName} - ${meta}`)
23+
},
24+
})
25+
```
26+
27+
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.
28+
29+
Then when you call `onSubmit` you can provide it the predefined meta like so:
30+
31+
```tsx
32+
<form
33+
onSubmit={(e) => {
34+
e.preventDefault()
35+
e.stopPropagation()
36+
form.handleSubmit({
37+
lastName: 'Astley',
38+
})
39+
}}
40+
>
41+
</form>
42+
```

0 commit comments

Comments
 (0)