@@ -67,8 +67,9 @@ import {
67
67
import { FormContextKey } from './symbols' ;
68
68
import { validateTypedSchema , validateObjectSchema } from './validate' ;
69
69
import { refreshInspector , registerFormWithDevTools } from './devtools' ;
70
- import { isCallable } from '../../shared' ;
70
+ import { isCallable , merge } from '../../shared' ;
71
71
import { getConfig } from './config' ;
72
+ import { PartialDeep } from 'type-fest' ;
72
73
73
74
type FormSchema < TValues extends Record < string , unknown > > =
74
75
| FlattenAndSetPathsType < TValues , GenericValidateFunction | string | GenericObject >
@@ -82,7 +83,7 @@ export interface FormOptions<
82
83
| TypedSchema < TValues , TOutput >
83
84
> {
84
85
validationSchema ?: MaybeRef < TSchema extends TypedSchema ? TypedSchema < TValues , TOutput > : any > ;
85
- initialValues ?: MaybeRef < Partial < TValues > > ;
86
+ initialValues ?: MaybeRef < PartialDeep < TValues > > ;
86
87
initialErrors ?: FlattenAndSetPathsType < TValues , string | undefined > ;
87
88
initialTouched ?: FlattenAndSetPathsType < TValues , boolean > ;
88
89
validateOnMount ?: boolean ;
@@ -103,10 +104,6 @@ function resolveInitialValues<TValues extends GenericObject = GenericObject>(opt
103
104
return deepCopy ( providedValues ) as TValues ;
104
105
}
105
106
106
- // TODO: BIG FAT WORK TO DO:
107
- // Resets must happen here, the useField should not handle its resets unless called from there.
108
- // Field validators must not set their state, the forms must do that instead and process their own errors, unless no form exists
109
-
110
107
export function useForm <
111
108
TValues extends GenericObject = GenericObject ,
112
109
TOutput = TValues ,
@@ -158,7 +155,10 @@ export function useForm<
158
155
const state = findPathState ( path ) ;
159
156
if ( state ) {
160
157
state . errors = normalizeErrorItem ( paths [ path ] ) ;
158
+ return ;
161
159
}
160
+
161
+ extraErrorsBag . value [ path as string ] = normalizeErrorItem ( paths [ path ] ) ;
162
162
} ) ;
163
163
}
164
164
@@ -544,16 +544,8 @@ export function useForm<
544
544
/**
545
545
* Sets multiple fields values
546
546
*/
547
- function setValues ( fields : Partial < TValues > ) {
548
- // clean up old values
549
- keysOf ( formValues ) . forEach ( key => {
550
- delete formValues [ key ] ;
551
- } ) ;
552
-
553
- // set up new values
554
- keysOf ( fields ) . forEach ( path => {
555
- setFieldValue ( path as Path < TValues > , fields [ path ] ) ;
556
- } ) ;
547
+ function setValues ( fields : PartialDeep < TValues > ) {
548
+ merge ( formValues , fields ) ;
557
549
558
550
// regenerate the arrays when the form values change
559
551
fieldArrays . forEach ( f => f && f . reset ( ) ) ;
@@ -939,7 +931,7 @@ export function useForm<
939
931
function useFormMeta < TValues extends Record < string , unknown > > (
940
932
pathsState : Ref < PathState < unknown > [ ] > ,
941
933
currentValues : TValues ,
942
- initialValues : MaybeRef < Partial < TValues > > ,
934
+ initialValues : MaybeRef < PartialDeep < TValues > > ,
943
935
errors : Ref < FormErrors < TValues > >
944
936
) {
945
937
const MERGE_STRATEGIES : Record < keyof Pick < FieldMeta < unknown > , 'touched' | 'pending' | 'valid' > , 'every' | 'some' > = {
@@ -990,18 +982,18 @@ function useFormInitialValues<TValues extends GenericObject>(
990
982
formValues : TValues ,
991
983
opts ?: FormOptions < TValues >
992
984
) {
993
- const values = resolveInitialValues ( opts ) ;
985
+ const values = resolveInitialValues ( opts ) as PartialDeep < TValues > ;
994
986
const providedValues = opts ?. initialValues ;
995
987
// these are the mutable initial values as the fields are mounted/unmounted
996
- const initialValues = ref < Partial < TValues > > ( values ) ;
988
+ const initialValues = ref < PartialDeep < TValues > > ( values ) ;
997
989
// these are the original initial value as provided by the user initially, they don't keep track of conditional fields
998
990
// this is important because some conditional fields will overwrite the initial values for other fields who had the same name
999
991
// like array fields, any push/insert operation will overwrite the initial values because they "create new fields"
1000
992
// so these are the values that the reset function should use
1001
993
// these only change when the user explicitly changes the initial values or when the user resets them with new values.
1002
- const originalInitialValues = ref < Partial < TValues > > ( deepCopy ( values ) ) as Ref < Partial < TValues > > ;
994
+ const originalInitialValues = ref < PartialDeep < TValues > > ( deepCopy ( values ) ) as Ref < PartialDeep < TValues > > ;
1003
995
1004
- function setInitialValues ( values : Partial < TValues > , updateFields = false ) {
996
+ function setInitialValues ( values : PartialDeep < TValues > , updateFields = false ) {
1005
997
initialValues . value = deepCopy ( values ) ;
1006
998
originalInitialValues . value = deepCopy ( values ) ;
1007
999
0 commit comments