Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

App.tsx

178 lines4.6 KB

1import { useForm } from '@tanstack/react-form'
2​
3import type { AnyFieldApi } from '@tanstack/react-form'
4​
5function FieldInfo({ field }: { field: AnyFieldApi }) {
6 return (
7 <>
8 {field.state.meta.isTouched && !field.state.meta.isValid ? (
9 <em>{field.state.meta.errors.join(',')}</em>
10 ) : null}
11 {field.state.meta.isValidating ? 'Validating...' : null}
12 </>
13 )
15​
16export default function App() {
17 const form1 = useForm({
18 defaultValues: {
19 firstName: '',
20 lastName: '',
21 },
22 onSubmit: async ({ value }) => {
23 console.log(value)
24 },
25 })
26​
27 const form2 = useForm({
28 defaultValues: {
29 age: 0,
30 },
31 validators: {
32 onChange({ value }) {
33 if (value.age < 15) {
34 return 'needs to be above 15'
35 }
36 return undefined
37 },
38 },
39 onSubmit: async ({ value }) => {
40 if (value.age < 20) throw 'needs to be above 20'
41 },
42 })
43​
44 return (
45 <div>
46 <h1>Form Devtools Example</h1>
47​
48 <h2>Form 1</h2>
49​
50 <form
51 onSubmit={(e) => {
52 e.preventDefault()
53 e.stopPropagation()
54 form1.handleSubmit()
55 }}
56 >
57 <div>
58 <form1.Field
59 name="firstName"
60 validators={{
61 onChange: ({ value }) =>
62 !value
63 ? 'A first name is required'
64 : value.length < 3
65 ? 'First name must be at least 3 characters'
66 : undefined,
67 }}
68 children={(field) => {
69 return (
70 <>
71 <label htmlFor={field.name}>First Name:</label>
72 <input
73 id={field.name}
74 name={field.name}
75 value={field.state.value}
76 onBlur={field.handleBlur}
77 onChange={(e) => field.handleChange(e.target.value)}
78 />
79 <FieldInfo field={field} />
80 </>
81 )
82 }}
83 />
84 </div>
85​
86 <div>
87 <form1.Field
88 name="lastName"
89 children={(field) => (
90 <>
91 <label htmlFor={field.name}>Last Name:</label>
92 <input
93 id={field.name}
94 name={field.name}
95 value={field.state.value}
96 onBlur={field.handleBlur}
97 onChange={(e) => field.handleChange(e.target.value)}
98 />
99 <FieldInfo field={field} />
100 </>
101 )}
102 />
103 </div>
104​
105 <form1.Subscribe
106 selector={(state) => [state.canSubmit, state.isSubmitting]}
107 children={([canSubmit, isSubmitting]) => (
108 <>
109 <button type="submit" disabled={!canSubmit}>
110 {isSubmitting ? '...' : 'Submit'}
111 </button>
112 <button
113 type="reset"
114 onClick={(e) => {
115 e.preventDefault()
116 form1.reset()
117 }}
118 >
119 Reset
120 </button>
121 </>
122 )}
123 />
124​
125 <form1.Subscribe
126 selector={(state) => [state.submissionAttempts]}
127 children={([submissionAttempts]) => (
128 <div>submission attempts: {submissionAttempts}</div>
129 )}
130 />
131 </form>
132​
133 <h2>Form 2</h2>
134 <form
135 onSubmit={(e) => {
136 e.preventDefault()
137 e.stopPropagation()
138 form2.handleSubmit()
139 }}
140 >
141 <div>
142 <form2.Field
143 name="age"
144 validators={{
145 onChange: ({ value }) =>
146 value < 10 ? 'Age must be at least 10' : undefined,
147 }}
148 children={(field) => {
149 return (
150 <>
151 <label htmlFor={field.name}>age:</label>
152 <input
153 type="number"
154 id={field.name}
155 name={field.name}
156 value={field.state.value}
157 onBlur={field.handleBlur}
158 onChange={(e) =>
159 field.handleChange(parseInt(e.target.value))
160 }
161 />
162 <FieldInfo field={field} />
163 </>
164 )
165 }}
166 />
167​
168 <form2.Subscribe
169 selector={(state) => [state.submissionAttempts]}
170 children={([submissionAttempts]) => (
171 <div>submission attempts: {submissionAttempts}</div>
172 )}
173 />
174 </div>
175 </form>
176 </div>
177 )