@@ -82,7 +82,7 @@ export interface FormOptions<
82
82
TOutput = TValues ,
83
83
TSchema extends TypedSchema < TValues , TOutput > | FormSchema < TValues > =
84
84
| FormSchema < TValues >
85
- | TypedSchema < TValues , TOutput >
85
+ | TypedSchema < TValues , TOutput > ,
86
86
> {
87
87
validationSchema ?: MaybeRef < TSchema extends TypedSchema ? TypedSchema < TValues , TOutput > : any > ;
88
88
initialValues ?: MaybeRef < PartialDeep < TValues > | undefined | null > ;
@@ -111,7 +111,7 @@ export function useForm<
111
111
TOutput = TValues ,
112
112
TSchema extends FormSchema < TValues > | TypedSchema < TValues , TOutput > =
113
113
| FormSchema < TValues >
114
- | TypedSchema < TValues , TOutput >
114
+ | TypedSchema < TValues , TOutput > ,
115
115
> ( opts ?: FormOptions < TValues , TOutput , TSchema > ) : FormContext < TValues , TOutput > {
116
116
const formId = FORM_COUNTER ++ ;
117
117
@@ -201,19 +201,25 @@ export function useForm<
201
201
* Holds a computed reference to all fields names and labels
202
202
*/
203
203
const fieldNames = computed ( ( ) => {
204
- return pathStates . value . reduce ( ( names , state ) => {
205
- names [ state . path ] = { name : state . path || '' , label : state . label || '' } ;
204
+ return pathStates . value . reduce (
205
+ ( names , state ) => {
206
+ names [ state . path ] = { name : state . path || '' , label : state . label || '' } ;
206
207
207
- return names ;
208
- } , { } as Record < string , { name : string ; label : string } > ) ;
208
+ return names ;
209
+ } ,
210
+ { } as Record < string , { name : string ; label : string } > ,
211
+ ) ;
209
212
} ) ;
210
213
211
214
const fieldBailsMap = computed ( ( ) => {
212
- return pathStates . value . reduce ( ( map , state ) => {
213
- map [ state . path ] = state . bails ?? true ;
215
+ return pathStates . value . reduce (
216
+ ( map , state ) => {
217
+ map [ state . path ] = state . bails ?? true ;
214
218
215
- return map ;
216
- } , { } as Record < string , boolean > ) ;
219
+ return map ;
220
+ } ,
221
+ { } as Record < string , boolean > ,
222
+ ) ;
217
223
} ) ;
218
224
219
225
// mutable non-reactive reference to initial errors
@@ -228,7 +234,7 @@ export function useForm<
228
234
const { initialValues, originalInitialValues, setInitialValues } = useFormInitialValues < TValues > (
229
235
pathStates ,
230
236
formValues ,
231
- opts
237
+ opts ,
232
238
) ;
233
239
234
240
// form meta aggregations
@@ -247,7 +253,7 @@ export function useForm<
247
253
248
254
function createPathState < TValue > (
249
255
path : MaybeRefOrGetter < Path < TValues > > ,
250
- config ?: Partial < PathStateConfig >
256
+ config ?: Partial < PathStateConfig > ,
251
257
) : PathState < TValue > {
252
258
const initialValue = computed ( ( ) => getFromPath ( initialValues . value , toValue ( path ) ) ) ;
253
259
const pathStateExists = pathStates . value . find ( s => isPathsEqual ( s . path , toValue ( path ) ) ) ;
@@ -380,9 +386,9 @@ export function useForm<
380
386
381
387
return validation ;
382
388
} ,
383
- { valid : formResult . valid , results : { } , errors : { } } as FormValidationResult < TValues >
389
+ { valid : formResult . valid , results : { } , errors : { } } as FormValidationResult < TValues > ,
384
390
) ;
385
- }
391
+ } ,
386
392
) ;
387
393
388
394
function mutateAllPathState ( mutation : ( state : PathState ) => void ) {
@@ -398,15 +404,18 @@ export function useForm<
398
404
function findHoistedPath ( path : Path < TValues > ) {
399
405
const candidates = pathStates . value . filter ( state => path . startsWith ( state . path ) ) ;
400
406
401
- return candidates . reduce ( ( bestCandidate , candidate ) => {
402
- if ( ! bestCandidate ) {
403
- return candidate as PathState < PathValue < TValues , Path < TValues > > > ;
404
- }
407
+ return candidates . reduce (
408
+ ( bestCandidate , candidate ) => {
409
+ if ( ! bestCandidate ) {
410
+ return candidate as PathState < PathValue < TValues , Path < TValues > > > ;
411
+ }
405
412
406
- return ( candidate . path . length > bestCandidate . path . length ? candidate : bestCandidate ) as PathState <
407
- PathValue < TValues , Path < TValues > >
408
- > ;
409
- } , undefined as PathState < PathValue < TValues , Path < TValues > > > | undefined ) ;
413
+ return ( candidate . path . length > bestCandidate . path . length ? candidate : bestCandidate ) as PathState <
414
+ PathValue < TValues , Path < TValues > >
415
+ > ;
416
+ } ,
417
+ undefined as PathState < PathValue < TValues , Path < TValues > > > | undefined ,
418
+ ) ;
410
419
}
411
420
412
421
let UNSET_BATCH : Path < TValues > [ ] = [ ] ;
@@ -431,7 +440,7 @@ export function useForm<
431
440
function makeSubmissionFactory ( onlyControlled : boolean ) {
432
441
return function submitHandlerFactory < TReturn = unknown > (
433
442
fn ?: SubmissionHandler < TValues , TOutput , TReturn > ,
434
- onValidationError ?: InvalidSubmissionHandler < TValues >
443
+ onValidationError ?: InvalidSubmissionHandler < TValues > ,
435
444
) {
436
445
return function submissionHandler ( e : unknown ) {
437
446
if ( e instanceof Event ) {
@@ -489,7 +498,7 @@ export function useForm<
489
498
490
499
// re-throw the err so it doesn't go silent
491
500
throw err ;
492
- }
501
+ } ,
493
502
) ;
494
503
} ;
495
504
} ;
@@ -507,7 +516,7 @@ export function useForm<
507
516
}
508
517
509
518
nextTick ( ( ) => {
510
- validateField ( path , { mode : 'silent' } ) ;
519
+ validateField ( path , { mode : 'silent' , warn : false } ) ;
511
520
} ) ;
512
521
513
522
if ( pathState . multiple && pathState . fieldsCount ) {
@@ -583,7 +592,7 @@ export function useForm<
583
592
function setFieldValue < T extends Path < TValues > > (
584
593
field : T | PathState ,
585
594
value : PathValue < TValues , T > | undefined ,
586
- shouldValidate = true
595
+ shouldValidate = true ,
587
596
) {
588
597
const clonedValue = deepCopy ( value ) ;
589
598
const path = typeof field === 'string' ? field : ( field . path as Path < TValues > ) ;
@@ -632,7 +641,7 @@ export function useForm<
632
641
633
642
function useFieldModel < TPath extends Path < TValues > > ( path : TPath ) : Ref < PathValue < TValues , TPath > > ;
634
643
function useFieldModel < TPaths extends readonly [ ...MaybeRef < Path < TValues > > [ ] ] > (
635
- paths : TPaths
644
+ paths : TPaths ,
636
645
) : MapValuesPathsToRefs < TValues , TPaths > ;
637
646
function useFieldModel < TPaths extends Path < TValues > | readonly [ ...MaybeRef < Path < TValues > > [ ] ] > ( pathOrPaths : TPaths ) {
638
647
if ( ! Array . isArray ( pathOrPaths ) ) {
@@ -723,7 +732,7 @@ export function useForm<
723
732
errors : result . errors ,
724
733
} ;
725
734
} ) ;
726
- } )
735
+ } ) ,
727
736
) ;
728
737
729
738
isValidating . value = false ;
@@ -764,7 +773,8 @@ export function useForm<
764
773
return state . validate ( opts ) ;
765
774
}
766
775
767
- if ( ! state ) {
776
+ const shouldWarn = ! state && ( opts ?. warn ?? true ) ;
777
+ if ( shouldWarn ) {
768
778
warn ( `field with path ${ path } was not found` ) ;
769
779
}
770
780
@@ -863,17 +873,17 @@ export function useForm<
863
873
refreshInspector ,
864
874
{
865
875
deep : true ,
866
- }
876
+ } ,
867
877
) ;
868
878
}
869
879
870
880
function defineComponentBinds <
871
881
TPath extends Path < TValues > ,
872
882
TValue = PathValue < TValues , TPath > ,
873
- TExtras extends GenericObject = GenericObject
883
+ TExtras extends GenericObject = GenericObject ,
874
884
> (
875
885
path : MaybeRefOrGetter < TPath > ,
876
- config ?: Partial < ComponentBindsConfig < TValue , TExtras > > | LazyComponentBindsConfig < TValue , TExtras >
886
+ config ?: Partial < ComponentBindsConfig < TValue , TExtras > > | LazyComponentBindsConfig < TValue , TExtras > ,
877
887
) {
878
888
const pathState = findPathState ( toValue ( path ) ) || createPathState ( path ) ;
879
889
const evalConfig = ( ) => ( isCallable ( config ) ? config ( omit ( pathState , PRIVATE_PATH_STATE_KEYS ) ) : config || { } ) ;
@@ -927,10 +937,10 @@ export function useForm<
927
937
function defineInputBinds <
928
938
TPath extends Path < TValues > ,
929
939
TValue = PathValue < TValues , TPath > ,
930
- TExtras extends GenericObject = GenericObject
940
+ TExtras extends GenericObject = GenericObject ,
931
941
> (
932
942
path : MaybeRefOrGetter < TPath > ,
933
- config ?: Partial < InputBindsConfig < TValue , TExtras > > | LazyInputBindsConfig < TValue , TExtras >
943
+ config ?: Partial < InputBindsConfig < TValue , TExtras > > | LazyInputBindsConfig < TValue , TExtras > ,
934
944
) {
935
945
const pathState = ( findPathState ( toValue ( path ) ) || createPathState ( path ) ) as PathState < TValue > ;
936
946
const evalConfig = ( ) => ( isCallable ( config ) ? config ( omit ( pathState , PRIVATE_PATH_STATE_KEYS ) ) : config || { } ) ;
@@ -1000,7 +1010,7 @@ function useFormMeta<TValues extends Record<string, unknown>>(
1000
1010
pathsState : Ref < PathState < unknown > [ ] > ,
1001
1011
currentValues : TValues ,
1002
1012
initialValues : MaybeRef < PartialDeep < TValues > > ,
1003
- errors : Ref < FormErrors < TValues > >
1013
+ errors : Ref < FormErrors < TValues > > ,
1004
1014
) {
1005
1015
const MERGE_STRATEGIES : Record < keyof Pick < FieldMeta < unknown > , 'touched' | 'pending' | 'valid' > , 'every' | 'some' > = {
1006
1016
touched : 'some' ,
@@ -1015,12 +1025,15 @@ function useFormMeta<TValues extends Record<string, unknown>>(
1015
1025
function calculateFlags ( ) {
1016
1026
const states = pathsState . value ;
1017
1027
1018
- return keysOf ( MERGE_STRATEGIES ) . reduce ( ( acc , flag ) => {
1019
- const mergeMethod = MERGE_STRATEGIES [ flag ] ;
1020
- acc [ flag ] = states [ mergeMethod ] ( s => s [ flag ] ) ;
1028
+ return keysOf ( MERGE_STRATEGIES ) . reduce (
1029
+ ( acc , flag ) => {
1030
+ const mergeMethod = MERGE_STRATEGIES [ flag ] ;
1031
+ acc [ flag ] = states [ mergeMethod ] ( s => s [ flag ] ) ;
1021
1032
1022
- return acc ;
1023
- } , { } as Record < keyof Omit < FieldMeta < unknown > , 'initialValue' > , boolean > ) ;
1033
+ return acc ;
1034
+ } ,
1035
+ { } as Record < keyof Omit < FieldMeta < unknown > , 'initialValue' > , boolean > ,
1036
+ ) ;
1024
1037
}
1025
1038
1026
1039
const flags = reactive ( calculateFlags ( ) ) ;
@@ -1048,7 +1061,7 @@ function useFormMeta<TValues extends Record<string, unknown>>(
1048
1061
function useFormInitialValues < TValues extends GenericObject > (
1049
1062
pathsState : Ref < PathState < unknown > [ ] > ,
1050
1063
formValues : TValues ,
1051
- opts ?: FormOptions < TValues >
1064
+ opts ?: FormOptions < TValues > ,
1052
1065
) {
1053
1066
const values = resolveInitialValues ( opts ) as PartialDeep < TValues > ;
1054
1067
const providedValues = opts ?. initialValues ;
@@ -1094,7 +1107,7 @@ function useFormInitialValues<TValues extends GenericObject>(
1094
1107
} ,
1095
1108
{
1096
1109
deep : true ,
1097
- }
1110
+ } ,
1098
1111
) ;
1099
1112
}
1100
1113
0 commit comments