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

index.tsx

161 lines4.3 KB

1import { For, Show } from 'solid-js'
2import { render } from 'solid-js/web'
3import { createForm } from '@tanstack/solid-form'
4import { type } from 'arktype'
5import { Schema as S } from 'effect'
6import * as v from 'valibot'
7import { z } from 'zod'
8import type { Accessor } from 'solid-js'
9import type { AnyFieldApi } from '@tanstack/solid-form'
10​
11function FieldInfo(props: { field: Accessor<AnyFieldApi> }) {
12 return (
13 <>
14 <Show when={props.field().meta.isTouched && props.field().meta.isInvalid}>
15 <em role="alert">
16 <For each={props.field().errors}>
17 {(error, index) => (
18 <>
19 {index() > 0 ? ', ' : ''}
20 {error.message}
21 </>
22 )}
23 </For>
24 </em>
25 </Show>
26 <Show when={props.field().meta.isValidating}>Validating...</Show>
27 </>
28 )
30​
31const ZodSchema = z.object({
32 firstName: z
33 .string()
34 .min(3, '[Zod] You must have a length of at least 3')
35 .startsWith('A', "[Zod] First name must start with 'A'"),
36 lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
37})
38​
39const ValibotSchema = v.object({
40 firstName: v.pipe(
41 v.string(),
42 v.minLength(3, '[Valibot] You must have a length of at least 3'),
43 v.startsWith('A', "[Valibot] First name must start with 'A'"),
44 ),
45 lastName: v.pipe(
46 v.string(),
47 v.minLength(3, '[Valibot] You must have a length of at least 3'),
48 ),
49})
50​
51const ArkTypeSchema = type({
52 firstName: 'string >= 3',
53 lastName: 'string >= 3',
54})
55​
56const EffectSchema = S.standardSchemaV1(
57 S.Struct({
58 firstName: S.String.pipe(
59 S.minLength(3),
60 S.annotations({
61 message: () => '[Effect/Schema] You must have a length of at least 3',
62 }),
63 ),
64 lastName: S.String.pipe(
65 S.minLength(3),
66 S.annotations({
67 message: () => '[Effect/Schema] You must have a length of at least 3',
68 }),
69 ),
70 }),
72​
73function App() {
74 const form = createForm(() => ({
75 defaultValues: {
76 firstName: '',
77 lastName: '',
78 },
79 validators: [
80 {
81 // Switch between these Standard Schema implementations seamlessly.
82 run: ZodSchema,
83 // run: ValibotSchema,
84 // run: ArkTypeSchema,
85 // run: EffectSchema,
86 triggers: ['change'],
87 },
88 ],
89 onSubmit: async ({ value }) => {
90 console.log(value)
91 },
92 }))
93​
94 return (
95 <div>
96 <h1>Standard Schema Form Example</h1>
97 <form
98 onSubmit={(event) => {
99 event.preventDefault()
100 event.stopPropagation()
101 form.handleSubmit()
102 }}
103 >
104 <div>
105 <form.Field name="firstName">
106 {(field) => (
107 <>
108 <label for={field().name}>First Name:</label>
109 <input
110 id={field().name}
111 name={field().name}
112 value={field().value}
113 onBlur={field().handleBlur}
114 onInput={(event) =>
115 field().handleChange(event.currentTarget.value)
116 }
117 aria-invalid={field().meta.isInvalid}
118 />
119 <FieldInfo field={field} />
120 </>
121 )}
122 </form.Field>
123 </div>
124 <div>
125 <form.Field name="lastName">
126 {(field) => (
127 <>
128 <label for={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={(event) =>
135 field().handleChange(event.currentTarget.value)
136 }
137 aria-invalid={field().meta.isInvalid}
138 />
139 <FieldInfo field={field} />
140 </>
141 )}
142 </form.Field>
143 </div>
144 <form.Subscribe
145 selector={(state) => [state.canSubmit, state.isSubmitting] as const}
146 >
147 {(state) => (
148 <button type="submit" disabled={!state()[0] || state()[1]}>
149 {state()[1] ? '...' : 'Submit'}
150 </button>
151 )}
152 </form.Subscribe>
153 </form>
154 </div>
155 )
157​
158void ValibotSchema
159void ArkTypeSchema
160void EffectSchema
161render(() => <App />, document.getElementById('root')!)