@@ -9,18 +9,15 @@ import {
9
9
isFunction ,
10
10
isNullOrUndefined ,
11
11
isNumber ,
12
- isObject ,
13
12
isString ,
14
13
isUndefined ,
15
14
} from '../utils'
16
15
import { InvalidDate } from '../utils/constant'
17
-
18
16
import { deepEqual } from '../utils/deepEqual'
19
17
import { isRadioOrCheckboxInput } from '../utils/fieldElement'
20
18
import { getFormEl } from '../utils/getFormEl'
21
19
import { getValidationMode } from '../utils/getValidationMode'
22
20
import { isFieldElement } from '../utils/isFieldElement'
23
-
24
21
import { warn } from '../utils/warn'
25
22
import { handleValidateError , validateField } from './validate'
26
23
import type { RegisterOptions } from '../types/validator'
@@ -78,11 +75,9 @@ export function creatFormControl<
78
75
_options . criteriaMode === VALIDATION_MODE . all
79
76
80
77
const _setFormState = ( props : { [ K in TFormStateKey ] ?: TFormState [ K ] } ) => {
81
- Object . entries ( props ) . forEach ( ( [ key , val ] ) => {
82
- if ( isObject ( _formState [ key ] ) ) {
83
- _formState [ key ] = val
84
- }
85
- } )
78
+ for ( const [ key , val ] of Object . entries ( props ) ) {
79
+ _formState [ key ] = val
80
+ }
86
81
}
87
82
88
83
const _setFormStateError = ( fieldName : FieldsKey , error : FieldError ) => {
@@ -92,6 +87,16 @@ export function creatFormControl<
92
87
const _getFormStateError = ( fieldName ?: FieldsKey ) =>
93
88
fieldName ? get ( _formState . errors , fieldName ) : _formState . errors
94
89
90
+ const isEmptyErrors = ( ) => {
91
+ for ( const key of _originalFieldsKey . keys ( ) ) {
92
+ if ( _getFormStateError ( key as FieldsKey ) ) {
93
+ return false
94
+ }
95
+ }
96
+
97
+ return true
98
+ }
99
+
95
100
const _removeFormStateError = ( fieldName : FieldsKey ) => {
96
101
if ( isEmptyObject ( _formState . errors ) ) {
97
102
return
@@ -151,6 +156,16 @@ export function creatFormControl<
151
156
}
152
157
}
153
158
159
+ const isDirty = ( ) => {
160
+ for ( const key of _originalFieldsKey . keys ( ) ) {
161
+ if ( _isDirtyField ( key as FieldsKey ) ) {
162
+ return true
163
+ }
164
+ }
165
+
166
+ return false
167
+ }
168
+
154
169
const _getDirtyFields = ( handleIsDirty = true ) => {
155
170
const dirtyFields = { } as TFormState [ 'dirtyFields' ]
156
171
@@ -165,7 +180,7 @@ export function creatFormControl<
165
180
166
181
if ( handleIsDirty ) {
167
182
_setFormState ( {
168
- isDirty : ! isEmptyObject ( dirtyFields ) ,
183
+ isDirty : isDirty ( ) ,
169
184
} )
170
185
}
171
186
@@ -410,7 +425,7 @@ export function creatFormControl<
410
425
await _onChange ( )
411
426
_handleAllDirtyFieldsOperate ( )
412
427
413
- if ( ! isEmptyObject ( _formState . errors ) && isFunction ( onError ) ) {
428
+ if ( ! isEmptyErrors ( ) && isFunction ( onError ) ) {
414
429
await onError ( _formState . errors , e )
415
430
_setFormState ( {
416
431
isSubmitting : false ,
0 commit comments