Open History to see the last commit
1import * as React from 'react'
2import { createRoot } from 'react-dom/client'
3β
4import { type } from 'arktype'
5import { Schema as S } from 'effect'
6import * as v from 'valibot'
7import { z } from 'zod'
8β
9import { TanStackDevtools } from '@tanstack/react-devtools'
10import { formDevtoolsPlugin } from '@tanstack/react-form-devtools'
11import { useForm } from '@tanstack/react-form'
12β
13import type { AnyFieldApi } from '@tanstack/react-form'
14β
15function FieldInfo({ field }: { field: AnyFieldApi }) {
16 return (
17 <>
18 {field.meta.isTouched && field.meta.isInvalid ? (
19 <em>{field.errors.map((err) => err.message).join(',')}</em>
20 ) : null}
21 {field.meta.isValidating ? 'Validating...' : null}
22 </>
23 )
24}
25β
26const ZodSchema = z.object({
27 firstName: z
28 .string()
29 .min(3, '[Zod] You must have a length of at least 3')
30 .startsWith('A', "[Zod] First name must start with 'A'"),
31 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
32})
33β
34const ValibotSchema = v.object({
35 firstName: v.pipe(
36 v.string(),
37 v.minLength(3, '[Valibot] You must have a length of at least 3'),
38 v.startsWith('A', "[Valibot] First name must start with 'A'"),
39 ),
40 lastName: v.pipe(
41 v.string(),
42 v.minLength(3, '[Valibot] You must have a length of at least 3'),
43 ),
44})
45β
46const ArkTypeSchema = type({
47 firstName: 'string >= 3',
48 lastName: 'string >= 3',
49})
50β
51const EffectSchema = S.standardSchemaV1(
52 S.Struct({
53 firstName: S.String.pipe(
54 S.minLength(3),
55 S.annotations({
56 message: () => '[Effect/Schema] You must have a length of at least 3',
57 }),
58 ),
59 lastName: S.String.pipe(
60 S.minLength(3),
61 S.annotations({
62 message: () => '[Effect/Schema] You must have a length of at least 3',
63 }),
64 ),
65 }),
66)
67β
68export default function App() {
69 const form = useForm({
70 defaultValues: {
71 firstName: '',
72 lastName: '',
73 },
74 validators: [
75 {
76 // DEMO: You can switch between schemas seamlessly
77 run: ZodSchema,
78 // run: ValibotSchema,
79 // run: ArkTypeSchema,
80 // run: EffectSchema,
81 triggers: ['change'],
82 },
83 ],
84 onSubmit: async ({ value }) => {
85 // Do something with form data
86 console.log(value)
87 },
88 })
89β
90 return (
91 <div>
92 <h1>Standard Schema Form Example</h1>
93 <form
94 onSubmit={(e) => {
95 e.preventDefault()
96 e.stopPropagation()
97 form.handleSubmit()
98 }}
99 >
100 <div>
101 {/* A type-safe field component*/}
102 <form.Field name="firstName">
103 {(field) => {
104 // Avoid hasty abstractions. Render props are great!
105 return (
106 <>
107 <label htmlFor={field.name}>First Name:</label>
108 <input
109 id={field.name}
110 name={field.name}
111 value={field.value}
112 onBlur={field.handleBlur}
113 onChange={(e) => field.handleChange(e.target.value)}
114 />
115 <FieldInfo field={field} />
116 </>
117 )
118 }}
119 </form.Field>
120 </div>
121 <div>
122 <form.Field name="lastName">
123 {(field) => (
124 <>
125 <label htmlFor={field.name}>Last Name:</label>
126 <input
127 id={field.name}
128 name={field.name}
129 value={field.value}
130 onBlur={field.handleBlur}
131 onChange={(e) => field.handleChange(e.target.value)}
132 />
133 <FieldInfo field={field} />
134 </>
135 )}
136 </form.Field>
137 </div>
138 <form.Subscribe
139 selector={(state) => [state.canSubmit, state.isSubmitting]}
140 >
141 {([canSubmit, isSubmitting]) => (
142 <button type="submit" disabled={!canSubmit}>
143 {isSubmitting ? '...' : 'Submit'}
144 </button>
145 )}
146 </form.Subscribe>
147 </form>
148 </div>
149 )
150}
151β
152const rootElement = document.getElementById('root')!
153β
154createRoot(rootElement).render(
155 <React.StrictMode>
156 <App />
157β
158 <TanStackDevtools
159 config={{ hideUntilHover: true }}
160 plugins={[formDevtoolsPlugin()]}
161 />
162 </React.StrictMode>,
163)