Open History to see the last commit
1import { shallow, useSelector } from '@tanstack/vue-store'
2import { InternalFormGroupApi } from '@tanstack/form-core/internals'
3import {
4 defineComponent,
5 h,
6 onMounted,
7 onUnmounted,
8 provide,
9 watchEffect,
10} from 'vue'
11import { Subscribe } from '../Subscribe.public'
12import {
13 createArrayFieldSubscription,
14 createValueFieldSubscription,
15} from './fieldSubscriptions.lib'
16import { createFieldView } from './fieldView.lib'
17import { useField } from './useField.lib'
18import type { AnyFieldApi } from '@tanstack/form-core'
19import type { Component, InjectionKey, Slots } from 'vue'
20import type {
21 AnyFieldApiOptions,
22 AnyInternalFormApi,
23 InternalFormGroupApi as InternalFormGroupApiType,
24} from '@tanstack/form-core/internals'
25import type { InternalVueFormApi } from './VueFormApi.lib'
26β
27export function attachVueFormComponents(
28 form: AnyInternalFormApi,
29 fieldComponents: Record<string, Component> | null,
30 fieldContext?: InjectionKey<AnyFieldApi>,
31): InternalVueFormApi {
32 const resultForm = form as InternalVueFormApi
33 resultForm.Field = createFieldComponent(
34 form,
35 fieldComponents,
36 false,
37 fieldContext,
38 ) as never
39 resultForm.ArrayField = createFieldComponent(
40 form,
41 fieldComponents,
42 true,
43 fieldContext,
44 ) as never
45 resultForm.Subscribe = createSubscribeComponent(form) as never
46 resultForm.FormGroup = createFormGroupComponent(resultForm) as never
47 return resultForm
48}
49β
50function createFieldComponent(
51 form: AnyInternalFormApi,
52 fieldComponents: Record<string, Component> | null,
53 array: boolean,
54 fieldContext?: InjectionKey<AnyFieldApi>,
55) {
56 return defineComponent(
57 (_props, context) => {
58 const options = () => ({ ...context.attrs, form }) as never
59 const fieldApi = useField(options)
60 const { selection, meta } = array
61 ? createArrayFieldSubscription(fieldApi)
62 : createValueFieldSubscription(fieldApi)
63 const field = createFieldView(fieldApi, selection, meta)
64 if (fieldComponents !== null) Object.assign(field, fieldComponents)
65β
66 if (fieldContext) {
67 provide(fieldContext, field)
68 }
69β
70 return () => {
71 void selection.value
72 return context.slots.default?.({ field })
73 }
74 },
75 {
76 name: array ? 'TanStackForm.ArrayField' : 'TanStackForm.Field',
77 inheritAttrs: false,
78 },
79 )
80}
81β
82function createSubscribeComponent(form: AnyInternalFormApi) {
83 return defineComponent(
84 (_props, context) => () =>
85 h(
86 Subscribe as never,
87 { ...context.attrs, source: form.atom } as never,
88 context.slots,
89 ),
90 {
91 name: 'TanStackForm.Subscribe',
92 inheritAttrs: false,
93 },
94 )
95}
96β
97function createFormGroupComponent(form: InternalVueFormApi) {
98 return defineComponent(
99 (_props, context) => {
100 const options = () => ({ ...context.attrs, form })
101 const group = attachVueFormGroupComponents(
102 new InternalFormGroupApi(options() as never),
103 form,
104 )
105β
106 watchEffect(() => group.update(options() as never))
107β
108 let mounted = false
109 onMounted(() => {
110 mounted = true
111 group.mount()
112 })
113 onUnmounted(() => {
114 if (mounted) group._cleanup()
115 })
116β
117 const state = useSelector(group.atom, (value) => value, {
118 compare: shallow,
119 })
120β
121 return () => {
122 void state.value
123 return context.slots.default?.({ group })
124 }
125 },
126 {
127 name: 'TanStackForm.FormGroup',
128 inheritAttrs: false,
129 },
130 )
131}
132β
133function forwardToField(
134 component: Component,
135 group: InternalFormGroupApiType<any, any, any, any, any>,
136) {
137 return defineComponent(
138 (_props, context) => () => {
139 const options = { ...context.attrs } as unknown as AnyFieldApiOptions
140 return h(
141 component,
142 group._getFormFieldOptions(options, (base, overrides) => ({
143 ...base,
144 ...overrides,
145 })) as never,
146 context.slots,
147 )
148 },
149 { inheritAttrs: false },
150 )
151}
152β
153function attachVueFormGroupComponents(
154 group: InternalFormGroupApiType<any, any, any, any, any>,
155 form: InternalVueFormApi,
156) {
157 type GroupWithComponents = InternalFormGroupApiType<
158 any,
159 any,
160 any,
161 any,
162 any
163 > & {
164 Field: Component
165 ArrayField: Component
166 Subscribe: Component
167 }
168β
169 const resultGroup = group as GroupWithComponents
170 resultGroup.Field = forwardToField(form.Field as never, group)
171 resultGroup.ArrayField = forwardToField(form.ArrayField as never, group)
172 resultGroup.Subscribe = defineComponent(
173 (_props, context) => () =>
174 h(
175 Subscribe as never,
176 { ...context.attrs, source: group.atom } as never,
177 context.slots as Slots,
178 ),
179 { name: 'TanStackForm.FormGroup.Subscribe', inheritAttrs: false },
180 )
181 return resultGroup
182}