Open History to see the last commit
1import type { FormApi, Validator } from '@tanstack/form-core'
2import { inject, provide } from 'vue'
3β
4export type FormContext<
5 TFormData = any,
6 TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
7> = {
8 formApi: FormApi<TFormData, TFormValidator>
9 parentFieldName?: string
10} | null
11β
12export const formContext = Symbol('FormContext')
13β
14export function provideFormContext<
15 TFormData = any,
16 TFormValidator extends Validator<TFormData, unknown> | undefined = undefined,
17>(val: FormContext<TFormData, TFormValidator>) {
18 provide(formContext, val)
19}
20β
21export function useFormContext() {
22 const formApi = inject(formContext) as FormContext
23β
24 if (!formApi) {
25 throw new Error(`You are trying to use the form API outside of a form!`)
26 }
27β
28 return formApi
29}