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

contexts.lib.ts

31 lines899 Bytes

1import { inject } from 'vue'
2import type { InjectionKey } from 'vue'
3import type { AnyFieldApi } from '@tanstack/form-core'
4import type { InternalVueFormApi } from '../VueForm/VueFormApi.lib'
5​
6export const FormContext = Symbol(
7 'TanStackForm.FormContext',
8) as InjectionKey<InternalVueFormApi>
9export const FieldContext = Symbol(
10 'TanStackForm.FieldContext',
11) as InjectionKey<AnyFieldApi>
12​
13export function useFieldContext(): AnyFieldApi {
14 const field = inject(FieldContext)
15 if (field === undefined) {
16 throw new Error(
17 'TanStack Form: Field components must be used within a `form.Field` component.',
18 )
19 }
20 return field
22​
23export function useFormContext(): InternalVueFormApi {
24 const form = inject(FormContext)
25 if (form === undefined) {
26 throw new Error(
27 'TanStack Form: Form components must be used within a `form.AppForm` component.',
28 )
29 }
30 return form