@tanstack/preact-form 2.0.0-alpha.0 → 2.0.0-alpha.1

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.
@@ -1,114 +0,0 @@
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
- }
@@ -1,22 +0,0 @@
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
- }
@@ -1,91 +0,0 @@
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>
@@ -1,109 +0,0 @@
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
- }
@@ -1,203 +0,0 @@
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
@@ -1,166 +0,0 @@
1
- import Preact, { useEffect } from 'preact/compat'
2
- import { InternalFormGroupApi } from '@tanstack/form-core/internals'
3
- import { Subscribe } from '../Subscribe.public'
4
- import {
5
- useArrayFieldSubscription,
6
- useValueFieldSubscription,
7
- } from './fieldSubscriptions.lib'
8
- import { useField } from './useField.lib'
9
- import type { AnyInternalFormApi } from '@tanstack/form-core/internals'
10
- import type { InternalPreactFormApi } from './PreactFormApi.lib'
11
- import type { FunctionComponent } from 'preact/compat'
12
- import type {
13
- PreactFormFieldProps,
14
- PreactFormGroupProps,
15
- PreactFormSubscribeProps,
16
- } from './Components.public'
17
-
18
- export function attachPreactFormComponents(
19
- form: AnyInternalFormApi,
20
- fieldComponents: Record<string, FunctionComponent<any>> | null,
21
- ): InternalPreactFormApi {
22
- const resultForm = form as InternalPreactFormApi
23
- resultForm.Field = createFieldComponent(
24
- form,
25
- fieldComponents,
26
- ) as InternalPreactFormApi['Field']
27
- resultForm.ArrayField = createArrayFieldComponent(form, fieldComponents)
28
- resultForm.Subscribe = createSubscribeComponent(form)
29
- resultForm.FormGroup = createFormGroupComponent(
30
- resultForm,
31
- ) as InternalPreactFormApi['FormGroup']
32
-
33
- return resultForm
34
- }
35
-
36
- type AnyFieldComponent = FunctionComponent<
37
- PreactFormFieldProps<any, any, any, any, never, any, any, any>
38
- >
39
-
40
- function createFieldComponent(
41
- form: AnyInternalFormApi,
42
- fieldComponents: Record<string, FunctionComponent<any>> | null,
43
- ): AnyFieldComponent {
44
- const TanStackFormField: AnyFieldComponent = (props) => {
45
- const fieldApi = useField({ ...props, form }, fieldComponents)
46
- const field = useValueFieldSubscription(fieldApi)
47
-
48
- return props.children(field)
49
- }
50
-
51
- TanStackFormField.displayName = 'TanStackForm.Field'
52
-
53
- return TanStackFormField
54
- }
55
-
56
- type AnyArrayFieldComponent = FunctionComponent<any>
57
-
58
- function createArrayFieldComponent(
59
- form: AnyInternalFormApi,
60
- fieldComponents: Record<string, FunctionComponent<any>> | null,
61
- ): AnyArrayFieldComponent {
62
- const TanStackFormArrayField: AnyArrayFieldComponent = (props) => {
63
- const fieldApi = useField({ ...props, form }, fieldComponents)
64
- const field = useArrayFieldSubscription(fieldApi)
65
-
66
- return props.children(field)
67
- }
68
-
69
- TanStackFormArrayField.displayName = 'TanStackForm.ArrayField'
70
-
71
- return TanStackFormArrayField
72
- }
73
-
74
- type AnySubscribeComponent = FunctionComponent<
75
- PreactFormSubscribeProps<any, any, any>
76
- >
77
-
78
- function createSubscribeComponent(
79
- form: AnyInternalFormApi,
80
- ): AnySubscribeComponent {
81
- const TanStackFormSubscribe: AnySubscribeComponent = (props) => {
82
- return <Subscribe source={form.atom} {...props} />
83
- }
84
-
85
- TanStackFormSubscribe.displayName = 'TanStackForm.Subscribe'
86
-
87
- return TanStackFormSubscribe
88
- }
89
-
90
- type AnyFormGroupComponent = FunctionComponent<
91
- PreactFormGroupProps<any, any, any, any, any, any>
92
- >
93
-
94
- function createFormGroupComponent(
95
- form: InternalPreactFormApi,
96
- ): AnyFormGroupComponent {
97
- const TanStackFormGroup: AnyFormGroupComponent = (props) => {
98
- const groupRef =
99
- Preact.useRef<InternalFormGroupApi<any, any, any, any, any>>(null)
100
-
101
- if (!groupRef.current) {
102
- groupRef.current = attachPreactFormGroupComponents(
103
- new InternalFormGroupApi({ ...props, form } as never),
104
- form,
105
- )
106
- }
107
-
108
- useEffect(() => groupRef.current?.update({ ...props, form }))
109
-
110
- Preact.useEffect(() => {
111
- const group = groupRef.current!
112
- group.mount()
113
- return () => group._cleanup()
114
- }, [])
115
-
116
- return props.children(groupRef.current as never)
117
- }
118
-
119
- TanStackFormGroup.displayName = 'TanStackForm.FormGroup'
120
-
121
- return TanStackFormGroup
122
- }
123
-
124
- function attachPreactFormGroupComponents(
125
- group: InternalFormGroupApi<any, any, any, any, any>,
126
- form: InternalPreactFormApi,
127
- ) {
128
- type FormGroupComponents = InternalFormGroupApi<any, any, any, any, any> & {
129
- Field: FunctionComponent<any>
130
- ArrayField: FunctionComponent<any>
131
- Subscribe: FunctionComponent<any>
132
- }
133
-
134
- const resultGroup: FormGroupComponents = group as never
135
-
136
- resultGroup.Field = function Field(props) {
137
- return (
138
- <form.Field
139
- {...(group._getFormFieldOptions(props, (base, overrides) => ({
140
- ...base,
141
- ...overrides,
142
- })) as any)}
143
- />
144
- )
145
- }
146
- resultGroup.Field.displayName = 'TanStackForm.FormGroup.Field'
147
-
148
- resultGroup.ArrayField = function ArrayField(props) {
149
- return (
150
- <form.ArrayField
151
- {...(group._getFormFieldOptions(props, (base, overrides) => ({
152
- ...base,
153
- ...overrides,
154
- })) as any)}
155
- />
156
- )
157
- }
158
- resultGroup.ArrayField.displayName = 'TanStackForm.FormGroup.ArrayField'
159
-
160
- resultGroup.Subscribe = (props) => {
161
- return <Subscribe source={group.atom} {...props} />
162
- }
163
- resultGroup.Subscribe.displayName = 'TanStackForm.FormGroup.Subscribe'
164
-
165
- return resultGroup
166
- }