Open History to see the last commit
1import { useArrayFieldSubscription } from '../ReactForm/fieldSubscriptions.lib'
2import { InternalReactFormApi } from '../ReactForm/ReactFormApi.lib'
3import { createInternalReactAppFieldApiClass } from './ReactAppFieldApi.lib'
4import {
5 createAppForm,
6 createFieldWithContext,
7 createFormGroupWithContext,
8} from './Components.lib'
9import type { DefaultOptions, FormOptions } from '@tanstack/form-core'
10import type { AppFormComponent } from './ReactAppFormApi.public'
11import type { ReactComponentTree } from './componentMap.public'
12β
13interface InternalReactAppFormApiInstance extends InternalReactFormApi {
14 AppForm: AppFormComponent
15}
16β
17type InternalReactAppFormApiConstructor<
18 TFormComponents extends ReactComponentTree,
19> = new (
20 options: FormOptions<any, any, any, unknown>,
21 defaultOptions?: DefaultOptions,
22) => InternalReactAppFormApiInstance & TFormComponents
23β
24export function createInternalReactAppFormApiClass<
25 const TFormComponents extends ReactComponentTree,
26>(
27 formComponents: TFormComponents,
28 fieldComponents: ReactComponentTree,
29): InternalReactAppFormApiConstructor<TFormComponents> {
30 const InternalReactAppFieldApi =
31 createInternalReactAppFieldApiClass(fieldComponents)
32β
33 class InternalReactAppFormApi
34 extends InternalReactFormApi
35 implements InternalReactAppFormApiInstance
36 {
37 AppForm: AppFormComponent
38β
39 override get _FieldApi(): typeof InternalReactAppFieldApi {
40 return InternalReactAppFieldApi
41 }
42β
43 constructor(
44 options: FormOptions<any, any, any, unknown>,
45 defaultOptions?: DefaultOptions,
46 ) {
47 super(options, defaultOptions)
48β
49 const field = createFieldWithContext(this, 'field')
50 const arrayField = createFieldWithContext(
51 this,
52 'field',
53 useArrayFieldSubscription,
54 )
55 const groupField = createFieldWithContext(this, 'field')
56 const groupArrayField = createFieldWithContext(
57 this,
58 'field',
59 useArrayFieldSubscription,
60 )
61 arrayField.displayName = 'TanStackForm.ArrayField'
62 groupArrayField.displayName = 'TanStackForm.ArrayField'
63β
64 this.AppForm = createAppForm(this)
65 this.Field = field as InternalReactAppFormApi['Field']
66 this.ArrayField = arrayField as InternalReactAppFormApi['ArrayField']
67 this.FormGroup = createFormGroupWithContext(
68 this,
69 groupField,
70 groupArrayField,
71 ) as InternalReactAppFormApi['FormGroup']
72 }
73 }
74β
75 Object.assign(InternalReactAppFormApi.prototype, formComponents)
76β
77 return InternalReactAppFormApi as unknown as InternalReactAppFormApiConstructor<TFormComponents>
78}