Open History to see the last commit
1import { formOptions } from '@tanstack/form-core'
2import { useInternalForm } from '../PreactForm/PreactFormApi.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 { FunctionComponent } from 'preact/compat'
12import type { FormOptions } from '@tanstack/form-core'
13β
14export function createFormHook<
15 const TFormComponents extends Record<string, FunctionComponent<any>>,
16 const TFieldComponents extends Record<string, FunctionComponent<any>>,
17>(
18 createOptions: CreateFormHookOptions<TFormComponents, TFieldComponents>,
19): AppFormHookResult<{
20 formComponents: TFormComponents
21 fieldComponents: TFieldComponents
22}> {
23 const initializeAppForm = createAppFormInitializer(createOptions)
24β
25 function useExtendedForm(hookOptions: FormOptions<any, any, any, unknown>) {
26 const form = useInternalForm(hookOptions, initializeAppForm)
27 return form
28 }
29 const useAppForm = useExtendedForm as never as UseAppFormHook<{
30 formComponents: TFormComponents
31 fieldComponents: TFieldComponents
32 }>
33β
34 return {
35 useFormContext: useFormContext as never,
36 appFormOptions: formOptions as never,
37 defineAppFieldGroup: defineFieldGroup as never,
38 useAppForm,
39 }
40}