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

Components.public.ts

451 lines10.7 KB

1import type {
2 DeepKeys,
3 DeepKeysWhereValueIncludes,
4 DeepValue,
5 FieldApi,
6 FieldApiOptions,
7 FieldValidators,
8 FormErrorTypes,
9 FormGroupApi,
10 FormGroupOptions,
11 FormGroupState,
12 FormGroupValidators,
13 FormState,
14 ToFieldError,
15 ToFormGroupErrorTypes,
16} from '@tanstack/form-core'
17import type { Component, PublicProps } from 'vue'
18import type { VueComponentInstance } from '../vueTypes.lib'
19​
20declare const fieldComponentsType: unique symbol
21​
22type ExactFieldBrand<out TValue> = {
23 readonly __tanstackFieldExactType: TValue
25​
26type AcceptsFieldBrand<out TAcceptedValue> = {
27 readonly __tanstackFieldAcceptsType: TAcceptedValue
29​
30type CompatibleFieldKey<TKey, TComponent, TTargetValue> =
31 TComponent extends ExactFieldBrand<infer TExact>
32 ? [TExact] extends [TTargetValue]
33 ? [TTargetValue] extends [TExact]
34 ? TKey
35 : never
36 : never
37 : TComponent extends AcceptsFieldBrand<infer TLoose>
38 ? [TTargetValue] extends [TLoose]
39 ? TKey
40 : never
41 : TKey
42​
43type FilteredFieldComponents<
44 TFieldComponents extends Record<string, Component>,
45 TTargetValue,
46> = {
47 [
48 K in keyof TFieldComponents as CompatibleFieldKey<
49 K,
50 TFieldComponents[K],
51 TTargetValue
52 >
53 ]: TFieldComponents[K]
55​
56type FieldComponentsMatchingType<
57 TFieldComponents extends Record<string, Component>,
58 TTargetValue,
59> = unknown extends TTargetValue
60 ? TFieldComponents
61 : string extends keyof TFieldComponents
62 ? TFieldComponents
63 : FilteredFieldComponents<TFieldComponents, TTargetValue>
64​
65export type VueFieldApi<
66 TFieldName,
67 TFieldValue,
68 TFieldError,
69 TFormData,
70 TFormErrorTypes extends FormErrorTypes,
71 TFieldComponents extends Record<string, Component>,
72> = FieldApi<TFieldName, TFieldValue, TFieldError, TFormData, TFormErrorTypes> &
73 FieldComponentsMatchingType<TFieldComponents, TFieldValue>
74​
75interface VueSubscribeProps<in out TSourceData, in out TSelected> {
76 selector: (state: TSourceData) => TSelected
77 when?: (selected: NoInfer<TSelected>) => boolean
79​
80export type VueFormSubscribeProps<
81 TFormData,
82 TFormErrorTypes extends FormErrorTypes,
83 TSelected,
84> = VueSubscribeProps<FormState<TFormData, TFormErrorTypes>, TSelected>
85​
86export type VueFormSubscribeComponent<
87 in out TFormData,
88 in out TFormErrorTypes extends FormErrorTypes,
89> = new <const TSelected>(
90 props: VueFormSubscribeProps<TFormData, TFormErrorTypes, TSelected> &
91 PublicProps,
92) => VueComponentInstance<
93 VueFormSubscribeProps<TFormData, TFormErrorTypes, TSelected>,
94 { default: NoInfer<TSelected> }
96​
97export interface VueFormFieldProps<
98 in out TFieldData,
99 in out TFieldName,
100 in out TFieldValue,
101 in out TFieldValidators extends FieldValidators<
102 TFieldData,
103 TFieldName,
104 TFieldValue
105 >,
106 in out TGroupFieldError,
107 in out TFormData,
108 in out TFormErrorTypes extends FormErrorTypes,
109 in out TFieldComponents extends Record<string, Component>,
110> extends FieldApiOptions<
111 TFieldData,
112 TFieldName,
113 TFieldValue,
114 TFieldValidators,
115 TGroupFieldError,
116 TFormData,
117 TFormErrorTypes
118> {
119 readonly [fieldComponentsType]?: TFieldComponents
121​
122export type VueFormFieldComponent<
123 in out TFormData,
124 in out TFormErrorTypes extends FormErrorTypes,
125 in out TFieldComponents extends Record<string, Component>,
126> = new <
127 TFieldName extends DeepKeys<TFormData>,
128 const TFieldValidators extends FieldValidators<
129 TFormData,
130 TFieldName,
131 DeepValue<TFormData, TFieldName>
132 >,
133>(
134 props: VueFormFieldProps<
135 TFormData,
136 TFieldName,
137 DeepValue<TFormData, TFieldName>,
138 TFieldValidators,
139 never,
140 TFormData,
141 TFormErrorTypes,
142 TFieldComponents
143 > &
144 PublicProps,
145) => VueComponentInstance<
146 VueFormFieldProps<
147 TFormData,
148 TFieldName,
149 DeepValue<TFormData, TFieldName>,
150 TFieldValidators,
151 never,
152 TFormData,
153 TFormErrorTypes,
154 TFieldComponents
155 >,
156 {
157 default: {
158 field: VueFieldApi<
159 TFieldName,
160 DeepValue<TFormData, TFieldName>,
161 ToFieldError<NoInfer<TFieldValidators>, never, TFormErrorTypes>,
162 TFormData,
163 TFormErrorTypes,
164 TFieldComponents
165 >
166 }
167 }
169​
170export type VueFormArrayFieldComponent<
171 in out TFormData,
172 in out TFormErrorTypes extends FormErrorTypes,
173 in out TFieldComponents extends Record<string, Component>,
174> = new <
175 TFieldName extends DeepKeysWhereValueIncludes<TFormData, Array<any>>,
176 const TFieldValidators extends FieldValidators<
177 TFormData,
178 TFieldName,
179 DeepValue<TFormData, TFieldName>
180 >,
181>(
182 props: VueFormFieldProps<
183 TFormData,
184 TFieldName,
185 DeepValue<TFormData, TFieldName>,
186 TFieldValidators,
187 never,
188 TFormData,
189 TFormErrorTypes,
190 TFieldComponents
191 > &
192 PublicProps,
193) => VueComponentInstance<
194 VueFormFieldProps<
195 TFormData,
196 TFieldName,
197 DeepValue<TFormData, TFieldName>,
198 TFieldValidators,
199 never,
200 TFormData,
201 TFormErrorTypes,
202 TFieldComponents
203 >,
204 {
205 default: {
206 field: VueFieldApi<
207 TFieldName,
208 DeepValue<TFormData, TFieldName>,
209 ToFieldError<NoInfer<TFieldValidators>, never, TFormErrorTypes>,
210 TFormData,
211 TFormErrorTypes,
212 TFieldComponents
213 >
214 }
215 }
217​
218export type VueFormGroupSubscribeProps<
219 TGroupValue,
220 TGroupErrorTypes extends FormErrorTypes,
221 TSelected,
222> = VueSubscribeProps<FormGroupState<TGroupValue, TGroupErrorTypes>, TSelected>
223​
224export type VueFormGroupSubscribeComponent<
225 in out TGroupValue,
226 in out TGroupErrorTypes extends FormErrorTypes,
227> = new <const TSelected>(
228 props: VueFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes, TSelected> &
229 PublicProps,
230) => VueComponentInstance<
231 VueFormGroupSubscribeProps<TGroupValue, TGroupErrorTypes, TSelected>,
232 { default: NoInfer<TSelected> }
234​
235export type VueFormGroupFieldComponent<
236 in out TFormData,
237 in out TGroupValue,
238 in out TGroupErrorTypes extends FormErrorTypes,
239 in out TFormErrorTypes extends FormErrorTypes,
240 in out TFieldComponents extends Record<string, Component>,
241> = new <
242 TFieldName extends DeepKeys<TGroupValue>,
243 const TFieldValidators extends FieldValidators<
244 TGroupValue,
245 TFieldName,
246 DeepValue<TGroupValue, TFieldName>
247 >,
248>(
249 props: VueFormFieldProps<
250 TGroupValue,
251 TFieldName,
252 DeepValue<TGroupValue, TFieldName>,
253 TFieldValidators,
254 TGroupErrorTypes['fieldError'],
255 TFormData,
256 TFormErrorTypes,
257 TFieldComponents
258 > &
259 PublicProps,
260) => VueComponentInstance<
261 VueFormFieldProps<
262 TGroupValue,
263 TFieldName,
264 DeepValue<TGroupValue, TFieldName>,
265 TFieldValidators,
266 TGroupErrorTypes['fieldError'],
267 TFormData,
268 TFormErrorTypes,
269 TFieldComponents
270 >,
271 {
272 default: {
273 field: VueFieldApi<
274 TFieldName,
275 DeepValue<TGroupValue, TFieldName>,
276 ToFieldError<
277 NoInfer<TFieldValidators>,
278 TGroupErrorTypes['fieldError'],
279 TFormErrorTypes
280 >,
281 TFormData,
282 TFormErrorTypes,
283 TFieldComponents
284 >
285 }
286 }
288​
289export type VueFormGroupArrayFieldComponent<
290 in out TFormData,
291 in out TGroupValue,
292 in out TGroupErrorTypes extends FormErrorTypes,
293 in out TFormErrorTypes extends FormErrorTypes,
294 in out TFieldComponents extends Record<string, Component>,
295> = new <
296 TFieldName extends DeepKeysWhereValueIncludes<TGroupValue, Array<any>>,
297 const TFieldValidators extends FieldValidators<
298 TGroupValue,
299 TFieldName,
300 DeepValue<TGroupValue, TFieldName>
301 >,
302>(
303 props: VueFormFieldProps<
304 TGroupValue,
305 TFieldName,
306 DeepValue<TGroupValue, TFieldName>,
307 TFieldValidators,
308 TGroupErrorTypes['fieldError'],
309 TFormData,
310 TFormErrorTypes,
311 TFieldComponents
312 > &
313 PublicProps,
314) => VueComponentInstance<
315 VueFormFieldProps<
316 TGroupValue,
317 TFieldName,
318 DeepValue<TGroupValue, TFieldName>,
319 TFieldValidators,
320 TGroupErrorTypes['fieldError'],
321 TFormData,
322 TFormErrorTypes,
323 TFieldComponents
324 >,
325 {
326 default: {
327 field: VueFieldApi<
328 TFieldName,
329 DeepValue<TGroupValue, TFieldName>,
330 ToFieldError<
331 NoInfer<TFieldValidators>,
332 TGroupErrorTypes['fieldError'],
333 TFormErrorTypes
334 >,
335 TFormData,
336 TFormErrorTypes,
337 TFieldComponents
338 >
339 }
340 }
342​
343export interface VueFormGroupApi<
344 in out TFormData,
345 in out TGroupName,
346 in out TGroupValue,
347 in out TGroupErrorTypes extends FormErrorTypes,
348 in out TFormErrorTypes extends FormErrorTypes,
349 in out TFieldComponents extends Record<string, Component>,
350> extends FormGroupApi<
351 TFormData,
352 TGroupName,
353 TGroupValue,
354 TGroupErrorTypes,
355 TFormErrorTypes
356> {
357 Field: VueFormGroupFieldComponent<
358 TFormData,
359 TGroupValue,
360 TGroupErrorTypes,
361 TFormErrorTypes,
362 TFieldComponents
363 >
364 ArrayField: VueFormGroupArrayFieldComponent<
365 TFormData,
366 TGroupValue,
367 TGroupErrorTypes,
368 TFormErrorTypes,
369 TFieldComponents
370 >
371 Subscribe: VueFormGroupSubscribeComponent<TGroupValue, TGroupErrorTypes>
373​
374export interface VueFormGroupProps<
375 in out TFormData,
376 in out TGroupName,
377 in out TGroupValue,
378 in out TGroupValidators extends FormGroupValidators<TGroupValue>,
379 in out TFormErrorTypes extends FormErrorTypes,
380 in out TFieldComponents extends Record<string, Component>,
381> extends Omit<
382 FormGroupOptions<
383 TFormData,
384 TGroupName,
385 TGroupValue,
386 TGroupValidators,
387 TFormErrorTypes
388 >,
389 'form'
390> {
391 readonly [fieldComponentsType]?: TFieldComponents
393​
394export type VueFormGroupComponent<
395 in out TFormData,
396 in out TFormErrorTypes extends FormErrorTypes,
397 in out TFieldComponents extends Record<string, Component>,
398> = new <
399 TGroupName extends DeepKeys<TFormData>,
400 TGroupValue extends DeepValue<TFormData, TGroupName>,
401 const TGroupValidators extends FormGroupValidators<TGroupValue>,
402>(
403 props: VueFormGroupProps<
404 TFormData,
405 TGroupName,
406 TGroupValue,
407 TGroupValidators,
408 TFormErrorTypes,
409 TFieldComponents
410 > &
411 PublicProps,
412) => VueComponentInstance<
413 VueFormGroupProps<
414 TFormData,
415 TGroupName,
416 TGroupValue,
417 TGroupValidators,
418 TFormErrorTypes,
419 TFieldComponents
420 >,
421 {
422 default: {
423 group: VueFormGroupApi<
424 TFormData,
425 TGroupName,
426 TGroupValue,
427 ToFormGroupErrorTypes<NoInfer<TGroupValidators>>,
428 TFormErrorTypes,
429 TFieldComponents
430 >
431 }
432 }
434​
435export interface VueTanStackFormComponents<
436 in out TFormData,
437 in out TFormErrorTypes extends FormErrorTypes,
438 in out TFieldComponents extends Record<string, Component> = Record<
439 never,
440 never
441 >,
442> {
443 Field: VueFormFieldComponent<TFormData, TFormErrorTypes, TFieldComponents>
444 ArrayField: VueFormArrayFieldComponent<
445 TFormData,
446 TFormErrorTypes,
447 TFieldComponents
448 >
449 Subscribe: VueFormSubscribeComponent<TFormData, TFormErrorTypes>
450 FormGroup: VueFormGroupComponent<TFormData, TFormErrorTypes, TFieldComponents>