@tanstack/preact-form 1.33.3 → 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.
Files changed (51) hide show
  1. package/dist/AppForm/Components.lib.js +3 -2
  2. package/dist/AppForm/PreactAppFormApi.public.d.ts +0 -1
  3. package/dist/AppForm/appFormOptions.public.d.ts +0 -1
  4. package/dist/AppForm/componentMap.public.d.ts +0 -1
  5. package/dist/AppForm/createFormHook.public.d.ts +0 -1
  6. package/dist/AppForm/createFormHook.public.js +2 -2
  7. package/dist/AppForm/createFormHookTypes.public.d.ts +110 -3
  8. package/dist/AppForm/fieldComponentHelpers.lib.js +3 -2
  9. package/dist/AppForm/getFormHookHelpers.public.d.ts +1 -2
  10. package/dist/AppForm/initializeAppForm.lib.js +2 -1
  11. package/dist/FieldGroup/FieldGroupApi.public.d.ts +0 -1
  12. package/dist/FieldGroup/withFields.lib.js +27 -63
  13. package/dist/FieldGroup/withFields.public.d.ts +26 -24
  14. package/dist/FieldGroup/withFields.public.js +4 -15
  15. package/dist/PreactForm/Components.lib.js +6 -4
  16. package/dist/PreactForm/Components.public.d.ts +2 -3
  17. package/dist/PreactForm/PreactFormApi.lib.js +2 -1
  18. package/dist/PreactForm/fieldSubscriptions.lib.js +4 -2
  19. package/dist/PreactForm/formApiTypes.public.d.ts +25 -2
  20. package/dist/PreactForm/formType.public.d.ts +30 -2
  21. package/dist/PreactForm/useForm.public.d.ts +0 -1
  22. package/dist/Subscribe.public.d.ts +0 -1
  23. package/dist/index.d.ts +2 -2
  24. package/dist/index.js +2 -2
  25. package/dist/preactTypes.public.d.ts +0 -2
  26. package/package.json +5 -6
  27. package/src/AppForm/Components.lib.tsx +0 -160
  28. package/src/AppForm/PreactAppFormApi.public.tsx +0 -17
  29. package/src/AppForm/appFormOptions.public.ts +0 -59
  30. package/src/AppForm/componentMap.public.ts +0 -19
  31. package/src/AppForm/contexts.lib.ts +0 -28
  32. package/src/AppForm/createFormHook.public.ts +0 -37
  33. package/src/AppForm/createFormHookTypes.public.ts +0 -34
  34. package/src/AppForm/fieldComponentHelpers.lib.tsx +0 -30
  35. package/src/AppForm/getFormHookHelpers.public.ts +0 -114
  36. package/src/AppForm/initializeAppForm.lib.ts +0 -22
  37. package/src/FieldGroup/FieldGroupApi.public.ts +0 -91
  38. package/src/FieldGroup/withFields.lib.ts +0 -191
  39. package/src/FieldGroup/withFields.public.ts +0 -221
  40. package/src/PreactForm/Components.lib.tsx +0 -166
  41. package/src/PreactForm/Components.public.ts +0 -383
  42. package/src/PreactForm/PreactFormApi.lib.tsx +0 -48
  43. package/src/PreactForm/fieldSubscriptions.lib.ts +0 -32
  44. package/src/PreactForm/formApiTypes.public.ts +0 -29
  45. package/src/PreactForm/formType.public.ts +0 -52
  46. package/src/PreactForm/useField.lib.ts +0 -52
  47. package/src/PreactForm/useForm.public.ts +0 -34
  48. package/src/Subscribe.public.tsx +0 -56
  49. package/src/index.ts +0 -18
  50. package/src/preactTypes.public.ts +0 -6
  51. package/src/types.public.ts +0 -2
