|
1 | | -import React,{ useState, useEffect } from 'react'; |
| 1 | +import { useState, useEffect } from 'react'; |
2 | 2 |
|
3 | 3 | export const useValidation = (value: string, validations: any[]) => { |
4 | 4 | const [isEmpty, setIsEmpty] = useState(true); |
5 | | - const [isEmailError, setIsEmailError] = useState(true); |
| 5 | + const [isEmailError, setIsEmailError] = useState(false); |
6 | 6 | const [isMinLengthError, setIsMinLengthError] = useState(false); |
7 | 7 | const [isMaxLengthError, setIsMaxLengthError] = useState(false); |
| 8 | + const [isInputValid, setIsInputValid] = useState(false); |
8 | 9 |
|
9 | 10 | useEffect(() => { |
10 | 11 | for (const validation in validations) { |
@@ -33,10 +34,19 @@ export const useValidation = (value: string, validations: any[]) => { |
33 | 34 | } |
34 | 35 | }, [value]); |
35 | 36 |
|
| 37 | + useEffect(() => { |
| 38 | + if (isEmpty || isMinLengthError || isMaxLengthError || isEmailError) { |
| 39 | + setIsInputValid(false); |
| 40 | + } else { |
| 41 | + setIsInputValid(true); |
| 42 | + } |
| 43 | + }, [isEmpty, isMinLengthError, isMaxLengthError, isEmailError]); |
| 44 | + |
36 | 45 | return { |
37 | 46 | isEmpty, |
38 | 47 | isMinLengthError, |
39 | 48 | isMaxLengthError, |
40 | 49 | isEmailError, |
| 50 | + isInputValid, |
41 | 51 | }; |
42 | 52 | }; |
0 commit comments