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

createForm.public.ts

62 lines1.7 KB

1import { createInternalForm, initializeForm } from './SolidFormApi.lib'
2import type {
3 FormOptions,
4 FormValidators,
5 ToFormErrorTypes,
6} from '@tanstack/form-core'
7import type { Accessor } from 'solid-js'
8import type { SolidFormApi } from './formApiTypes.public'
9import type { DefaultSolidFormComponentMap } from './AppForm/componentMap.public'
10​
11export type CreateFormHook = <
12 TFormData,
13 const TFormValidators extends FormValidators<TFormData>,
14 TSubmitReturn,
15>(
16 options: Accessor<
17 FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>
18 >,
19) => SolidFormApi<
20 TFormData,
21 ToFormErrorTypes<TFormValidators, TSubmitReturn>,
22 DefaultSolidFormComponentMap
24​
25function createFormHook(
26 options: Accessor<FormOptions<any, any, any, unknown>>,
27) {
28 return createInternalForm(options, initializeForm)
30​
31/**
32 * Creates a Solid form whose instance and lifecycle are owned by the current
33 * reactive owner.
34 *
35 * Pass an options accessor. `defaultValues` establish the initial state and
36 * inferred form value type, while signals read by the accessor update the same
37 * form instance when they change. The form is cleaned up when its owner is
38 * disposed.
39 *
40 * @example
41 * ```tsx
42 * function ProfileForm() {
43 * const form = createForm(() => ({
44 * defaultValues: { name: '' },
45 * onSubmit: ({ value }) => saveProfile(value),
46 * }))
47 *
48 * return (
49 * <form
50 * onSubmit={(event) => {
51 * event.preventDefault()
52 * void form.handleSubmit()
53 * }}
54 * />
55 * )
56 * }
57 * ```
58 *
59 * @returns The Solid form API with typed field, subscription, and form-group
60 * components attached.
61 */
62export const createForm = createFormHook as never as CreateFormHook