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

index.tsx

156 lines4.0 KB

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