Open History to see the last commit
1import { getContext } from 'svelte'
2import { createForm } from './createForm.svelte'
3import AppFormSvelte from './AppForm.svelte'
4import AppFieldSvelte from './AppField.svelte'
5import { fieldContextKey, formContextKey } from './context-keys.js'
6import type {
7 AnyFieldApi,
8 AnyFormApi,
9 FieldApi,
10 FormAsyncValidateOrFn,
11 FormOptions,
12 FormValidateOrFn,
13} from '@tanstack/form-core'
14import type { FieldComponent } from './types.js'
15import type { SvelteFormExtendedApi } from './createForm.svelte'
16import type { Component, Snippet, SvelteComponent } from 'svelte'
17β
18/**
19 * TypeScript inferencing is weird.
20 *
21 * If you have:
22 *
23 * @example
24 *
25 * interface Args<T> {
26 * arg?: T
27 * }
28 *
29 * function test<T>(arg?: Partial<Args<T>>): T {
30 * return 0 as any;
31 * }
32 *
33 * const a = test({});
34 *
35 * Then `T` will default to `unknown`.
36 *
37 * However, if we change `test` to be:
38 *
39 * @example
40 *
41 * function test<T extends undefined>(arg?: Partial<Args<T>>): T;
42 *
43 * Then `T` becomes `undefined`.
44 *
45 * Here, we are checking if the passed type `T` extends `DefaultT` and **only**
46 * `DefaultT`, as if that's the case we assume that inferencing has not occured.
47 */
48type UnwrapOrAny<T> = [unknown] extends [T] ? any : T
49type UnwrapDefaultOrAny<DefaultT, T> = [DefaultT] extends [T]
50 ? [T] extends [DefaultT]
51 ? any
52 : T
53 : T
54β
55export function createFormCreatorContexts() {
56 function useFieldContext<TData>() {
57 const field = getContext(fieldContextKey) as AnyFieldApi
58β
59 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
60 if (!field) {
61 throw new Error(
62 '`fieldContext` only works when within a `fieldComponent` passed to `createFormCreator`',
63 )
64 }
65β
66 return field as FieldApi<
67 any,
68 string,
69 TData,
70 any,
71 any,
72 any,
73 any,
74 any,
75 any,
76 any,
77 any,
78 any,
79 any,
80 any,
81 any,
82 any,
83 any,
84 any,
85 any,
86 any,
87 any,
88 any,
89 any
90 >
91 }
92β
93 function useFormContext() {
94 const form = getContext(formContextKey) as AnyFormApi
95β
96 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
97 if (!form) {
98 throw new Error(
99 '`formContext` only works when within a `formComponent` passed to `createFormCreator`',
100 )
101 }
102β
103 return form as SvelteFormExtendedApi<
104 // If you need access to the form data, you need to use `withForm` instead
105 Record<string, never>,
106 any,
107 any,
108 any,
109 any,
110 any,
111 any,
112 any,
113 any,
114 any,
115 any,
116 any
117 >
118 }
119β
120 return { useFieldContext, useFormContext }
121}
122β
123interface CreateFormRuneProps<
124 TFieldComponents extends Record<string, Component<any, any>>,
125 TFormComponents extends Record<string, Component<any, any>>,
126> {
127 fieldComponents: TFieldComponents
128 formComponents: TFormComponents
129}
130β
131/**
132 * @private
133 */
134type AppFieldExtendedSvelteFormApi<
135 TFormData,
136 TOnMount extends undefined | FormValidateOrFn<TFormData>,
137 TOnChange extends undefined | FormValidateOrFn<TFormData>,
138 TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
139 TOnBlur extends undefined | FormValidateOrFn<TFormData>,
140 TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
141 TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
142 TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
143 TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
144 TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
145 TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
146 TSubmitMeta,
147 TFieldComponents extends Record<string, Component<any, any>>,
148 TFormComponents extends Record<string, Component<any, any>>,
149> = SvelteFormExtendedApi<
150 TFormData,
151 TOnMount,
152 TOnChange,
153 TOnChangeAsync,
154 TOnBlur,
155 TOnBlurAsync,
156 TOnSubmit,
157 TOnSubmitAsync,
158 TOnDynamic,
159 TOnDynamicAsync,
160 TOnServer,
161 TSubmitMeta
162> &
163 NoInfer<TFormComponents> & {
164 AppField: FieldComponent<
165 TFormData,
166 TOnMount,
167 TOnChange,
168 TOnChangeAsync,
169 TOnBlur,
170 TOnBlurAsync,
171 TOnSubmit,
172 TOnSubmitAsync,
173 TOnDynamic,
174 TOnDynamicAsync,
175 TOnServer,
176 TSubmitMeta,
177 NoInfer<TFieldComponents>
178 >
179 AppForm: Component<{ children: Snippet }>
180 }
181β
182export function createFormCreator<
183 const TComponents extends Record<string, Component<any, any>>,
184 const TFormComponents extends Record<string, Component<any, any>>,
185>({
186 fieldComponents,
187 formComponents,
188}: CreateFormRuneProps<TComponents, TFormComponents>) {
189 function createAppForm<
190 TFormData,
191 TOnMount extends undefined | FormValidateOrFn<TFormData>,
192 TOnChange extends undefined | FormValidateOrFn<TFormData>,
193 TOnChangeAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
194 TOnBlur extends undefined | FormValidateOrFn<TFormData>,
195 TOnBlurAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
196 TOnSubmit extends undefined | FormValidateOrFn<TFormData>,
197 TOnSubmitAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
198 TOnDynamic extends undefined | FormValidateOrFn<TFormData>,
199 TOnDynamicAsync extends undefined | FormAsyncValidateOrFn<TFormData>,
200 TOnServer extends undefined | FormAsyncValidateOrFn<TFormData>,
201 TSubmitMeta,
202 >(
203 props: () => FormOptions<
204 TFormData,
205 TOnMount,
206 TOnChange,
207 TOnChangeAsync,
208 TOnBlur,
209 TOnBlurAsync,
210 TOnSubmit,
211 TOnSubmitAsync,
212 TOnDynamic,
213 TOnDynamicAsync,
214 TOnServer,
215 TSubmitMeta
216 >,
217 ): AppFieldExtendedSvelteFormApi<
218 TFormData,
219 TOnMount,
220 TOnChange,
221 TOnChangeAsync,
222 TOnBlur,
223 TOnBlurAsync,
224 TOnSubmit,
225 TOnSubmitAsync,
226 TOnDynamic,
227 TOnDynamicAsync,
228 TOnServer,
229 TSubmitMeta,
230 TComponents,
231 TFormComponents
232 > {
233 const form = createForm(props)
234β
235 const AppForm = ((internal, props) => {
236 return AppFormSvelte(internal, { ...props, form })
237 }) as Component<{ children: Snippet }>
238β
239 const AppField = ((internal, { children, ...fieldProps }) =>
240 AppFieldSvelte(internal, {
241 fieldProps,
242 form,
243 fieldComponents,
244 children,
245 } as never)) as FieldComponent<
246 TFormData,
247 TOnMount,
248 TOnChange,
249 TOnChangeAsync,
250 TOnBlur,
251 TOnBlurAsync,
252 TOnSubmit,
253 TOnSubmitAsync,
254 TOnDynamic,
255 TOnDynamicAsync,
256 TOnServer,
257 TSubmitMeta,
258 TComponents
259 >
260β
261 const extendedForm = Object.assign(form, {
262 AppField,
263 AppForm,
264 ...formComponents,
265 })
266β
267 return extendedForm
268 }
269β
270 return {
271 createAppForm,
272 }
273}