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

createForm.public.ts

53 lines1.5 KB

1import { createInternalForm, initializeForm } from './createForm.svelte'
2import type {
3 FormOptions,
4 FormValidators,
5 ToFormErrorTypes,
6} from '@tanstack/form-core'
7import type { SvelteFormApi } from './formApiTypes.public'
8import type { DefaultSvelteFormComponentMap } from './AppForm/componentMap.public'
9​
10export type CreateForm = <
11 TFormData,
12 const TFormValidators extends FormValidators<TFormData>,
13 TSubmitReturn,
14>(
15 options: () => FormOptions<
16 TFormData,
17 TFormValidators,
18 TSubmitReturn,
19 unknown
20 >,
21) => SvelteFormApi<
22 TFormData,
23 ToFormErrorTypes<TFormValidators, TSubmitReturn>,
24 DefaultSvelteFormComponentMap
26​
27function createFormImpl(options: () => FormOptions<any, any, any, unknown>) {
28 return createInternalForm(options, initializeForm)
30​
31/**
32 * Creates a Svelte form whose instance and lifecycle are owned by the current
33 * component.
34 *
35 * Pass an options function. `defaultValues` establish the initial state and
36 * inferred form value type, while reactive values read by the function update
37 * the same form instance when they change.
38 *
39 * Call this during component initialization. The form mounts with the
40 * component and is cleaned up when the component unmounts.
41 *
42 * @example
43 * ```ts
44 * const form = createForm(() => ({
45 * defaultValues: { name: '' },
46 * onSubmit: ({ value }) => saveProfile(value),
47 * }))
48 * ```
49 *
50 * @returns The Svelte form API with typed field, subscription, and form-group
51 * components attached.
52 */
53export const createForm = createFormImpl as never as CreateForm