Open History to see the last commit
1import { useForm } from '@tanstack/react-form'
2import { type } from 'arktype'
3import * as React from 'react'
4import { createRoot } from 'react-dom/client'
5import * as v from 'valibot'
6import { z } from 'zod'
7import type { FieldApi } from '@tanstack/react-form'
8β
9function FieldInfo({ field }: { field: FieldApi<any, any, any, any> }) {
10 return (
11 <>
12 {field.state.meta.isTouched && field.state.meta.errors.length ? (
13 <em>{field.state.meta.errors.join(',')}</em>
14 ) : null}
15 {field.state.meta.isValidating ? 'Validating...' : null}
16 </>
17 )
18}
19β
20const ZodSchema = z.object({
21 firstName: z
22 .string()
23 .min(3, '[Zod] You must have a length of at least 3')
24 .startsWith('A', "[Zod] First name must start with 'A'"),
25 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
26})
27β
28const ValibotSchema = v.object({
29 firstName: v.pipe(
30 v.string(),
31 v.minLength(3, '[Valibot] You must have a length of at least 3'),
32 v.startsWith('A', "[Valibot] First name must start with 'A'"),
33 ),
34 lastName: v.pipe(
35 v.string(),
36 v.minLength(3, '[Valibot] You must have a length of at least 3'),
37 ),
38})
39β
40const ArkTypeSchema = type({
41 firstName: 'string >= 3',
42 lastName: 'string >= 3',
43})
44β
45export default function App() {
46 const form = useForm({
47 defaultValues: {
48 firstName: '',
49 lastName: '',
50 },
51 validators: {
52 // DEMO: You can switch between schemas seamlessly
53 onChange: ZodSchema,
54 // onChange: ValibotSchema,
55 // onChange: ArkTypeSchema,
56 },
57 onSubmit: async ({ value }) => {
58 // Do something with form data
59 console.log(value)
60 },
61 // DEMO: There's no need to pass an adapter! You may use it only if you need to pass custom options.
62 // validatorAdapter: standardSchemaValidator(),
63 // validatorAdapter: standardSchemaValidator({ transformErrors: (issues) => issues.map((issue) => issue.message)[0] }),
64 })
65β
66 return (
67 <div>
68 <h1>Standard Schema Form Example</h1>
69 <form
70 onSubmit={(e) => {
71 e.preventDefault()
72 e.stopPropagation()
73 form.handleSubmit()
74 }}
75 >
76 <div>
77 {/* A type-safe field component*/}
78 <form.Field
79 name="firstName"
80 children={(field) => {
81 // Avoid hasty abstractions. Render props are great!
82 return (
83 <>
84 <label htmlFor={field.name}>First Name:</label>
85 <input
86 id={field.name}
87 name={field.name}
88 value={field.state.value}
89 onBlur={field.handleBlur}
90 onChange={(e) => field.handleChange(e.target.value)}
91 />
92 <FieldInfo field={field} />
93 </>
94 )
95 }}
96 />
97 </div>
98 <div>
99 <form.Field
100 name="lastName"
101 children={(field) => (
102 <>
103 <label htmlFor={field.name}>Last Name:</label>
104 <input
105 id={field.name}
106 name={field.name}
107 value={field.state.value}
108 onBlur={field.handleBlur}
109 onChange={(e) => field.handleChange(e.target.value)}
110 />
111 <FieldInfo field={field} />
112 </>
113 )}
114 />
115 </div>
116 <form.Subscribe
117 selector={(state) => [state.canSubmit, state.isSubmitting]}
118 children={([canSubmit, isSubmitting]) => (
119 <button type="submit" disabled={!canSubmit}>
120 {isSubmitting ? '...' : 'Submit'}
121 </button>
122 )}
123 />
124 </form>
125 </div>
126 )
127}
128β
129const rootElement = document.getElementById('root')!
130β
131createRoot(rootElement).render(
132 <React.StrictMode>
133 <App />
134 </React.StrictMode>,
135)