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