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

formContext.ts

23 lines537 Bytes

1import type { FormApi } from '@tanstack/form-core'
2import { inject, provide } from 'vue-demi'
3​
4export type FormContext = {
5 formApi: FormApi<any, unknown>
6 parentFieldName?: string
7} | null
8​
9export const formContext = Symbol('FormContext')
10​
11export function provideFormContext(val: FormContext) {
12 provide(formContext, val)
14​
15export function useFormContext() {
16 const formApi = inject(formContext) as FormContext
17​
18 if (!formApi) {
19 throw new Error(`You are trying to use the form API outside of a form!`)
20 }
21​
22 return formApi