@@ -2,21 +2,17 @@ import { createForm } from './createForm.svelte'
22import type {
33 AnyFieldApi ,
44 AnyFormApi ,
5- BaseFormOptions ,
6- DeepKeysOfType ,
75 FieldApi ,
8- FieldsMap ,
96 FormAsyncValidateOrFn ,
107 FormOptions ,
118 FormValidateOrFn ,
129} from '@tanstack/form-core'
1310import type { FieldComponent } from './types.js'
1411import type { SvelteFormExtendedApi } from './createForm.svelte'
1512import { Component , getContext , Snippet , SvelteComponent } from 'svelte'
16-
17- // We should never hit the `null` case here
18- const fieldContextKey = "__tanstack_field_context_key"
19- const formContextKey = "__tanstack_form_context_key"
13+ import AppFormSvelte from './AppForm.svelte'
14+ import AppFieldSvelte from './AppField.svelte'
15+ import { fieldContextKey , formContextKey } from './context-keys.js'
2016
2117/**
2218 * TypeScript inferencing is weird.
@@ -51,8 +47,8 @@ const formContextKey = "__tanstack_form_context_key"
5147type UnwrapOrAny < T > = [ unknown ] extends [ T ] ? any : T
5248type UnwrapDefaultOrAny < DefaultT , T > = [ DefaultT ] extends [ T ]
5349 ? [ T ] extends [ DefaultT ]
54- ? any
55- : T
50+ ? any
51+ : T
5652 : T
5753
5854export function createFormHookContexts ( ) {
@@ -181,7 +177,7 @@ export type AppFieldExtendedReactFormApi<
181177 TSubmitMeta ,
182178 NoInfer < TFieldComponents >
183179 >
184- AppForm : Component < { children : Snippet } >
180+ AppForm : Component < { children : Snippet } >
185181 }
186182
187183export interface WithFormProps <
@@ -201,23 +197,23 @@ export interface WithFormProps<
201197 TFormComponents extends Record < string , Component < any , any > > ,
202198 TRenderProps extends object = Record < string , never > ,
203199> extends 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- > {
200+ TFormData ,
201+ TOnMount ,
202+ TOnChange ,
203+ TOnChangeAsync ,
204+ TOnBlur ,
205+ TOnBlurAsync ,
206+ TOnSubmit ,
207+ TOnSubmitAsync ,
208+ TOnDynamic ,
209+ TOnDynamicAsync ,
210+ TOnServer ,
211+ TSubmitMeta
212+ > {
217213 // Optional, but adds props to the `render` function outside of `form`
218214 props ?: TRenderProps
219215 render : (
220- props :
216+ props :
221217 NoInfer < TRenderProps > & {
222218 form : AppFieldExtendedReactFormApi <
223219 TFormData ,
@@ -295,52 +291,31 @@ export function createFormHook<
295291 > {
296292 const form = createForm ( props )
297293
298- const AppForm = useMemo ( ( ) => {
299- const AppForm = ( ( { children } ) => {
300- return (
301- < formContext . Provider value = { form } > { children} < / f o r m C o n t e x t .P r o v i d e r >
302- )
303- } ) as ComponentType < PropsWithChildren >
304- return AppForm
305- } , [ form ] )
294+ const AppForm = ( ( internal , props ) => {
295+ return AppFormSvelte ( internal , { ...props , form } )
296+ } ) as Component < { children : Snippet } >
306297
307- const AppField = useMemo ( ( ) => {
308- const AppField = ( ( { children, ...props } ) => {
309- return (
310- < form . Field { ...props } >
311- { ( field ) => (
312- // eslint-disable-next-line @eslint-react/no-context-provider
313- < fieldContext . Provider value = { field } >
314- { children ( Object . assign ( field , fieldComponents ) ) }
315- < / f i e l d C o n t e x t .P r o v i d e r >
316- ) }
317- < / form . Field >
318- )
319- } ) as FieldComponent <
320- TFormData ,
321- TOnMount ,
322- TOnChange ,
323- TOnChangeAsync ,
324- TOnBlur ,
325- TOnBlurAsync ,
326- TOnSubmit ,
327- TOnSubmitAsync ,
328- TOnDynamic ,
329- TOnDynamicAsync ,
330- TOnServer ,
331- TSubmitMeta ,
332- TComponents
333- >
334- return AppField
335- } , [ form ] )
298+ const AppField = ( ( internal , props ) => AppFieldSvelte ( internal , { ...props , form } as never ) ) as FieldComponent <
299+ TFormData ,
300+ TOnMount ,
301+ TOnChange ,
302+ TOnChangeAsync ,
303+ TOnBlur ,
304+ TOnBlurAsync ,
305+ TOnSubmit ,
306+ TOnSubmitAsync ,
307+ TOnDynamic ,
308+ TOnDynamicAsync ,
309+ TOnServer ,
310+ TSubmitMeta ,
311+ TComponents
312+ >
336313
337- const extendedForm = useMemo ( ( ) => {
338- return Object . assign ( form , {
339- AppField,
340- AppForm,
341- ...formComponents ,
342- } )
343- } , [ form , AppField , AppForm ] )
314+ const extendedForm = Object . assign ( form , {
315+ AppField,
316+ AppForm,
317+ ...formComponents ,
318+ } )
344319
345320 return extendedForm
346321 }
0 commit comments