Open History to see the last commit
1import { expectTypeOf } from 'vitest'
2import {
3 createForm,
4 createFormHook,
5 defineFieldGroup,
6 formOptions,
7 getFormHookHelpers,
8} from '../src'
9import type { Accessor } from 'solid-js'
10import type { FieldWithValue, SolidFormType, ValidationIssue } from '../src'
11β
12function SubscribeTypes() {
13 const form = createForm(() => ({
14 defaultValues: { name: '' },
15 }))
16β
17 return (
18 <form.Subscribe
19 selector={(state) => [state.values.name, state.submissionAttempts]}
20 >
21 {(selected) => {
22 expectTypeOf(selected()).toEqualTypeOf<readonly [string, number]>()
23 return null
24 }}
25 </form.Subscribe>
26 )
27}
28β
29function FormAndGroupTypes() {
30 const form = createForm(() => ({
31 defaultValues: {
32 guest: { name: '', emails: [''] },
33 budget: 0,
34 },
35 validators: [
36 {
37 triggers: [],
38 run: () => ({ form: { message: 'form' }, fields: {} }),
39 },
40 ],
41 onSubmit: () => 'submitted' as const,
42 }))
43β
44 return (
45 <form.FormGroup
46 name="guest"
47 validators={[
48 {
49 triggers: [],
50 run: ({ value }) => {
51 expectTypeOf(value.name).toEqualTypeOf<string>()
52 // @ts-expect-error group values do not expose root fields
53 value.budget
54 return {
55 fields: {
56 name: { message: '', source: 'group' as const },
57 },
58 }
59 },
60 },
61 ]}
62 >
63 {(group) => {
64 expectTypeOf(group().state.values.name).toEqualTypeOf<string>()
65 expectTypeOf(group().form.state.values.budget).toEqualTypeOf<number>()
66 // @ts-expect-error group state is scoped to the group value
67 group().state.values.budget
68β
69 return (
70 <>
71 <group.Field name="name">
72 {(field) => {
73 expectTypeOf(field().value).toEqualTypeOf<string>()
74 expectTypeOf(field().errors).toMatchTypeOf<
75 Array<ValidationIssue>
76 >()
77 return null
78 }}
79 </group.Field>
80 <group.ArrayField name="emails">{() => null}</group.ArrayField>
81 {/* @ts-expect-error field names are relative to the group */}
82 <group.Field name="budget">{() => null}</group.Field>
83 {/* @ts-expect-error ArrayField only accepts array values */}
84 <group.ArrayField name="name">{() => null}</group.ArrayField>
85 <group.Subscribe selector={(state) => state.values.name}>
86 {(name) => {
87 expectTypeOf(name).toEqualTypeOf<Accessor<string>>()
88 return null
89 }}
90 </group.Subscribe>
91 </>
92 )
93 }}
94 </form.FormGroup>
95 )
96}
97β
98const sharedOptions = formOptions({
99 defaultValues: { email: '' },
100 validators: [],
101})
102type SharedForm = SolidFormType<typeof sharedOptions>
103β
104function SharedFormChild(props: { form: SharedForm }) {
105 return (
106 <props.form.Field name="email">
107 {(field) => {
108 expectTypeOf(field().errors).toEqualTypeOf<Array<ValidationIssue>>()
109 return null
110 }}
111 </props.form.Field>
112 )
113}
114β
115function TextField(props: {
116 field: Accessor<FieldWithValue<string>>
117 label: string
118}) {
119 return props.label
120}
121β
122function NumberField(props: { field: Accessor<FieldWithValue<number>> }) {
123 return String(props.field().value)
124}
125β
126const { fieldComponent } = getFormHookHelpers()
127const Text = fieldComponent.strict(TextField, 'field')
128const Number = fieldComponent.strict(NumberField, 'field')
129const { useAppForm, defineAppFieldGroup } = createFormHook({
130 fieldComponents: { Text, Number },
131 formComponents: {},
132})
133β
134function AppFormTypes() {
135 const form = useAppForm(() => ({
136 defaultValues: { name: '', age: 0 },
137 }))
138 return (
139 <>
140 <form.Field name="name">
141 {(field) => (
142 <>
143 <field.Text label="Name" />
144 {/* @ts-expect-error number-only component is filtered out */}
145 <field.Number />
146 </>
147 )}
148 </form.Field>
149 <form.Field name="age">
150 {(field) => (
151 <>
152 <field.Number />
153 {/* @ts-expect-error string-only component is filtered out */}
154 <field.Text label="Age" />
155 </>
156 )}
157 </form.Field>
158 </>
159 )
160}
161β
162const profileFieldGroup = defineFieldGroup(({ strict }) => ({
163 name: strict<string>(),
164 age: strict<number>(),
165}))
166β
167function ProfileFields(props: { fields: typeof profileFieldGroup.fields }) {
168 return (
169 <props.fields.Field name="name">
170 {(field) => {
171 expectTypeOf(field().value).toEqualTypeOf<string>()
172 return null
173 }}
174 </props.fields.Field>
175 )
176}
177β
178const Profile = profileFieldGroup.bindComponent(ProfileFields, 'fields')
179β
180function FieldGroupBindingsTypes() {
181 const form = createForm(() => ({
182 defaultValues: {
183 user: { name: '', age: 0 },
184 wrong: { name: 0, age: '' },
185 },
186 }))
187 const formWithIdentityBindings = createForm(() => ({
188 defaultValues: {
189 name: '',
190 age: 0,
191 alternate: { name: '', age: 0 },
192 },
193 }))
194β
195 return (
196 <>
197 <Profile form={formWithIdentityBindings} />
198 <Profile
199 form={formWithIdentityBindings}
200 fields={{ name: 'alternate.name', age: 'alternate.age' }}
201 />
202 <Profile
203 form={formWithIdentityBindings}
204 // @ts-expect-error optional field bindings still need every defined field
205 fields={{ name: 'name' }}
206 />
207 {/* @ts-expect-error non-identity field bindings remain required */}
208 <Profile form={form} />
209 <Profile form={form} fields={{ name: 'user.name', age: 'user.age' }} />
210 <Profile
211 form={form}
212 fields={{
213 // @ts-expect-error strict string slot rejects number fields
214 name: 'wrong.name',
215 // @ts-expect-error strict number slot rejects string fields
216 age: 'wrong.age',
217 }}
218 />
219 </>
220 )
221}
222β
223const appFieldGroup = defineAppFieldGroup(({ strict }) => ({
224 name: strict<string>(),
225}))
226β
227function AppFields(props: { fields: typeof appFieldGroup.fields }) {
228 return (
229 <props.fields.Field name="name">
230 {(field) => <field.Text label="Name" />}
231 </props.fields.Field>
232 )
233}
234β
235const BoundAppFields = appFieldGroup.bindComponent(AppFields, 'fields')
236β
237void FormAndGroupTypes
238void SubscribeTypes
239void SharedFormChild
240void AppFormTypes
241void FieldGroupBindingsTypes
242void AppFields
243void BoundAppFields