Skip to content

Commit b7f6d0f

Browse files
committed
chore: more work on the Svelte composition adapter
1 parent 173be02 commit b7f6d0f

File tree

5 files changed

+94
-68
lines changed

5 files changed

+94
-68
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- Take "form" as an prop, pass it to context, and render children -->
2+
<script lang="ts">
3+
import { Snippet } from 'svelte'
4+
import InnerAppField from './InnerAppField.svelte'
5+
6+
interface Props {
7+
form: any
8+
children: Snippet
9+
}
10+
const { children, form }: Props = $props()
11+
</script>
12+
13+
<form.Field name="fullName">
14+
{#snippet children(field: any)}
15+
<InnerAppField field={field} children={children}/>
16+
{/snippet}
17+
</form.Field>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- Take "form" as an prop, pass it to context, and render children -->
2+
<script lang="ts">
3+
import { setContext, Snippet } from 'svelte'
4+
import { formContextKey } from './context-keys.js'
5+
6+
interface Props {
7+
form: any
8+
children: Snippet
9+
}
10+
11+
const { children, form }: Props = $props()
12+
13+
setContext(formContextKey, form)
14+
</script>
15+
16+
{@render children?.()}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!-- Take "form" as an prop, pass it to context, and render children -->
2+
<script lang="ts">
3+
import { setContext, Snippet } from 'svelte'
4+
import { fieldContextKey } from './context-keys.js'
5+
6+
interface Props {
7+
field: any
8+
children: Snippet
9+
}
10+
11+
const { children, field }: Props = $props()
12+
13+
setContext(fieldContextKey, field)
14+
</script>
15+
16+
{@render children?.()}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const fieldContextKey = "__tanstack_field_context_key"
2+
export const formContextKey = "__tanstack_form_context_key"

packages/svelte-form/src/createFormRune.svelte.ts

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ import { createForm } from './createForm.svelte'
22
import type {
33
AnyFieldApi,
44
AnyFormApi,
5-
BaseFormOptions,
6-
DeepKeysOfType,
75
FieldApi,
8-
FieldsMap,
96
FormAsyncValidateOrFn,
107
FormOptions,
118
FormValidateOrFn,
129
} from '@tanstack/form-core'
1310
import type { FieldComponent } from './types.js'
1411
import type { SvelteFormExtendedApi } from './createForm.svelte'
1512
import { 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"
5147
type UnwrapOrAny<T> = [unknown] extends [T] ? any : T
5248
type UnwrapDefaultOrAny<DefaultT, T> = [DefaultT] extends [T]
5349
? [T] extends [DefaultT]
54-
? any
55-
: T
50+
? any
51+
: T
5652
: T
5753

5854
export 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

187183
export 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}</formContext.Provider>
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-
</fieldContext.Provider>
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

Comments
 (0)