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 { AnyFieldApi } from '@tanstack/react-form'
8β
9function FieldInfo({ field }: { field: AnyFieldApi }) {
10 return (
11 <>
12 {field.state.meta.isTouched && field.state.meta.errors.length ? (
13 <em>{field.state.meta.errors.map((err) => err.message).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 })
62β
63 return (
64 <div>
65 <h1>Standard Schema Form Example</h1>
66 <form
67 onSubmit={(e) => {
68 e.preventDefault()
69 e.stopPropagation()
70 form.handleSubmit()
71 }}
72 >
73 <div>
74 {/* A type-safe field component*/}
75 <form.Field
76 name="firstName"
77 children={(field) => {
78 // Avoid hasty abstractions. Render props are great!
79 return (
80 <>
81 <label htmlFor={field.name}>First Name:</label>
82 <input
83 id={field.name}
84 name={field.name}
85 value={field.state.value}
86 onBlur={field.handleBlur}
87 onChange={(e) => field.handleChange(e.target.value)}
88 />
89 <FieldInfo field={field} />
90 </>
91 )
92 }}
93 />
94 </div>
95 <div>
96 <form.Field
97 name="lastName"
98 children={(field) => (
99 <>
100 <label htmlFor={field.name}>Last Name:</label>
101 <input
102 id={field.name}
103 name={field.name}
104 value={field.state.value}
105 onBlur={field.handleBlur}
106 onChange={(e) => field.handleChange(e.target.value)}
107 />
108 <FieldInfo field={field} />
109 </>
110 )}
111 />
112 </div>
113 <form.Subscribe
114 selector={(state) => [state.canSubmit, state.isSubmitting]}
115 children={([canSubmit, isSubmitting]) => (
116 <button type="submit" disabled={!canSubmit}>
117 {isSubmitting ? '...' : 'Submit'}
118 </button>
119 )}
120 />
121 </form>
122 </div>
123 )
124}
125β
126const rootElement = document.getElementById('root')!
127β
128createRoot(rootElement).render(
129 <React.StrictMode>
130 <App />
131 </React.StrictMode>,
132)