@tanstack/preact-form 1.30.4 → 1.33.3
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 +90 -0
- package/dist/AppForm/PreactAppFormApi.public.d.ts +15 -0
- package/dist/AppForm/appFormOptions.public.d.ts +15 -0
- package/dist/AppForm/componentMap.public.d.ts +11 -0
- package/dist/AppForm/contexts.lib.js +18 -0
- package/dist/AppForm/createFormHook.public.d.ts +7 -0
- package/dist/AppForm/createFormHook.public.js +26 -0
- package/dist/AppForm/createFormHookTypes.public.d.ts +16 -0
- package/dist/AppForm/fieldComponentHelpers.lib.js +21 -0
- package/dist/AppForm/getFormHookHelpers.public.d.ts +35 -0
- package/dist/AppForm/getFormHookHelpers.public.js +18 -0
- package/dist/AppForm/initializeAppForm.lib.js +12 -0
- package/dist/FieldGroup/FieldGroupApi.public.d.ts +25 -0
- package/dist/FieldGroup/withFields.lib.js +98 -0
- package/dist/FieldGroup/withFields.public.d.ts +53 -0
- package/dist/FieldGroup/withFields.public.js +19 -0
- package/dist/PreactForm/Components.lib.js +97 -0
- package/dist/PreactForm/Components.public.d.ts +67 -0
- package/dist/PreactForm/PreactFormApi.lib.js +27 -0
- package/dist/PreactForm/fieldSubscriptions.lib.js +27 -0
- package/dist/PreactForm/formApiTypes.public.d.ts +11 -0
- package/dist/PreactForm/formType.public.d.ts +10 -0
- package/dist/PreactForm/useField.lib.js +31 -0
- package/dist/PreactForm/useForm.public.d.ts +9 -0
- package/dist/PreactForm/useForm.public.js +10 -0
- package/dist/Subscribe.public.d.ts +30 -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 +10 -0
- package/dist/types.public.d.ts +2 -0
- package/package.json +16 -17
- 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 +34 -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 +191 -0
- package/src/FieldGroup/withFields.public.ts +221 -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,28 @@
|
|
|
1
|
+
import { createContext, useContext } from 'preact/compat'
|
|
2
|
+
import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
|
|
3
|
+
import type { InternalPreactFormApi } from '../PreactForm/PreactFormApi.lib'
|
|
4
|
+
|
|
5
|
+
export const FormContext = createContext<InternalPreactFormApi | null>(null)
|
|
6
|
+
export const FieldContext = createContext<AnyInternalFieldApi | null>(null)
|
|
7
|
+
|
|
8
|
+
export function useFieldContext(): AnyInternalFieldApi {
|
|
9
|
+
const field = useContext(FieldContext)
|
|
10
|
+
if (field === null) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
'TanStack Form: Field components must be used within a `form.Field` component.',
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return field
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useFormContext(): InternalPreactFormApi {
|
|
20
|
+
const form = useContext(FormContext)
|
|
21
|
+
if (form === null) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
'TanStack Form: Form components must be used within a `form.AppForm` component.',
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return form
|
|
28
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useInternalForm } from '../PreactForm/PreactFormApi.lib'
|
|
2
|
+
import { getFieldGroupHelpers } from '../FieldGroup/withFields.public'
|
|
3
|
+
import { createAppFormInitializer } from './initializeAppForm.lib'
|
|
4
|
+
import { useFormContext } from './contexts.lib'
|
|
5
|
+
import type { AppFormOptionsApi } from './appFormOptions.public'
|
|
6
|
+
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
7
|
+
import type {
|
|
8
|
+
AppFormHookResult,
|
|
9
|
+
UseAppFormHook,
|
|
10
|
+
} from './createFormHookTypes.public'
|
|
11
|
+
import type { FormOptions } from '@tanstack/form-core'
|
|
12
|
+
|
|
13
|
+
const appFormOptions = ((opts) => {
|
|
14
|
+
return opts
|
|
15
|
+
}) as AppFormOptionsApi<any>
|
|
16
|
+
|
|
17
|
+
appFormOptions.strictSchema = (opts) => opts as never
|
|
18
|
+
appFormOptions.looseSchema = (opts) => opts as never
|
|
19
|
+
|
|
20
|
+
export function createFormHook<
|
|
21
|
+
const TComponents extends AnyPreactFormComponentMap,
|
|
22
|
+
>(createOptions: TComponents): AppFormHookResult<TComponents> {
|
|
23
|
+
const initializeAppForm = createAppFormInitializer(createOptions)
|
|
24
|
+
|
|
25
|
+
function useExtendedForm(hookOptions: FormOptions<any, any, any>) {
|
|
26
|
+
const form = useInternalForm(hookOptions, initializeAppForm)
|
|
27
|
+
return form
|
|
28
|
+
}
|
|
29
|
+
const useAppForm = useExtendedForm as never as UseAppFormHook<TComponents>
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
useFormContext: useFormContext as never,
|
|
33
|
+
appFormOptions,
|
|
34
|
+
getAppFieldGroupHelpers: getFieldGroupHelpers,
|
|
35
|
+
useAppForm,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AppFormOptionsApi } from './appFormOptions.public'
|
|
2
|
+
import type { AnyPreactFormComponentMap } from './componentMap.public'
|
|
3
|
+
import type { PreactAppFormApi } from './PreactAppFormApi.public'
|
|
4
|
+
import type { FieldGroupHelpers } from '../FieldGroup/withFields.public'
|
|
5
|
+
import type {
|
|
6
|
+
FormOptions,
|
|
7
|
+
FormValidators,
|
|
8
|
+
ToFormErrorTypes,
|
|
9
|
+
} from '@tanstack/form-core'
|
|
10
|
+
|
|
11
|
+
export type UseAppFormHook<
|
|
12
|
+
in out TComponents extends AnyPreactFormComponentMap,
|
|
13
|
+
> = <
|
|
14
|
+
TFormData,
|
|
15
|
+
TFormValidators extends FormValidators<TFormData>,
|
|
16
|
+
TSubmitReturn,
|
|
17
|
+
>(
|
|
18
|
+
options: FormOptions<TFormData, TFormValidators, TSubmitReturn>,
|
|
19
|
+
) => PreactAppFormApi<
|
|
20
|
+
TFormData,
|
|
21
|
+
ToFormErrorTypes<TFormValidators, TSubmitReturn>,
|
|
22
|
+
TComponents
|
|
23
|
+
>
|
|
24
|
+
|
|
25
|
+
export interface AppFormHookResult<
|
|
26
|
+
in out TComponents extends AnyPreactFormComponentMap,
|
|
27
|
+
> {
|
|
28
|
+
appFormOptions: AppFormOptionsApi<TComponents>
|
|
29
|
+
getAppFieldGroupHelpers: () => FieldGroupHelpers<
|
|
30
|
+
TComponents['fieldComponents']
|
|
31
|
+
>
|
|
32
|
+
useAppForm: UseAppFormHook<TComponents>
|
|
33
|
+
useFormContext: () => PreactAppFormApi<any, any, TComponents>
|
|
34
|
+
}
|
|
@@ -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,191 @@
|
|
|
1
|
+
import Preact from 'preact/compat'
|
|
2
|
+
import { createAtom, shallow } from '@tanstack/preact-store'
|
|
3
|
+
import {
|
|
4
|
+
getBy,
|
|
5
|
+
transformFieldOptionsFieldNames,
|
|
6
|
+
} from '@tanstack/form-core/internals'
|
|
7
|
+
import type { FunctionComponent } from 'preact/compat'
|
|
8
|
+
import type { AnyFieldGroupApi } from './FieldGroupApi.public'
|
|
9
|
+
|
|
10
|
+
type FieldBindings = Record<string, string>
|
|
11
|
+
|
|
12
|
+
interface ResolvedNameCacheEntry {
|
|
13
|
+
binding: string
|
|
14
|
+
name: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
type ResolvedNameCache = Map<string, ResolvedNameCacheEntry>
|
|
18
|
+
|
|
19
|
+
function getRootSegmentEnd(name: string): number {
|
|
20
|
+
const dotIndex = name.indexOf('.')
|
|
21
|
+
const arrayIndex = name.indexOf('[')
|
|
22
|
+
|
|
23
|
+
if (dotIndex === -1) return arrayIndex === -1 ? name.length : arrayIndex
|
|
24
|
+
if (arrayIndex === -1) return dotIndex
|
|
25
|
+
return Math.min(dotIndex, arrayIndex)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function resolveFieldName(
|
|
29
|
+
bindings: FieldBindings,
|
|
30
|
+
cache: ResolvedNameCache,
|
|
31
|
+
fieldName: string,
|
|
32
|
+
): string {
|
|
33
|
+
const rootEnd = getRootSegmentEnd(fieldName)
|
|
34
|
+
const rootName = fieldName.slice(0, rootEnd)
|
|
35
|
+
const binding = bindings[rootName]
|
|
36
|
+
|
|
37
|
+
if (binding === undefined) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`TanStack Form: Missing field group binding for "${rootName}".`,
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const cached = cache.get(fieldName)
|
|
44
|
+
if (cached?.binding === binding) return cached.name
|
|
45
|
+
|
|
46
|
+
const resolvedName = `${binding}${fieldName.slice(rootEnd)}`
|
|
47
|
+
cache.set(fieldName, {
|
|
48
|
+
binding,
|
|
49
|
+
name: resolvedName,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
return resolvedName
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function resolveFieldProps<TProps extends { name: string }>(
|
|
56
|
+
props: TProps,
|
|
57
|
+
resolveName: (name: string) => string,
|
|
58
|
+
): TProps {
|
|
59
|
+
return transformFieldOptionsFieldNames(
|
|
60
|
+
props,
|
|
61
|
+
resolveName,
|
|
62
|
+
(base, overrides) => ({
|
|
63
|
+
...base,
|
|
64
|
+
...overrides,
|
|
65
|
+
}),
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function createFieldGroupApi(
|
|
70
|
+
form: any,
|
|
71
|
+
bindingsRef: Preact.RefObject<FieldBindings>,
|
|
72
|
+
fieldNames: Array<string>,
|
|
73
|
+
): AnyFieldGroupApi {
|
|
74
|
+
const cache: ResolvedNameCache = new Map()
|
|
75
|
+
const resolveName = (fieldName: string) =>
|
|
76
|
+
resolveFieldName(bindingsRef.current!, cache, fieldName)
|
|
77
|
+
const atom = createAtom(
|
|
78
|
+
() => {
|
|
79
|
+
const nextValues: Record<string, unknown> = {}
|
|
80
|
+
const formValues = form._atoms.values.get()
|
|
81
|
+
|
|
82
|
+
for (const fieldName of fieldNames) {
|
|
83
|
+
nextValues[fieldName] = getBy(formValues, resolveName(fieldName))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return nextValues
|
|
87
|
+
},
|
|
88
|
+
{ compare: shallow },
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
const Field: FunctionComponent<any> = (props) => {
|
|
92
|
+
const FormField = form.Field
|
|
93
|
+
return Preact.createElement(
|
|
94
|
+
FormField as FunctionComponent<any>,
|
|
95
|
+
resolveFieldProps(props, resolveName),
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
Field.displayName = 'TanStackForm.FieldGroup.Field'
|
|
99
|
+
|
|
100
|
+
const ArrayField: FunctionComponent<any> = (props) => {
|
|
101
|
+
const FormArrayField = form.ArrayField
|
|
102
|
+
return Preact.createElement(
|
|
103
|
+
FormArrayField as FunctionComponent<any>,
|
|
104
|
+
resolveFieldProps(props, resolveName),
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
ArrayField.displayName = 'TanStackForm.FieldGroup.ArrayField'
|
|
108
|
+
|
|
109
|
+
const Subscribe: FunctionComponent<any> = (props) => {
|
|
110
|
+
const FormSubscribe = form.Subscribe
|
|
111
|
+
return Preact.createElement(FormSubscribe as FunctionComponent<any>, props)
|
|
112
|
+
}
|
|
113
|
+
Subscribe.displayName = 'TanStackForm.FieldGroup.Subscribe'
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
atom,
|
|
117
|
+
Field,
|
|
118
|
+
ArrayField,
|
|
119
|
+
Subscribe,
|
|
120
|
+
getFieldValue: (fieldName: string) =>
|
|
121
|
+
form.getFieldValue(resolveName(fieldName)),
|
|
122
|
+
setFieldValue: (fieldName: string, value: unknown, options?: unknown) =>
|
|
123
|
+
form.setFieldValue(resolveName(fieldName), value, options),
|
|
124
|
+
resetField: (fieldName: string) => form.resetField(resolveName(fieldName)),
|
|
125
|
+
swapFieldValues: (
|
|
126
|
+
fieldName: string,
|
|
127
|
+
indexA: number,
|
|
128
|
+
indexB: number,
|
|
129
|
+
options?: unknown,
|
|
130
|
+
) => form.swapFieldValues(resolveName(fieldName), indexA, indexB, options),
|
|
131
|
+
pushFieldValue: (fieldName: string, value: unknown, options?: unknown) =>
|
|
132
|
+
form.pushFieldValue(resolveName(fieldName), value, options),
|
|
133
|
+
insertFieldValue: (
|
|
134
|
+
fieldName: string,
|
|
135
|
+
index: number,
|
|
136
|
+
value: unknown,
|
|
137
|
+
options?: unknown,
|
|
138
|
+
) => form.insertFieldValue(resolveName(fieldName), index, value, options),
|
|
139
|
+
clearFieldValues: (fieldName: string, options?: unknown) =>
|
|
140
|
+
form.clearFieldValues(resolveName(fieldName), options),
|
|
141
|
+
removeFieldValue: (fieldName: string, index: number, options?: unknown) =>
|
|
142
|
+
form.removeFieldValue(resolveName(fieldName), index, options),
|
|
143
|
+
filterFieldValues: (
|
|
144
|
+
fieldName: string,
|
|
145
|
+
predicate: (...args: Array<any>) => boolean,
|
|
146
|
+
options?: unknown,
|
|
147
|
+
) => form.filterFieldValues(resolveName(fieldName), predicate, options),
|
|
148
|
+
} as never
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export const helperRuntime = {
|
|
152
|
+
strict: () => null,
|
|
153
|
+
loose: () => null,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function defineFieldsRuntime<TFields>(fields: TFields): TFields {
|
|
157
|
+
return fields
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function withFieldsRuntime(
|
|
161
|
+
_fields: AnyFieldGroupApi,
|
|
162
|
+
Component: (props: any) => unknown,
|
|
163
|
+
fieldsPropName: string,
|
|
164
|
+
) {
|
|
165
|
+
const fieldNames = Object.keys(_fields as unknown as Record<string, unknown>)
|
|
166
|
+
|
|
167
|
+
const FieldGroupComponent = (props: any) => {
|
|
168
|
+
const { form, ...restProps } = props
|
|
169
|
+
const bindingsRef = Preact.useRef<FieldBindings>(props[fieldsPropName])
|
|
170
|
+
|
|
171
|
+
if (!form) {
|
|
172
|
+
throw new Error('TanStack Form: Field groups must receive a `form` prop.')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
bindingsRef.current = props[fieldsPropName]
|
|
176
|
+
|
|
177
|
+
const fieldGroupApi = Preact.useMemo(
|
|
178
|
+
() => createFieldGroupApi(form as never, bindingsRef, fieldNames),
|
|
179
|
+
[form],
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
return Component({
|
|
183
|
+
...restProps,
|
|
184
|
+
[fieldsPropName]: fieldGroupApi,
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
FieldGroupComponent.displayName = 'TanStackForm.FieldGroup'
|
|
189
|
+
|
|
190
|
+
return FieldGroupComponent
|
|
191
|
+
}
|