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

createFormHook.public.ts

38 lines1.3 KB

1import { formOptions } from '@tanstack/form-core'
2import { useInternalForm } from '../VueForm/VueFormApi.lib'
3import { defineFieldGroup } from '../FieldGroup/withFields.public'
4import { createAppFormInitializer } from './initializeAppForm.lib'
5import { useFormContext } from './contexts.lib'
6import type {
7 AppFormHookResult,
8 CreateFormHookOptions,
9 UseAppFormHook,
10} from './createFormHookTypes.public'
11import type { Component } from 'vue'
12import type { FormOptions } from '@tanstack/form-core'
13​
14export function createFormHook<
15 const TFormComponents extends Record<string, Component>,
16 const TFieldComponents extends Record<string, Component>,
17>(
18 createOptions: CreateFormHookOptions<TFormComponents, TFieldComponents>,
19): AppFormHookResult<{
20 formComponents: TFormComponents
21 fieldComponents: TFieldComponents
22}> {
23 const initializeAppForm = createAppFormInitializer(createOptions)
24​
25 function useExtendedForm(options: FormOptions<any, any, any, unknown>) {
26 return useInternalForm(options, initializeAppForm)
27 }
28​
29 return {
30 useFormContext: useFormContext as never,
31 appFormOptions: formOptions as never,
32 defineAppFieldGroup: defineFieldGroup as never,
33 useAppForm: useExtendedForm as never as UseAppFormHook<{
34 formComponents: TFormComponents
35 fieldComponents: TFieldComponents
36 }>,
37 }