Open History to see the last commit
1import { createContext, useContext } from 'solid-js'
2import type { Accessor } from 'solid-js'
3import type { AnyInternalFieldApi } from '@tanstack/form-core/internals'
4import type { InternalSolidFormApi } from '../SolidFormApi.lib'
5β
6export const FormContext = createContext<InternalSolidFormApi>()
7export const FieldContext = createContext<Accessor<AnyInternalFieldApi>>()
8β
9export function useFieldContext(): Accessor<AnyInternalFieldApi> {
10 const field = useContext(FieldContext)
11 if (field === undefined) {
12 throw new Error(
13 'TanStack Form: Field components must be used within a `form.Field` component.',
14 )
15 }
16 return field
17}
18β
19export function useFormContext(): InternalSolidFormApi {
20 const form = useContext(FormContext)
21 if (form === undefined) {
22 throw new Error(
23 'TanStack Form: Form components must be used within a `form.AppForm` component.',
24 )
25 }
26 return form
27}