11import { computed , reactive , ref , Ref , unref , watch } from 'vue' ;
2- import { FormContextKey } from './symbols' ;
3- import { FieldMeta , FieldState , MaybeRef } from './types' ;
4- import { getFromPath , injectWithSelf , isEqual } from './utils' ;
2+ import { FieldMeta , FieldState , MaybeRef , PrivateFormContext } from './types' ;
3+ import { getFromPath , isEqual } from './utils' ;
54
65export interface StateSetterInit < TValue = unknown > extends FieldState < TValue > {
76 initialValue : TValue ;
@@ -20,7 +19,7 @@ export interface FieldStateComposable<TValue = unknown> {
2019
2120export interface StateInit < TValue = unknown > {
2221 modelValue : MaybeRef < TValue > ;
23- standalone : boolean ;
22+ form ?: PrivateFormContext ;
2423}
2524
2625let ID_COUNTER = 0 ;
@@ -29,8 +28,8 @@ export function useFieldState<TValue = unknown>(
2928 path : MaybeRef < string > ,
3029 init : Partial < StateInit < TValue > >
3130) : FieldStateComposable < TValue > {
32- const { value, initialValue, setInitialValue } = _useFieldValue < TValue > ( path , init . modelValue , ! init . standalone ) ;
33- const { errorMessage, errors, setErrors } = _useFieldErrors ( path , ! init . standalone ) ;
31+ const { value, initialValue, setInitialValue } = _useFieldValue < TValue > ( path , init . modelValue , init . form ) ;
32+ const { errorMessage, errors, setErrors } = _useFieldErrors ( path , init . form ) ;
3433 const meta = _useFieldMeta ( value , initialValue , errors ) ;
3534 const id = ID_COUNTER >= Number . MAX_SAFE_INTEGER ? 0 : ++ ID_COUNTER ;
3635
@@ -76,9 +75,8 @@ interface FieldValueComposable<TValue = unknown> {
7675export function _useFieldValue < TValue = unknown > (
7776 path : MaybeRef < string > ,
7877 modelValue ?: MaybeRef < TValue > ,
79- shouldInjectForm = true
78+ form ?: PrivateFormContext
8079) : FieldValueComposable < TValue > {
81- const form = shouldInjectForm === true ? injectWithSelf ( FormContextKey , undefined ) : undefined ;
8280 const modelRef = ref ( unref ( modelValue ) ) as Ref < TValue > ;
8381
8482 function resolveInitialValue ( ) {
@@ -170,9 +168,7 @@ function _useFieldMeta<TValue>(
170168/**
171169 * Creates the error message state for the field state
172170 */
173- export function _useFieldErrors ( path : MaybeRef < string > , shouldInjectForm : boolean ) {
174- const form = shouldInjectForm ? injectWithSelf ( FormContextKey , undefined ) : undefined ;
175-
171+ export function _useFieldErrors ( path : MaybeRef < string > , form ?: PrivateFormContext ) {
176172 function normalizeErrors ( messages : string | string [ ] | null | undefined ) {
177173 if ( ! messages ) {
178174 return [ ] ;
0 commit comments