Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

SolidFormApi.lib.ts

175 lines4.6 KB

1import {
2 InternalFormApi,
3 InternalFormGroupApi,
4} from '@tanstack/form-core/internals'
5import { shallow, useSelector } from '@tanstack/solid-store'
6import {
7 createComponent,
8 createMemo,
9 createRenderEffect,
10 createUniqueId,
11 mergeProps,
12 onCleanup,
13 untrack,
14} from 'solid-js'
15import { Subscribe } from './Subscribe.public'
16import { createArrayField, createField } from './createField.lib'
17import type {
18 AnyInternalFormApi,
19 InternalFormGroupApi as InternalFormGroupApiType,
20} from '@tanstack/form-core/internals'
21import type { FormOptions } from '@tanstack/form-core'
22import type { Component } from 'solid-js'
23import type {
24 SolidFormFieldProps,
25 SolidFormGroupProps,
26 SolidFormSubscribeProps,
27 SolidTanStackFormComponents,
28} from './Components.public'
29​
30export interface InternalSolidFormApi
31 extends AnyInternalFormApi, SolidTanStackFormComponents<any, any, any> {}
32​
33export function attachSolidFormComponents(
34 form: AnyInternalFormApi,
35 fieldComponents: Record<string, Component<any>> | null,
36): InternalSolidFormApi {
37 const solidFormApi = form as InternalSolidFormApi
38​
39 solidFormApi.Field = function TanStackFormField(
40 props: SolidFormFieldProps<any, any, any, any, any, any, any, any>,
41 ) {
42 const fieldOptions = mergeProps(props, { form })
43 const fieldApi = createField(() => fieldOptions as never)
44 if (fieldComponents !== null) Object.assign(fieldApi, fieldComponents)
45​
46 return props.children(fieldApi as never)
47 } as never
48​
49 solidFormApi.ArrayField = function TanStackFormArrayField(
50 props: SolidFormFieldProps<any, any, any, any, any, any, any, any>,
51 ) {
52 const fieldOptions = mergeProps(props, { form })
53 const fieldApi = createArrayField(() => fieldOptions as never)
54 if (fieldComponents !== null) Object.assign(fieldApi, fieldComponents)
55​
56 return props.children(fieldApi as never)
57 } as never
58​
59 solidFormApi.Subscribe = function TanStackFormSubscribe(
60 props: SolidFormSubscribeProps<any, any, any>,
61 ) {
62 return createComponent(
63 Subscribe as Component<any>,
64 mergeProps(props, { source: solidFormApi.atom }),
65 )
66 }
67​
68 solidFormApi.FormGroup = function TanStackFormGroup(
69 props: SolidFormGroupProps<any, any, any, any, any, any>,
70 ) {
71 const group = attachSolidFormGroupComponents(
72 new InternalFormGroupApi({ ...props, form } as never),
73 solidFormApi,
74 )
75​
76 createRenderEffect(() => group.update({ ...props, form } as never))
77 group.mount()
78 onCleanup(() => group._cleanup())
79​
80 const state = useSelector(group.atom, (value) => value, {
81 compare: shallow,
82 })
83 const groupAccessor = createMemo(
84 () => {
85 state()
86 return group
87 },
88 undefined,
89 { equals: false },
90 )
91​
92 Object.assign(groupAccessor, {
93 Field: group.Field,
94 ArrayField: group.ArrayField,
95 Subscribe: group.Subscribe,
96 })
97​
98 return props.children(groupAccessor as never)
99 }
100​
101 return solidFormApi
103​
104function attachSolidFormGroupComponents(
105 group: InternalFormGroupApiType<any, any, any, any, any>,
106 form: InternalSolidFormApi,
107) {
108 type GroupWithComponents = InternalFormGroupApiType<
109 any,
110 any,
111 any,
112 any,
113 any
114 > & {
115 Field: Component<any>
116 ArrayField: Component<any>
117 Subscribe: Component<any>
118 }
119​
120 const resultGroup = group as GroupWithComponents
121​
122 resultGroup.Field = function Field(props) {
123 return createComponent(
124 form.Field as Component<any>,
125 group._getFormFieldOptions(props, mergeProps) as any,
126 )
127 }
128​
129 resultGroup.ArrayField = function ArrayField(props) {
130 return createComponent(
131 form.ArrayField as Component<any>,
132 group._getFormFieldOptions(props, mergeProps) as any,
133 )
134 }
135​
136 resultGroup.Subscribe = function GroupSubscribe(props) {
137 return createComponent(
138 Subscribe as Component<any>,
139 mergeProps(props, { source: group.atom }),
140 )
141 }
142​
143 return resultGroup
145​
146export function initializeForm(
147 options: FormOptions<any, any, any, unknown>,
148): InternalSolidFormApi {
149 return attachSolidFormComponents(new InternalFormApi(options), null)
151​
152export function createInternalForm(
153 options: () => FormOptions<any, any, any, unknown>,
154 initializeFn: (
155 options: FormOptions<any, any, any, unknown>,
156 ) => InternalSolidFormApi,
157) {
158 const solidFormId = createUniqueId()
159 const resolveOptions = () => {
160 const current = options()
161 return current.formId === undefined
162 ? { ...current, formId: solidFormId }
163 : current
164 }
165 const form = untrack(() => initializeFn(resolveOptions()))
166​
167 createRenderEffect(() => {
168 form._update(resolveOptions() as never)
169 })
170​
171 const unmount = form.mount()
172 onCleanup(unmount)
173​
174 return form