-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathcreateFormHook.test-d.tsx
More file actions
287 lines (251 loc) · 7.1 KB
/
createFormHook.test-d.tsx
File metadata and controls
287 lines (251 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { describe, expectTypeOf, it } from 'vitest'
import { formOptions } from '@tanstack/form-core'
import { createFormHook, createFormHookContexts } from '../src'
import type { JSX } from 'solid-js/jsx-runtime'
const { fieldContext, useFieldContext, formContext, useFormContext } =
createFormHookContexts()
function Test() {
return null
}
const { useAppForm, withForm, withFieldGroup } = createFormHook({
fieldComponents: {
Test,
},
formComponents: {
Test,
},
fieldContext,
formContext,
})
describe('createFormHook', () => {
it('should not break with an infinite type on large schemas', () => {
const ActivityKind0_Names = ['Work', 'Rest', 'OnCall'] as const
type ActivityKind0 = (typeof ActivityKind0_Names)[number]
enum DayOfWeek {
Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
interface Branding<Brand> {
__type?: Brand
}
type Branded<T, Brand> = T & Branding<Brand>
type ActivityId = Branded<string, 'ActivityId'>
interface ActivitySelectorFormData {
includeAll: boolean
includeActivityIds: ActivityId[]
includeActivityKinds: Set<ActivityKind0>
excludeActivityIds: ActivityId[]
}
const GeneratedTypes0Visibility_Names = [
'Normal',
'Advanced',
'Hidden',
] as const
type GeneratedTypes0Visibility =
(typeof GeneratedTypes0Visibility_Names)[number]
interface FormValuesBase {
key: string
visibility: GeneratedTypes0Visibility
}
interface ActivityCountFormValues extends FormValuesBase {
_type: 'ActivityCount'
activitySelector: ActivitySelectorFormData
daysOfWeek: DayOfWeek[]
label: string
}
interface PlanningTimesFormValues extends FormValuesBase {
_type: 'PlanningTimes'
showTarget: boolean
showPlanned: boolean
showDiff: boolean
}
type EditorValues = ActivityCountFormValues | PlanningTimesFormValues
interface EditorFormValues<EditorType extends EditorValues = EditorValues> {
editors: Record<string, EditorType>
ordering: string[]
}
const ExampleUsage = withForm({
props: {
initialValues: '' as keyof EditorFormValues['editors'],
},
defaultValues: {} as EditorFormValues,
render: ({ form, initialValues }) => {
return (
<div class="m-3">
<form.AppField name={`editors.${initialValues}.key` as const}>
{(field) => {
expectTypeOf(field().state.value).toExtend<string>()
return null
}}
</form.AppField>
<form.AppForm>
<form.Test />
</form.AppForm>
</div>
)
},
})
})
it('types should be properly inferred when using formOptions', () => {
type Person = {
firstName: string
lastName: string
}
const formOpts = formOptions({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
} as Person,
})
const WithFormComponent = withForm({
...formOpts,
render: ({ form }) => {
expectTypeOf(form.state.values).toEqualTypeOf<Person>()
return <form.Test />
},
})
})
it('types should be properly inferred when passing args alongside formOptions', () => {
type Person = {
firstName: string
lastName: string
}
const formOpts = formOptions({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
} as Person,
})
const WithFormComponent = withForm({
...formOpts,
onSubmitMeta: {
test: 'test',
},
render: ({ form }) => {
expectTypeOf(form.handleSubmit).toEqualTypeOf<{
(): Promise<void>
(submitMeta: { test: string }): Promise<void>
}>
return <form.Test />
},
})
})
it('types should be properly inferred when formOptions are being overridden', () => {
type Person = {
firstName: string
lastName: string
}
type PersonWithAge = Person & {
age: number
}
const formOpts = formOptions({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
} as Person,
})
const WithFormComponent = withForm({
...formOpts,
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
age: 10,
},
render: ({ form }) => {
expectTypeOf(form.state.values).toExtend<PersonWithAge>()
return <form.Test />
},
})
})
it('withForm props should be properly inferred', () => {
const WithFormComponent = withForm({
props: {
prop1: 'test',
prop2: 10,
},
render: ({ form, ...props }) => {
expectTypeOf(props).toEqualTypeOf<{
prop1: string
prop2: number
children?: JSX.Element
}>()
return <form.Test />
},
})
})
it("component made from withForm should have it's props properly typed", () => {
const formOpts = formOptions({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
},
})
const appForm = useAppForm(() => formOpts)
const WithFormComponent = withForm({
...formOpts,
props: {
prop1: 'test',
prop2: 10,
},
render: ({ form, children, ...props }) => {
expectTypeOf(props).toEqualTypeOf<{
prop1: string
prop2: number
}>()
return <form.Test />
},
})
const CorrectComponent = (
<WithFormComponent form={appForm} prop1="test" prop2={10} />
)
// @ts-expect-error Missing required props prop1 and prop2
const MissingPropsComponent = <WithFormComponent form={appForm} />
const incorrectFormOpts = formOptions({
defaultValues: {
firstName: 'FirstName',
lastName: 'LastName',
firstNameWrong: 'FirstName',
lastNameWrong: 'LastName',
},
})
const incorrectAppForm = useAppForm(() => incorrectFormOpts)
const IncorrectFormOptsComponent = (
// @ts-expect-error Incorrect form opts
<WithFormComponent form={incorrectAppForm} prop1="test" prop2={10} />
)
})
it('withFieldGroup should reject nullish-only field-group paths', () => {
const groupFields = {
name: '',
}
type WrapperValues = {
namespace3: { name: string } | null | undefined
nope: null | undefined
}
const defaultValues: WrapperValues = {
namespace3: null,
nope: null,
}
const FieldGroup = withFieldGroup({
defaultValues: groupFields,
render: function Render({ group }) {
expectTypeOf(group.state.values).toEqualTypeOf<{
name: string
}>()
expectTypeOf(group.state.values.name).toEqualTypeOf<string>()
return null
},
})
const form = useAppForm(() => ({
defaultValues,
}))
const Component = <FieldGroup form={form} fields="namespace3" />
// @ts-expect-error nullish-only fields cannot produce the group shape
const WrongComponent = <FieldGroup form={form} fields="nope" />
})
})