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

getFormHookHelpers.public.ts

109 lines3.6 KB

1import { brandComponentFactory, wrapField } from './fieldComponentHelpers.lib'
2import type { FieldWithValue } from '@tanstack/form-core'
3import type { Component, PublicProps } from 'vue'
4import type { VueComponentInstance } from '../vueTypes.lib'
5​
6type ExactFieldBrand<out TValue> = {
7 readonly __tanstackFieldExactType: TValue
8}
9​
10type AcceptsFieldBrand<out TAcceptedValue> = {
11 readonly __tanstackFieldAcceptsType: TAcceptedValue
13​
14type AnyFieldComponent = Component
15type PropsOf<TComponent> = TComponent extends new (...args: any[]) => {
16 $props: infer TProps
18 ? Omit<TProps, keyof PublicProps>
19 : never
20type FieldValuePropKeys<TProps> = {
21 [K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never
22}[keyof TProps]
23type FieldValueFromProp<TProp> =
24 TProp extends FieldWithValue<infer TValue> ? TValue : never
25type FieldValueFromComponentProp<
26 TProps,
27 TFieldPropKey extends keyof TProps,
28> = FieldValueFromProp<TProps[TFieldPropKey]>
29type PublicPropsReplacingField<
30 TProps,
31 TFieldPropKey extends keyof TProps,
32> = Omit<TProps, TFieldPropKey>
33​
34type StrictInjectedFieldComponent<
35 TComponent extends AnyFieldComponent,
36 TProps = PropsOf<TComponent>,
37 TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
38> = (new (
39 props: PublicPropsReplacingField<TProps, TFieldPropKey> & PublicProps,
40) => VueComponentInstance<
41 PublicPropsReplacingField<TProps, TFieldPropKey> & Record<string, any>,
42 {}
43>) &
44 ExactFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>
45​
46type LooseInjectedFieldComponent<
47 TComponent extends AnyFieldComponent,
48 TProps = PropsOf<TComponent>,
49 TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
50> = (new (
51 props: PublicPropsReplacingField<TProps, TFieldPropKey> & PublicProps,
52) => VueComponentInstance<
53 PublicPropsReplacingField<TProps, TFieldPropKey> & Record<string, any>,
54 {}
55>) &
56 AcceptsFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>
57​
58type StrictBrandedFieldComponent<TComponent, TValue> = TComponent &
59 ExactFieldBrand<TValue>
60type LooseBrandedFieldComponent<TComponent, TValue> = TComponent &
61 AcceptsFieldBrand<TValue>
62​
63interface FieldComponentHelper {
64 strict: <
65 TComponent extends AnyFieldComponent,
66 TProps = PropsOf<TComponent>,
67 TFieldPropKey extends FieldValuePropKeys<TProps> =
68 FieldValuePropKeys<TProps>,
69 >(
70 component: TComponent,
71 fieldPropKey: TFieldPropKey,
72 ) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
73 loose: <
74 TComponent extends AnyFieldComponent,
75 TProps = PropsOf<TComponent>,
76 TFieldPropKey extends FieldValuePropKeys<TProps> =
77 FieldValuePropKeys<TProps>,
78 >(
79 component: TComponent,
80 fieldPropKey: TFieldPropKey,
81 ) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
83​
84interface FieldBrandHelper {
85 strict: <TValue>() => <TComponent extends AnyFieldComponent>(
86 component: TComponent,
87 ) => StrictBrandedFieldComponent<TComponent, TValue>
88 loose: <TValue>() => <TComponent extends AnyFieldComponent>(
89 component: TComponent,
90 ) => LooseBrandedFieldComponent<TComponent, TValue>
92​
93export interface FormHookHelpers {
94 fieldBrand: FieldBrandHelper
95 fieldComponent: FieldComponentHelper
97​
98export function getFormHookHelpers(): FormHookHelpers {
99 return {
100 fieldComponent: {
101 loose: wrapField as FieldComponentHelper['loose'],
102 strict: wrapField as FieldComponentHelper['strict'],
103 },
104 fieldBrand: {
105 loose: brandComponentFactory as FieldBrandHelper['loose'],
106 strict: brandComponentFactory as FieldBrandHelper['strict'],
107 },
108 }