1
- import { useState , useEffect , useMemo , useRef } from 'react' ;
2
- import { getInitial , getValue , getErrors , addUserDefinedKeywords } from './utils' ;
1
+ import { useEffect , useMemo , useRef , useState } from 'react' ;
2
+ import { addUserDefinedKeywords , getErrors , getInitial , getValue } from './utils' ;
3
3
import { ajv } from './utils/validation' ;
4
4
5
+ import { ErrorObject , JSONSchemaType , KeywordDefinition } from 'ajv' ;
6
+ import { useDebounce } from './Hooks/useDebounce' ;
5
7
import {
6
8
AJVMessageFunction ,
7
9
FormField ,
8
10
IState ,
9
- useFormErrors ,
10
11
UseFormReturn ,
12
+ useFormErrors ,
11
13
} from './utils/types' ;
12
- import { ErrorObject , JSONSchemaType , KeywordDefinition } from 'ajv' ;
13
- import { useDebounce } from './Hooks/useDebounce' ;
14
14
15
15
const useAJVForm = < T extends Record < string , any > > (
16
16
initial : T ,
@@ -41,17 +41,7 @@ const useAJVForm = <T extends Record<string, any>>(
41
41
const AJVValidate = ajv . compile ( schema ) ;
42
42
43
43
const resetForm = ( ) => {
44
- setState (
45
- Object . keys ( state ) . reduce ( ( acc , name ) => {
46
- return {
47
- ...acc ,
48
- [ name ] : {
49
- value : '' ,
50
- error : '' ,
51
- } ,
52
- } ;
53
- } , { } as IState < T > ) ,
54
- ) ;
44
+ setState ( initialStateRef . current ) ;
55
45
} ;
56
46
57
47
const validateField = ( fieldName : keyof T ) => {
@@ -93,6 +83,7 @@ const useAJVForm = <T extends Record<string, any>>(
93
83
name : Object . keys ( form ) [ 0 ] as keyof T ,
94
84
editId : editCounter ,
95
85
} ) ;
86
+
96
87
setEditCounter ( editCounter + 1 ) ;
97
88
98
89
return newState ;
@@ -144,6 +135,11 @@ const useAJVForm = <T extends Record<string, any>>(
144
135
145
136
return { isValid : false , data : null } ;
146
137
}
138
+
139
+ if ( ! isFormValid ( state , initialStateRef . current ) ) {
140
+ return { isValid : false , data : null } ;
141
+ }
142
+
147
143
return { isValid : true , data } ;
148
144
} catch ( error ) {
149
145
return { isValid : false , data : null } ;
@@ -159,10 +155,23 @@ const useAJVForm = <T extends Record<string, any>>(
159
155
) ;
160
156
} ;
161
157
162
- const isFormValid = ( currentState : IState < T > ) : boolean => {
163
- return ! Object . keys ( currentState ) . some ( ( key ) => currentState [ key ] . error !== '' ) ;
158
+ const isFormValid = (
159
+ currentState : IState < T > ,
160
+ initialState : IState < T > ,
161
+ ) : boolean => {
162
+ const hasErrors = Object . keys ( currentState ) . some (
163
+ ( key ) => currentState [ key ] . error !== '' ,
164
+ ) ;
165
+
166
+ const formIsDirty = isFormDirty ( currentState , initialState ) ;
167
+
168
+ return ! hasErrors && formIsDirty ;
164
169
} ;
165
170
171
+ const isValid = useMemo ( ( ) => {
172
+ return isFormValid ( state , initialStateRef . current ) ;
173
+ } , [ state ] ) ;
174
+
166
175
const isDirty = useMemo (
167
176
( ) => isFormDirty ( state , initialStateRef . current ) ,
168
177
[ state ] ,
@@ -172,8 +181,6 @@ const useAJVForm = <T extends Record<string, any>>(
172
181
setState ( _setErrors ( getErrors ( errors , options ?. userDefinedMessages ) ) ) ;
173
182
} ;
174
183
175
- const isValid = useMemo ( ( ) => isFormValid ( state ) , [ state ] ) ;
176
-
177
184
useEffect ( ( ) => {
178
185
if ( options ?. shouldDebounceAndValidate === false || ! debouncedField ) {
179
186
return ;
0 commit comments