Open History to see the last commit
1import Preact, { useEffect } from 'preact/compat'
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 { AnyInternalFormApi } from '@tanstack/form-core/internals'
10import type { InternalPreactFormApi } from './PreactFormApi.lib'
11import type { FunctionComponent } from 'preact/compat'
12import type {
13 PreactFormFieldProps,
14 PreactFormGroupProps,
15 PreactFormSubscribeProps,
16} from './Components.public'
17β
18export 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β
36type AnyFieldComponent = FunctionComponent<
37 PreactFormFieldProps<any, any, any, any, never, any, any, any>
38>
39β
40function 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β
56type AnyArrayFieldComponent = FunctionComponent<any>
57β
58function 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β
74type AnySubscribeComponent = FunctionComponent<
75 PreactFormSubscribeProps<any, any, any>
76>
77β
78function 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β
90type AnyFormGroupComponent = FunctionComponent<
91 PreactFormGroupProps<any, any, any, any, any, any>
92>
93β
94function 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β
124function 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}