Skip to content

Instantly share code, notes, and snippets.

@Streeterxs
Created June 16, 2020 22:46
Show Gist options
  • Save Streeterxs/53e0ccc23854a1c1c6257d0a6ac6fccb to your computer and use it in GitHub Desktop.
Save Streeterxs/53e0ccc23854a1c1c6257d0a6ac6fccb to your computer and use it in GitHub Desktop.
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