Open History to see the last commit
1import { initializeForm, useInternalForm } from './VueFormApi.lib'
2import type {
3 FormOptions,
4 FormValidators,
5 ToFormErrorTypes,
6} from '@tanstack/form-core'
7import type {
8 AnyVueFormComponentMap,
9 DefaultVueFormComponentMap,
10} from '../AppForm/componentMap.public'
11import type { VueFormApi } from './formApiTypes.public'
12β
13export type UseFormHook<in out TComponents extends AnyVueFormComponentMap> = <
14 TFormData,
15 const TFormValidators extends FormValidators<TFormData>,
16 TSubmitReturn,
17>(
18 options: FormOptions<TFormData, TFormValidators, TSubmitReturn, unknown>,
19) => VueFormApi<
20 TFormData,
21 ToFormErrorTypes<TFormValidators, TSubmitReturn>,
22 TComponents
23>
24β
25function useFormHook(options: FormOptions<any, any, any, unknown>) {
26 return useInternalForm(options, initializeForm)
27}
28β
29/**
30 * Creates a Vue form whose instance and lifecycle are owned by the current
31 * component.
32 *
33 * `defaultValues` establish the initial state and inferred form value type. To
34 * update the existing form after creation, pass a reactive options object;
35 * tracked changes are applied without replacing the form instance.
36 *
37 * Call this during the component's setup phase. The form mounts with the
38 * component and is cleaned up when the component unmounts.
39 *
40 * @example
41 * ```ts
42 * const form = useForm({
43 * defaultValues: { name: '' },
44 * onSubmit: ({ value }) => saveProfile(value),
45 * })
46 * ```
47 *
48 * @returns The Vue form API with typed field, subscription, and form-group
49 * components attached.
50 */
51export const useForm =
52 useFormHook as never as UseFormHook<DefaultVueFormComponentMap>