@@ -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,191 +0,0 @@
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
- }
@@ -1,221 +0,0 @@
1
- import {
2
- defineFieldsRuntime,
3
- helperRuntime,
4
- withFieldsRuntime,
5
- } from './withFields.lib'
6
- import type {
7
- DeepKeys,
8
- DeepKeysWhereValueIncludes,
9
- DeepValue,
10
- FormApi,
11
- } from '@tanstack/form-core'
12
- import type { FunctionComponent } from 'preact/compat'
13
- import type { CrossVersionPreactNode } from '../preactTypes.public'
14
- import type { PreactTanStackFormComponents } from '../PreactForm/Components.public'
15
- import type { FieldGroupApi } from './FieldGroupApi.public'
16
-
17
- declare const fieldGroupFieldSlotValueSymbol: unique symbol
18
- declare const fieldGroupFieldsSymbol: unique symbol
19
-
20
- export type FieldGroupFieldSlotMode = 'strict' | 'loose'
21
-
22
- export interface FieldGroupFieldSlot<
23
- out TValue,
24
- out TMode extends FieldGroupFieldSlotMode = FieldGroupFieldSlotMode,
25
- > {
26
- readonly mode: TMode
27
- readonly [fieldGroupFieldSlotValueSymbol]: TValue
28
- }
29
-
30
- export type AnyFieldGroupFieldSlot = FieldGroupFieldSlot<any>
31
-
32
- export type StrictFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
33
- TValue,
34
- 'strict'
35
- >
36
-
37
- export type LooseFieldGroupFieldSlot<TValue> = FieldGroupFieldSlot<
38
- TValue,
39
- 'loose'
40
- >
41
-
42
- export type FieldGroupFieldSlotValue<TSlot> =
43
- TSlot extends FieldGroupFieldSlot<infer TValue> ? TValue : never
44
-
45
- export type FieldGroupFieldSlotModeOf<TSlot> =
46
- TSlot extends FieldGroupFieldSlot<any, infer TMode> ? TMode : never
47
-
48
- type IsSame<TTypeA, TTypeB> = [TTypeA] extends [TTypeB]
49
- ? [TTypeB] extends [TTypeA]
50
- ? true
51
- : false
52
- : false
53
-
54
- export type FieldGroupFieldSlotAllows<TSlot, TValue> =
55
- TSlot extends FieldGroupFieldSlot<infer TAcceptedValue, infer TMode>
56
- ? TMode extends 'strict'
57
- ? IsSame<TValue, TAcceptedValue>
58
- : [TValue] extends [TAcceptedValue]
59
- ? true
60
- : false
61
- : false
62
-
63
- export type FieldGroupFieldNameForSlot<
64
- TFieldData,
65
- TSlot extends AnyFieldGroupFieldSlot,
66
- > = {
67
- [TFieldName in DeepKeys<TFieldData>]: FieldGroupFieldSlotAllows<
68
- TSlot,
69
- DeepValue<TFieldData, TFieldName>
70
- > extends true
71
- ? TFieldName
72
- : never
73
- }[DeepKeys<TFieldData>]
74
-
75
- export type FieldGroupFields = Record<string, AnyFieldGroupFieldSlot>
76
-
77
- export type FieldGroupFieldNames<
78
- TFieldData,
79
- TFields extends FieldGroupFields,
80
- > = {
81
- [TFieldName in keyof TFields]: FieldGroupFieldNameForSlot<
82
- TFieldData,
83
- TFields[TFieldName]
84
- >
85
- }
86
-
87
- export type FieldGroupFieldData<TFields extends FieldGroupFields> = {
88
- [
89
- TFieldName in keyof TFields
90
- ]: TFields[TFieldName] extends FieldGroupFieldSlot<infer TValue, any>
91
- ? TValue
92
- : never
93
- }
94
-
95
- export type FieldGroupFieldsOf<TFieldGroup> = TFieldGroup extends {
96
- readonly [fieldGroupFieldsSymbol]: infer TFields
97
- }
98
- ? TFields
99
- : never
100
-
101
- export type FieldGroupDefinition<
102
- TFields extends FieldGroupFields,
103
- TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
104
- never,
105
- never
106
- >,
107
- > = FieldGroupApi<FieldGroupFieldData<TFields>, TFieldComponents> & {
108
- readonly [fieldGroupFieldsSymbol]: TFields
109
- }
110
-
111
- export type FieldGroupFieldComponentsOf<TFieldGroup> =
112
- TFieldGroup extends FieldGroupDefinition<any, infer TFieldComponents>
113
- ? TFieldComponents
114
- : never
115
-
116
- export type FieldGroupForm<
117
- TFieldComponents extends Record<string, FunctionComponent<any>> = Record<
118
- never,
119
- never
120
- >,
121
- TFormData = any,
122
- > = FormApi<TFormData, any> &
123
- PreactTanStackFormComponents<TFormData, any, TFieldComponents>
124
-
125
- export type FieldGroupFieldBindingForSlot<
126
- TFormData,
127
- TSlot extends AnyFieldGroupFieldSlot,
128
- > =
129
- TSlot extends FieldGroupFieldSlot<infer TValue, infer TMode>
130
- ? TMode extends 'strict'
131
- ? FieldGroupFieldNameForSlot<TFormData, TSlot>
132
- : DeepKeysWhereValueIncludes<TFormData, TValue>
133
- : never
134
-
135
- export type FieldGroupFieldBindings<
136
- TFields extends FieldGroupFields,
137
- TFormData = any,
138
- > = {
139
- [TFieldName in keyof TFields]: FieldGroupFieldBindingForSlot<
140
- TFormData,
141
- TFields[TFieldName]
142
- >
143
- }
144
-
145
- export type FieldGroupFieldBindingsOf<TFieldGroup, TFormData> =
146
- FieldGroupFieldsOf<TFieldGroup> extends FieldGroupFields
147
- ? FieldGroupFieldBindings<FieldGroupFieldsOf<TFieldGroup>, TFormData>
148
- : never
149
-
150
- export type FieldGroupFieldsPropName<
151
- TProps,
152
- TFieldGroup extends FieldGroupDefinition<any, any>,
153
- > = {
154
- [TPropName in keyof TProps]-?: IsSame<
155
- TProps[TPropName],
156
- TFieldGroup
157
- > extends true
158
- ? TPropName
159
- : never
160
- }[keyof TProps]
161
-
162
- export type FieldGroupWithFieldsFn = <
163
- TFieldGroup extends FieldGroupDefinition<any, any>,
164
- TProps extends object,
165
- TFieldsPropName extends FieldGroupFieldsPropName<TProps, TFieldGroup>,
166
- >(
167
- fields: TFieldGroup,
168
- Component: (props: TProps) => CrossVersionPreactNode,
169
- fieldsPropName: TFieldsPropName,
170
- ) => <TFormData>(
171
- props: Omit<TProps, TFieldsPropName | 'form'> & {
172
- form: FieldGroupForm<FieldGroupFieldComponentsOf<TFieldGroup>, TFormData>
173
- } & {
174
- [TPropName in TFieldsPropName]: FieldGroupFieldBindingsOf<
175
- TFieldGroup,
176
- TFormData
177
- >
178
- },
179
- ) => CrossVersionPreactNode
180
-
181
- export interface FieldGroupHelper {
182
- strict: <TValue>() => StrictFieldGroupFieldSlot<TValue>
183
- loose: <TValue>() => LooseFieldGroupFieldSlot<TValue>
184
- }
185
-
186
- const helper: FieldGroupHelper = helperRuntime as never
187
-
188
- export type DefineFieldsFn<
189
- TFieldComponents extends Record<string, FunctionComponent<any>>,
190
- > = <const TFields extends FieldGroupFields>(
191
- fields: TFields,
192
- ) => FieldGroupDefinition<TFields, TFieldComponents>
193
-
194
- const defineFields: DefineFieldsFn<Record<never, never>> =
195
- defineFieldsRuntime as never
196
-
197
- const withFields: FieldGroupWithFieldsFn = withFieldsRuntime as never
198
-
199
- export interface FieldGroupHelpers<
200
- TFieldComponents extends Record<string, FunctionComponent<any>>,
201
- > {
202
- helper: FieldGroupHelper
203
- defineFields: DefineFieldsFn<TFieldComponents>
204
- withFields: FieldGroupWithFieldsFn
205
- }
206
-
207
- function createFieldGroupHelpers<
208
- TFieldComponents extends Record<string, FunctionComponent<any>>,
209
- >(): FieldGroupHelpers<TFieldComponents> {
210
- return {
211
- helper,
212
- defineFields: defineFields as never,
213
- withFields,
214
- }
215
- }
216
-
217
- export function getFieldGroupHelpers(): FieldGroupHelpers<
218
- Record<never, never>
219
- > {
220
- return createFieldGroupHelpers()
221
- }
@@ -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
- }