File tree Expand file tree Collapse file tree 3 files changed +32
-17
lines changed Expand file tree Collapse file tree 3 files changed +32
-17
lines changed Original file line number Diff line number Diff line change @@ -12,12 +12,15 @@ You can create options for your form so that it can be shared between multiple f
12
12
Example:
13
13
14
14
``` tsx
15
+ interface User {
16
+ firstName: string
17
+ lastName: string
18
+ hobbies: Array <string >
19
+ }
20
+ const defaultUser: User = { firstName: ' ' , lastName: ' ' , hobbies: [] }
21
+
15
22
const formOpts = formOptions ({
16
- defaultValues: {
17
- firstName: ' ' ,
18
- lastName: ' ' ,
19
- hobbies: [],
20
- } as Person ,
23
+ defaultValues: defaultUser ,
21
24
})
22
25
```
23
26
@@ -38,16 +41,19 @@ const form = useForm({
38
41
You may also create a form instance without using ` formOptions ` by using the standalone ` useForm ` API:
39
42
40
43
``` tsx
44
+ interface User {
45
+ firstName: string
46
+ lastName: string
47
+ hobbies: Array <string >
48
+ }
49
+ const defaultUser: User = { firstName: ' ' , lastName: ' ' , hobbies: [] }
50
+
41
51
const form = useForm ({
52
+ defaultValues: defaultUser ,
42
53
onSubmit : async ({ value }) => {
43
54
// Do something with form data
44
55
console .log (value )
45
56
},
46
- defaultValues: {
47
- firstName: ' ' ,
48
- lastName: ' ' ,
49
- hobbies: [],
50
- } as Person ,
51
57
})
52
58
```
53
59
Original file line number Diff line number Diff line change @@ -52,11 +52,15 @@ useForm<MyForm>()
52
52
You should do:
53
53
54
54
``` typescript
55
+ interface Person {
56
+ name: string
57
+ age: number
58
+ }
59
+
60
+ const defaultPerson: Person = { name: ' Bill Luo' , age: 24 }
61
+
55
62
useForm ({
56
- defaultValues: {
57
- name: ' Bill Luo' ,
58
- age: 24 ,
59
- } as MyForm ,
63
+ defaultValues: defaultPerson ,
60
64
})
61
65
```
62
66
Original file line number Diff line number Diff line change @@ -2,11 +2,16 @@ import * as React from 'react'
2
2
import { createRoot } from 'react-dom/client'
3
3
import { useForm } from '@tanstack/react-form'
4
4
5
+ interface Person {
6
+ name : string
7
+ age : number
8
+ }
9
+
10
+ const defaultPeople : { people : Array < Person > } = { people : [ ] }
11
+
5
12
function App ( ) {
6
13
const form = useForm ( {
7
- defaultValues : {
8
- people : [ ] as Array < { name : string ; age : number } > ,
9
- } ,
14
+ defaultValues : defaultPeople ,
10
15
onSubmit ( { value } ) {
11
16
alert ( JSON . stringify ( value ) )
12
17
} ,
You can’t perform that action at this time.
0 commit comments