Created
June 16, 2020 22:46
-
-
Save Streeterxs/53e0ccc23854a1c1c6257d0a6ac6fccb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const [formValidation, setFormValidation] = useState<UserFormValidation>({ | |
emailIsValid: false, | |
cpfIsValid: false, | |
nomeIsValid: false, | |
cepIsValid: false, | |
ruaIsValid: false, | |
numeroIsValid: false, | |
bairroIsValid: false, | |
cidadeIsValid: false, | |
showErrors: false | |
}); | |
const handleFormInput = <T,>(value: T, key: UserFormKeys, aditionalLogics?: boolean[]) => { | |
const keyOfForValidation = `${key}IsValid` as keyof UserFormValidation; | |
const adicionalLogicsReduced = aditionalLogics ? | |
aditionalLogics.reduce((acc, curr) => { | |
return acc && curr; | |
}, true) : | |
true; | |
if (!value || !adicionalLogicsReduced) { | |
if (formValidation[keyOfForValidation]) { | |
setFormValidation({ | |
...formValidation, | |
[keyOfForValidation]: false | |
}); | |
} | |
return; | |
} | |
if (!formValidation[keyOfForValidation]) { | |
setFormValidation({...formValidation, [keyOfForValidation]: true}); | |
} | |
const formobjkey = `${key}` as keyof UserForm | |
(formObj[formobjkey] as any) = value; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment