@tanstack/preact-form 1.30.4 → 2.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/AppForm/Components.lib.js +91 -0
- package/dist/AppForm/PreactAppFormApi.public.d.ts +14 -0
- package/dist/AppForm/appFormOptions.public.d.ts +14 -0
- package/dist/AppForm/componentMap.public.d.ts +10 -0
- package/dist/AppForm/contexts.lib.js +18 -0
- package/dist/AppForm/createFormHook.public.d.ts +6 -0
- package/dist/AppForm/createFormHook.public.js +26 -0
- package/dist/AppForm/createFormHookTypes.public.d.ts +15 -0
- package/dist/AppForm/fieldComponentHelpers.lib.js +22 -0
- package/dist/AppForm/getFormHookHelpers.public.d.ts +34 -0
- package/dist/AppForm/getFormHookHelpers.public.js +18 -0
- package/dist/AppForm/initializeAppForm.lib.js +13 -0
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +24 -0
- package/dist/FieldGroup/withFields.lib.js +62 -0
- package/dist/FieldGroup/withFields.public.d.ts +55 -0
- package/dist/FieldGroup/withFields.public.js +8 -0
- package/dist/PreactForm/Components.lib.js +99 -0
- package/dist/PreactForm/Components.public.d.ts +66 -0
- package/dist/PreactForm/PreactFormApi.lib.js +28 -0
- package/dist/PreactForm/fieldSubscriptions.lib.js +29 -0
- package/dist/PreactForm/formApiTypes.public.d.ts +10 -0
- package/dist/PreactForm/formType.public.d.ts +9 -0
- package/dist/PreactForm/useField.lib.js +31 -0
- package/dist/PreactForm/useForm.public.d.ts +8 -0
- package/dist/PreactForm/useForm.public.js +10 -0
- package/dist/Subscribe.public.d.ts +29 -0
- package/dist/Subscribe.public.js +16 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +11 -0
- package/dist/preactTypes.public.d.ts +8 -0
- package/dist/types.public.d.ts +2 -0
- package/package.json +17 -18
- package/src/AppForm/Components.lib.tsx +160 -0
- package/src/AppForm/PreactAppFormApi.public.tsx +17 -0
- package/src/AppForm/appFormOptions.public.ts +59 -0
- package/src/AppForm/componentMap.public.ts +19 -0
- package/src/AppForm/contexts.lib.ts +28 -0
- package/src/AppForm/createFormHook.public.ts +37 -0
- package/src/AppForm/createFormHookTypes.public.ts +32 -0
- package/src/AppForm/fieldComponentHelpers.lib.tsx +30 -0
- package/src/AppForm/getFormHookHelpers.public.ts +114 -0
- package/src/AppForm/initializeAppForm.lib.ts +22 -0
- package/src/FieldGroup/FieldGroupApi.public.ts +91 -0
- package/src/FieldGroup/withFields.lib.ts +109 -0
- package/src/FieldGroup/withFields.public.ts +203 -0
- package/src/PreactForm/Components.lib.tsx +166 -0
- package/src/PreactForm/Components.public.ts +383 -0
- package/src/PreactForm/PreactFormApi.lib.tsx +48 -0
- package/src/PreactForm/fieldSubscriptions.lib.ts +32 -0
- package/src/PreactForm/formApiTypes.public.ts +29 -0
- package/src/PreactForm/formType.public.ts +52 -0
- package/src/PreactForm/useField.lib.ts +52 -0
- package/src/PreactForm/useForm.public.ts +34 -0
- package/src/Subscribe.public.tsx +56 -0
- package/src/index.ts +16 -9
- package/src/preactTypes.public.ts +6 -0
- package/src/types.public.ts +2 -0
- package/dist/cjs/createFormHook.cjs +0 -101
- package/dist/cjs/createFormHook.cjs.map +0 -1
- package/dist/cjs/createFormHook.d.cts +0 -80
- package/dist/cjs/index.cjs +0 -34
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -9
- package/dist/cjs/types.d.cts +0 -17
- package/dist/cjs/useField.cjs +0 -108
- package/dist/cjs/useField.cjs.map +0 -1
- package/dist/cjs/useField.d.cts +0 -52
- package/dist/cjs/useFieldGroup.cjs +0 -48
- package/dist/cjs/useFieldGroup.cjs.map +0 -1
- package/dist/cjs/useFieldGroup.d.cts +0 -30
- package/dist/cjs/useForm.cjs +0 -78
- package/dist/cjs/useForm.cjs.map +0 -1
- package/dist/cjs/useForm.d.cts +0 -31
- package/dist/cjs/useFormGroup.cjs +0 -163
- package/dist/cjs/useFormGroup.cjs.map +0 -1
- package/dist/cjs/useFormGroup.d.cts +0 -13
- package/dist/cjs/useFormId.cjs +0 -6
- package/dist/cjs/useFormId.cjs.map +0 -1
- package/dist/cjs/useFormId.d.cts +0 -2
- package/dist/cjs/useIsomorphicLayoutEffect.cjs +0 -6
- package/dist/cjs/useIsomorphicLayoutEffect.cjs.map +0 -1
- package/dist/cjs/useIsomorphicLayoutEffect.d.cts +0 -2
- package/dist/esm/createFormHook.d.ts +0 -80
- package/dist/esm/createFormHook.js +0 -101
- package/dist/esm/createFormHook.js.map +0 -1
- package/dist/esm/index.d.ts +0 -9
- package/dist/esm/index.js +0 -22
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/types.d.ts +0 -17
- package/dist/esm/useField.d.ts +0 -52
- package/dist/esm/useField.js +0 -108
- package/dist/esm/useField.js.map +0 -1
- package/dist/esm/useFieldGroup.d.ts +0 -30
- package/dist/esm/useFieldGroup.js +0 -48
- package/dist/esm/useFieldGroup.js.map +0 -1
- package/dist/esm/useForm.d.ts +0 -31
- package/dist/esm/useForm.js +0 -78
- package/dist/esm/useForm.js.map +0 -1
- package/dist/esm/useFormGroup.d.ts +0 -13
- package/dist/esm/useFormGroup.js +0 -163
- package/dist/esm/useFormGroup.js.map +0 -1
- package/dist/esm/useFormId.d.ts +0 -2
- package/dist/esm/useFormId.js +0 -6
- package/dist/esm/useFormId.js.map +0 -1
- package/dist/esm/useIsomorphicLayoutEffect.d.ts +0 -2
- package/dist/esm/useIsomorphicLayoutEffect.js +0 -6
- package/dist/esm/useIsomorphicLayoutEffect.js.map +0 -1
- package/src/createFormHook.tsx +0 -603
- package/src/types.ts +0 -143
- package/src/useField.tsx +0 -730
- package/src/useFieldGroup.tsx +0 -259
- package/src/useForm.tsx +0 -300
- package/src/useFormGroup.tsx +0 -741
- package/src/useFormId.ts +0 -3
- package/src/useIsomorphicLayoutEffect.ts +0 -4
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useFieldContext } from './contexts.lib'
|
|
2
|
+
import type { FunctionComponent } from 'preact/compat'
|
|
3
|
+
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
4
|
+
|
|
5
|
+
type AnyFieldComponent = (props: any) => CrossVersionPreactNode
|
|
6
|
+
|
|
7
|
+
export function wrapField(
|
|
8
|
+
Component: AnyFieldComponent,
|
|
9
|
+
fieldPropKey: string,
|
|
10
|
+
): AnyFieldComponent {
|
|
11
|
+
const Wrapper: FunctionComponent<any> = function TanStackFormFieldWrapper(
|
|
12
|
+
props,
|
|
13
|
+
) {
|
|
14
|
+
const field = useFieldContext()
|
|
15
|
+
|
|
16
|
+
const newProps = { ...props, [fieldPropKey]: field }
|
|
17
|
+
|
|
18
|
+
return <Component {...newProps} />
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
Wrapper.displayName = 'TanStackForm.FieldComponent'
|
|
22
|
+
|
|
23
|
+
return Wrapper as never
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function brandComponentFactory(): (
|
|
27
|
+
component: AnyFieldComponent,
|
|
28
|
+
) => AnyFieldComponent {
|
|
29
|
+
return (component) => component
|
|
30
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { brandComponentFactory, wrapField } from './fieldComponentHelpers.lib'
|
|
2
|
+
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
3
|
+
import type { FieldWithValue } from '@tanstack/form-core'
|
|
4
|
+
|
|
5
|
+
type ExactFieldBrand<out TValue> = {
|
|
6
|
+
readonly __tanstackFieldExactType: TValue
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type AcceptsFieldBrand<out TAcceptedValue> = {
|
|
10
|
+
readonly __tanstackFieldAcceptsType: TAcceptedValue
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type AnyFieldComponent = (props: any) => CrossVersionPreactNode
|
|
14
|
+
|
|
15
|
+
type PropsOf<TComponent> = TComponent extends (
|
|
16
|
+
props: infer TProps,
|
|
17
|
+
) => CrossVersionPreactNode
|
|
18
|
+
? TProps
|
|
19
|
+
: never
|
|
20
|
+
|
|
21
|
+
type FieldValuePropKeys<TProps> = {
|
|
22
|
+
[K in keyof TProps]-?: TProps[K] extends FieldWithValue<any> ? K : never
|
|
23
|
+
}[keyof TProps]
|
|
24
|
+
|
|
25
|
+
type FieldValueFromProp<TProp> =
|
|
26
|
+
TProp extends FieldWithValue<infer TValue> ? TValue : never
|
|
27
|
+
|
|
28
|
+
type FieldValueFromComponentProp<
|
|
29
|
+
TProps,
|
|
30
|
+
TFieldPropKey extends keyof TProps,
|
|
31
|
+
> = FieldValueFromProp<TProps[TFieldPropKey]>
|
|
32
|
+
|
|
33
|
+
type PublicPropsReplacingField<
|
|
34
|
+
TProps,
|
|
35
|
+
TFieldPropKey extends keyof TProps,
|
|
36
|
+
> = Omit<TProps, TFieldPropKey>
|
|
37
|
+
|
|
38
|
+
type StrictInjectedFieldComponent<
|
|
39
|
+
TComponent extends AnyFieldComponent,
|
|
40
|
+
TProps = PropsOf<TComponent>,
|
|
41
|
+
TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
|
|
42
|
+
> = ((
|
|
43
|
+
props: PublicPropsReplacingField<TProps, TFieldPropKey>,
|
|
44
|
+
) => CrossVersionPreactNode) &
|
|
45
|
+
ExactFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>
|
|
46
|
+
|
|
47
|
+
type LooseInjectedFieldComponent<
|
|
48
|
+
TComponent extends AnyFieldComponent,
|
|
49
|
+
TProps = PropsOf<TComponent>,
|
|
50
|
+
TFieldPropKey extends FieldValuePropKeys<TProps> = FieldValuePropKeys<TProps>,
|
|
51
|
+
> = ((
|
|
52
|
+
props: PublicPropsReplacingField<TProps, TFieldPropKey>,
|
|
53
|
+
) => CrossVersionPreactNode) &
|
|
54
|
+
AcceptsFieldBrand<FieldValueFromComponentProp<TProps, TFieldPropKey>>
|
|
55
|
+
|
|
56
|
+
type StrictBrandedFieldComponent<
|
|
57
|
+
TComponent extends AnyFieldComponent,
|
|
58
|
+
TValue,
|
|
59
|
+
> = TComponent & ExactFieldBrand<TValue>
|
|
60
|
+
|
|
61
|
+
type LooseBrandedFieldComponent<
|
|
62
|
+
TComponent extends AnyFieldComponent,
|
|
63
|
+
TAcceptedValue,
|
|
64
|
+
> = TComponent & AcceptsFieldBrand<TAcceptedValue>
|
|
65
|
+
|
|
66
|
+
interface FieldComponentHelper {
|
|
67
|
+
strict: <
|
|
68
|
+
TComponent extends AnyFieldComponent,
|
|
69
|
+
TProps = PropsOf<TComponent>,
|
|
70
|
+
TFieldPropKey extends FieldValuePropKeys<TProps> =
|
|
71
|
+
FieldValuePropKeys<TProps>,
|
|
72
|
+
>(
|
|
73
|
+
Component: TComponent,
|
|
74
|
+
fieldPropKey: TFieldPropKey,
|
|
75
|
+
) => StrictInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
|
|
76
|
+
|
|
77
|
+
loose: <
|
|
78
|
+
TComponent extends AnyFieldComponent,
|
|
79
|
+
TProps = PropsOf<TComponent>,
|
|
80
|
+
TFieldPropKey extends FieldValuePropKeys<TProps> =
|
|
81
|
+
FieldValuePropKeys<TProps>,
|
|
82
|
+
>(
|
|
83
|
+
Component: TComponent,
|
|
84
|
+
fieldPropKey: TFieldPropKey,
|
|
85
|
+
) => LooseInjectedFieldComponent<TComponent, TProps, TFieldPropKey>
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface FieldBrandHelper {
|
|
89
|
+
strict: <TValue>() => <TComponent extends AnyFieldComponent>(
|
|
90
|
+
Component: TComponent,
|
|
91
|
+
) => StrictBrandedFieldComponent<TComponent, TValue>
|
|
92
|
+
|
|
93
|
+
loose: <TValue>() => <TComponent extends AnyFieldComponent>(
|
|
94
|
+
Component: TComponent,
|
|
95
|
+
) => LooseBrandedFieldComponent<TComponent, TValue>
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface FormHookHelpers {
|
|
99
|
+
fieldBrand: FieldBrandHelper
|
|
100
|
+
fieldComponent: FieldComponentHelper
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function getFormHookHelpers(): FormHookHelpers {
|
|
104
|
+
return {
|
|
105
|
+
fieldComponent: {
|
|
106
|
+
loose: wrapField as FieldComponentHelper['loose'],
|
|
107
|
+
strict: wrapField as FieldComponentHelper['strict'],
|
|
108
|
+
},
|
|
109
|
+
fieldBrand: {
|
|
110
|
+
loose: brandComponentFactory as FieldBrandHelper['loose'],
|
|
111
|
+
strict: brandComponentFactory as FieldBrandHelper['strict'],
|
|
112
|
+
},
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { InternalFormApi } from '@tanstack/form-core/internals'
|
|
2
|
+
import { attachPreactAppFormComponents } from './Components.lib'
|
|
3
|
+
import type { FormOptions } from '@tanstack/form-core'
|
|
4
|
+
import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
|
|
5
|
+
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
6
|
+
|
|
7
|
+
export function createAppFormInitializer<
|
|
8
|
+
TComponents extends AnyPreactFormComponentMap,
|
|
9
|
+
>(
|
|
10
|
+
createOptions: TComponents,
|
|
11
|
+
): (options: FormOptions<any, any, any>) => InternalPreactFormApi {
|
|
12
|
+
return (options) => {
|
|
13
|
+
const form = new InternalFormApi(options)
|
|
14
|
+
const extendedForm = attachPreactAppFormComponents(
|
|
15
|
+
form,
|
|
16
|
+
createOptions.formComponents,
|
|
17
|
+
createOptions.fieldComponents,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
return extendedForm as never
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DeepKeys,
|
|
3
|
+
DeepKeysWhereValueIncludes,
|
|
4
|
+
DeepValue,
|
|
5
|
+
FieldValidators,
|
|
6
|
+
FormApiArrayMethods,
|
|
7
|
+
FormApiFieldMethods,
|
|
8
|
+
FormErrorTypes,
|
|
9
|
+
FormState,
|
|
10
|
+
ValidationIssue,
|
|
11
|
+
} from '@tanstack/form-core'
|
|
12
|
+
import type { FunctionComponent } from 'preact/compat'
|
|
13
|
+
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
14
|
+
import type {
|
|
15
|
+
PreactFormFieldProps,
|
|
16
|
+
PreactFormSubscribeProps,
|
|
17
|
+
} from '../PreactForm/Components.public'
|
|
18
|
+
import type { ReadonlyAtom } from '@tanstack/preact-store'
|
|
19
|
+
|
|
20
|
+
export interface FieldGroupFieldComponent<
|
|
21
|
+
in out TFieldData,
|
|
22
|
+
in out TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
23
|
+
> {
|
|
24
|
+
<const TFieldName extends DeepKeys<TFieldData>>(
|
|
25
|
+
props: PreactFormFieldProps<
|
|
26
|
+
TFieldData,
|
|
27
|
+
TFieldName,
|
|
28
|
+
DeepValue<TFieldData, TFieldName>,
|
|
29
|
+
FieldValidators<
|
|
30
|
+
TFieldData,
|
|
31
|
+
TFieldName,
|
|
32
|
+
DeepValue<TFieldData, TFieldName>
|
|
33
|
+
>,
|
|
34
|
+
ValidationIssue,
|
|
35
|
+
unknown,
|
|
36
|
+
FormErrorTypes,
|
|
37
|
+
TFieldComponents
|
|
38
|
+
>,
|
|
39
|
+
): CrossVersionPreactNode
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface FieldGroupArrayFieldComponent<
|
|
43
|
+
in out TFieldData,
|
|
44
|
+
in out TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
45
|
+
> {
|
|
46
|
+
<const TFieldName extends DeepKeysWhereValueIncludes<TFieldData, Array<any>>>(
|
|
47
|
+
props: PreactFormFieldProps<
|
|
48
|
+
TFieldData,
|
|
49
|
+
TFieldName,
|
|
50
|
+
DeepValue<TFieldData, TFieldName>,
|
|
51
|
+
FieldValidators<
|
|
52
|
+
TFieldData,
|
|
53
|
+
TFieldName,
|
|
54
|
+
DeepValue<TFieldData, TFieldName>
|
|
55
|
+
>,
|
|
56
|
+
ValidationIssue,
|
|
57
|
+
unknown,
|
|
58
|
+
FormErrorTypes,
|
|
59
|
+
TFieldComponents
|
|
60
|
+
>,
|
|
61
|
+
): CrossVersionPreactNode
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type FieldGroupSubscribeProps<TSelected> = PreactFormSubscribeProps<
|
|
65
|
+
unknown,
|
|
66
|
+
FormErrorTypes,
|
|
67
|
+
TSelected
|
|
68
|
+
>
|
|
69
|
+
|
|
70
|
+
export type FieldGroupSubscribeComponent = <TSelected>(
|
|
71
|
+
props: FieldGroupSubscribeProps<TSelected>,
|
|
72
|
+
) => CrossVersionPreactNode
|
|
73
|
+
|
|
74
|
+
export interface FieldGroupApi<
|
|
75
|
+
in out TFieldData,
|
|
76
|
+
in out TFieldComponents extends Record<string, FunctionComponent<any>> =
|
|
77
|
+
Record<never, never>,
|
|
78
|
+
>
|
|
79
|
+
extends FormApiFieldMethods<TFieldData>, FormApiArrayMethods<TFieldData> {
|
|
80
|
+
atom: ReadonlyAtom<TFieldData>
|
|
81
|
+
Field: FieldGroupFieldComponent<TFieldData, TFieldComponents>
|
|
82
|
+
ArrayField: FieldGroupArrayFieldComponent<TFieldData, TFieldComponents>
|
|
83
|
+
Subscribe: FieldGroupSubscribeComponent
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type AnyFieldGroupApi = FieldGroupApi<
|
|
87
|
+
any,
|
|
88
|
+
Record<string, FunctionComponent<any>>
|
|
89
|
+
>
|
|
90
|
+
|
|
91
|
+
export type FieldGroupFormState = FormState<unknown, FormErrorTypes>
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import Preact from 'preact/compat'
|
|
2
|
+
import {
|
|
3
|
+
InternalFieldGroupApi,
|
|
4
|
+
defineFieldGroupFieldsRuntime,
|
|
5
|
+
fieldGroupHelperRuntime,
|
|
6
|
+
} from '@tanstack/form-core/internals'
|
|
7
|
+
import type { InternalFieldGroupBindings } from '@tanstack/form-core/internals'
|
|
8
|
+
import type { FunctionComponent } from 'preact/compat'
|
|
9
|
+
import type { AnyFieldGroupApi } from './FieldGroupApi.public'
|
|
10
|
+
|
|
11
|
+
function attachPreactFieldGroupComponents(
|
|
12
|
+
group: InternalFieldGroupApi,
|
|
13
|
+
form: any,
|
|
14
|
+
): AnyFieldGroupApi {
|
|
15
|
+
const Field: FunctionComponent<any> = (props) => {
|
|
16
|
+
const FormField = form.Field
|
|
17
|
+
return Preact.createElement(
|
|
18
|
+
FormField as FunctionComponent<any>,
|
|
19
|
+
group._getFormFieldOptions(props, (base, overrides) => ({
|
|
20
|
+
...base,
|
|
21
|
+
...overrides,
|
|
22
|
+
})),
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
Field.displayName = 'TanStackForm.FieldGroup.Field'
|
|
26
|
+
|
|
27
|
+
const ArrayField: FunctionComponent<any> = (props) => {
|
|
28
|
+
const FormArrayField = form.ArrayField
|
|
29
|
+
return Preact.createElement(
|
|
30
|
+
FormArrayField as FunctionComponent<any>,
|
|
31
|
+
group._getFormFieldOptions(props, (base, overrides) => ({
|
|
32
|
+
...base,
|
|
33
|
+
...overrides,
|
|
34
|
+
})),
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
ArrayField.displayName = 'TanStackForm.FieldGroup.ArrayField'
|
|
38
|
+
|
|
39
|
+
const Subscribe: FunctionComponent<any> = (props) => {
|
|
40
|
+
const FormSubscribe = form.Subscribe
|
|
41
|
+
return Preact.createElement(FormSubscribe as FunctionComponent<any>, props)
|
|
42
|
+
}
|
|
43
|
+
Subscribe.displayName = 'TanStackForm.FieldGroup.Subscribe'
|
|
44
|
+
|
|
45
|
+
return Object.assign(group, {
|
|
46
|
+
Field,
|
|
47
|
+
ArrayField,
|
|
48
|
+
Subscribe,
|
|
49
|
+
}) as never
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function defineFieldGroupRuntime<
|
|
53
|
+
TFields extends Record<string, unknown>,
|
|
54
|
+
>(defineFieldGroupFn: (helperRuntime: any) => TFields) {
|
|
55
|
+
const fields = defineFieldGroupFieldsRuntime(
|
|
56
|
+
defineFieldGroupFn(fieldGroupHelperRuntime),
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
fields,
|
|
61
|
+
bindComponent: (
|
|
62
|
+
Component: (props: any) => unknown,
|
|
63
|
+
fieldsPropName: string,
|
|
64
|
+
) => withFieldsRuntime(fields, Component, fieldsPropName),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function withFieldsRuntime(
|
|
69
|
+
fields: Record<string, unknown>,
|
|
70
|
+
Component: (props: any) => unknown,
|
|
71
|
+
fieldsPropName: string,
|
|
72
|
+
) {
|
|
73
|
+
const fieldNames = Object.keys(fields)
|
|
74
|
+
|
|
75
|
+
const FieldGroupComponent = (props: any) => {
|
|
76
|
+
const { form, ...restProps } = props
|
|
77
|
+
const bindingsRef = Preact.useRef<InternalFieldGroupBindings>(
|
|
78
|
+
props[fieldsPropName],
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if (!form) {
|
|
82
|
+
throw new Error('TanStack Form: Field groups must receive a `form` prop.')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
bindingsRef.current = props[fieldsPropName]
|
|
86
|
+
|
|
87
|
+
const fieldGroupApi = Preact.useMemo(
|
|
88
|
+
() =>
|
|
89
|
+
attachPreactFieldGroupComponents(
|
|
90
|
+
new InternalFieldGroupApi({
|
|
91
|
+
form,
|
|
92
|
+
fieldNames,
|
|
93
|
+
getBindings: () => bindingsRef.current!,
|
|
94
|
+
}),
|
|
95
|
+
form,
|
|
96
|
+
),
|
|
97
|
+
[form],
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
return Component({
|
|
101
|
+
...restProps,
|
|
102
|
+
[fieldsPropName]: fieldGroupApi,
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
FieldGroupComponent.displayName = 'TanStackForm.FieldGroup'
|
|
107
|
+
|
|
108
|
+
return FieldGroupComponent
|
|
109
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { defineFieldGroupRuntime } from './withFields.lib'
|
|
2
|
+
import type {
|
|
3
|
+
DeepKeys,
|
|
4
|
+
DeepKeysWhereValueIncludes,
|
|
5
|
+
DeepValue,
|
|
6
|
+
FormApi,
|
|
7
|
+
} from '@tanstack/form-core'
|
|
8
|
+
import type { FunctionComponent } from 'preact/compat'
|
|
9
|
+
import type { CrossVersionPreactNode } from '../preactTypes.public'
|
|
10
|
+
import type { PreactTanStackFormComponents } from '../PreactForm/Components.public'
|
|
11
|
+
import type { FieldGroupApi } from './FieldGroupApi.public'
|
|
12
|
+
|
|
13
|
+
declare const fieldGroupFieldSlotValueSymbol: unique symbol
|
|
14
|
+
declare const fieldGroupFieldsSymbol: unique symbol
|
|
15
|
+
|
|
16
|
+
export type FieldGroupFieldSlotMode = 'strict' | 'loose'
|
|
17
|
+
|
|
18
|
+
export interface FieldGroupFieldSlot<
|
|
19
|
+
out TValue,
|
|
20
|
+
out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode,
|
|
21
|
+
> {
|
|
22
|
+
readonly mode: TMode
|
|
23
|
+
readonly [fieldGroupFieldSlotValueSymbol]: TValue
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>
|
|
27
|
+
|
|
28
|
+
export type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
|
|
29
|
+
TValue,
|
|
30
|
+
'strict'
|
|
31
|
+
>
|
|
32
|
+
|
|
33
|
+
export type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
|
|
34
|
+
TValue,
|
|
35
|
+
'loose'
|
|
36
|
+
>
|
|
37
|
+
|
|
38
|
+
export type FieldGroupFieldSlotValue<TSlot> =
|
|
39
|
+
TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never
|
|
40
|
+
|
|
41
|
+
export type FieldGroupFieldSlotModeOf<TSlot> =
|
|
42
|
+
TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never
|
|
43
|
+
|
|
44
|
+
type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB]
|
|
45
|
+
? [TTypeB] extends [TTypeA]
|
|
46
|
+
? true
|
|
47
|
+
: false
|
|
48
|
+
: false
|
|
49
|
+
|
|
50
|
+
export type FieldGroupFieldSlotAllows<TSlot, TValue> =
|
|
51
|
+
TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode>
|
|
52
|
+
? TMode extends 'strict'
|
|
53
|
+
? IsSame<TValue, TAcceptedValue>
|
|
54
|
+
: [TValue] extends [TAcceptedValue]
|
|
55
|
+
? true
|
|
56
|
+
: false
|
|
57
|
+
: false
|
|
58
|
+
|
|
59
|
+
export type FieldGroupFieldNameForSlot<
|
|
60
|
+
TFieldData,
|
|
61
|
+
TSlot extends AnyFieldGroupFieldSlot,
|
|
62
|
+
> = {
|
|
63
|
+
[TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<
|
|
64
|
+
TSlot,
|
|
65
|
+
DeepValue<TFieldData, TFieldName>
|
|
66
|
+
> extends true
|
|
67
|
+
? TFieldName
|
|
68
|
+
: never
|
|
69
|
+
}[DeepKeys<TFieldData>]
|
|
70
|
+
|
|
71
|
+
export type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>
|
|
72
|
+
|
|
73
|
+
export type FieldGroupFieldNames<
|
|
74
|
+
TFieldData,
|
|
75
|
+
TFields extends FieldGroupFields,
|
|
76
|
+
> = {
|
|
77
|
+
[TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<
|
|
78
|
+
TFieldData,
|
|
79
|
+
TFields[TFieldName]
|
|
80
|
+
>
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type FieldGroupFieldData<TFields extends FieldGroupFields> = {
|
|
84
|
+
[
|
|
85
|
+
TFieldName in keyof TFields
|
|
86
|
+
]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any>
|
|
87
|
+
? TValue
|
|
88
|
+
: never
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
|
|
92
|
+
readonly [fieldGroupFieldsSymbol]: infer TFields
|
|
93
|
+
}
|
|
94
|
+
? TFields
|
|
95
|
+
: never
|
|
96
|
+
|
|
97
|
+
export type PreactFieldGroup<
|
|
98
|
+
TFields extends FieldGroupFields,
|
|
99
|
+
TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
|
|
100
|
+
never,
|
|
101
|
+
never
|
|
102
|
+
>,
|
|
103
|
+
> = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
|
|
104
|
+
readonly [fieldGroupFieldsSymbol]: TFields
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type FieldGroupFieldComponentsOf<TFieldGroup> =
|
|
108
|
+
TFieldGroup extends PreactFieldGroup<any, infer TFieldComponents>
|
|
109
|
+
? TFieldComponents
|
|
110
|
+
: never
|
|
111
|
+
|
|
112
|
+
export type FieldGroupForm<
|
|
113
|
+
TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
|
|
114
|
+
never,
|
|
115
|
+
never
|
|
116
|
+
>,
|
|
117
|
+
TFormData = any,
|
|
118
|
+
> = FormApi<TFormData, any> &
|
|
119
|
+
PreactTanStackFormComponents<TFormData, any, TFieldComponents>
|
|
120
|
+
|
|
121
|
+
export type FieldGroupFieldBindingForSlot<
|
|
122
|
+
TFormData,
|
|
123
|
+
TSlot extends AnyFieldGroupFieldSlot,
|
|
124
|
+
> =
|
|
125
|
+
TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode>
|
|
126
|
+
? TMode extends 'strict'
|
|
127
|
+
? FieldGroupFieldNameForSlot<TFormData, TSlot>
|
|
128
|
+
: DeepKeysWhereValueIncludes<TFormData, TValue>
|
|
129
|
+
: never
|
|
130
|
+
|
|
131
|
+
export type FieldGroupFieldBindings<
|
|
132
|
+
TFields extends FieldGroupFields,
|
|
133
|
+
TFormData = any,
|
|
134
|
+
> = {
|
|
135
|
+
[TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<
|
|
136
|
+
TFormData,
|
|
137
|
+
TFields[TFieldName]
|
|
138
|
+
>
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> =
|
|
142
|
+
FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields
|
|
143
|
+
? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData>
|
|
144
|
+
: never
|
|
145
|
+
|
|
146
|
+
export type FieldGroupFieldsPropName<
|
|
147
|
+
TProps,
|
|
148
|
+
TFieldGroup extends PreactFieldGroup<any, any>,
|
|
149
|
+
> = {
|
|
150
|
+
[TPropName in keyof TProps]-?: IsSame<
|
|
151
|
+
TProps[TPropName],
|
|
152
|
+
TFieldGroup
|
|
153
|
+
> extends true
|
|
154
|
+
? TPropName
|
|
155
|
+
: never
|
|
156
|
+
}[keyof TProps]
|
|
157
|
+
|
|
158
|
+
export type FieldGroupWithFieldsFn<
|
|
159
|
+
TFieldGroup extends PreactFieldGroup<any, any>,
|
|
160
|
+
> = <
|
|
161
|
+
TProps extends object,
|
|
162
|
+
TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>,
|
|
163
|
+
>(
|
|
164
|
+
Component: (props: TProps) => CrossVersionPreactNode,
|
|
165
|
+
fieldsPropName: TFieldsPropName,
|
|
166
|
+
) => <TFormData>(
|
|
167
|
+
props: Omit<TProps, TFieldsPropName | 'form'> & {
|
|
168
|
+
form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>
|
|
169
|
+
} & {
|
|
170
|
+
[TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<
|
|
171
|
+
TFieldGroup,
|
|
172
|
+
TFormData
|
|
173
|
+
>
|
|
174
|
+
},
|
|
175
|
+
) => CrossVersionPreactNode
|
|
176
|
+
|
|
177
|
+
export interface FieldGroupHelper {
|
|
178
|
+
strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>
|
|
179
|
+
loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface FieldGroupDefinition<
|
|
183
|
+
TFields extends FieldGroupFields,
|
|
184
|
+
TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
185
|
+
> {
|
|
186
|
+
/** The virtual field-group API injected into the bound component. */
|
|
187
|
+
fields: PreactFieldGroup<TFields, TFieldComponents>
|
|
188
|
+
/** Binds a component's virtual field API to concrete paths in a form. */
|
|
189
|
+
bindComponent: FieldGroupWithFieldsFn<
|
|
190
|
+
PreactFieldGroup<TFields, TFieldComponents>
|
|
191
|
+
>
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Signature shared by `defineFieldGroup` and app-form field-group definers. */
|
|
195
|
+
export type DefineFieldGroupFn<
|
|
196
|
+
TFieldComponents extends Record<string, FunctionComponent<any>>,
|
|
197
|
+
> = <const TFields extends FieldGroupFields>(
|
|
198
|
+
defineFn: (helper: FieldGroupHelper) => TFields,
|
|
199
|
+
) => FieldGroupDefinition<TFields, TFieldComponents>
|
|
200
|
+
|
|
201
|
+
/** Defines a reusable group of virtual fields. */
|
|
202
|
+
export const defineFieldGroup: DefineFieldGroupFn<Record<never, never>> =
|
|
203
|
+
defineFieldGroupRuntime as never
|