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