Created
September 1, 2019 03:09
-
-
Save pixeldrew/a1cccb125c9680e5424fd9fdee82ac88 to your computer and use it in GitHub Desktop.
Simple Form Hook
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
function useForm(callback, defaultValues, schema) { | |
const [values, setValues] = useState({ ...defaultValues }); | |
const [errors, setErrors] = useState([]); | |
const [validateOnChange, setValidateOnChange] = useState(false); | |
async function validate() { | |
if (schema) { | |
const errors = await catchErrors(); | |
if (errors.length > 0) { | |
setErrors([...errors]); | |
return false; | |
} | |
setErrors([]); | |
} | |
return true; | |
} | |
async function catchErrors() { | |
let caughtErrors = []; | |
for (let [id] of Object.entries(values)) { | |
try { | |
await schema.validateAt(id, values); | |
} catch ({ errors }) { | |
caughtErrors.push({ | |
id, | |
msgs: errors[0] | |
}); | |
} | |
} | |
return caughtErrors; | |
} | |
async function handleSubmit(event) { | |
if (event) event.preventDefault(); | |
if (schema) { | |
setValidateOnChange(true); | |
if (await validate()) { | |
callback(values); | |
} | |
} else { | |
callback(values); | |
} | |
} | |
function handleChange(name) { | |
if (name.target) { | |
name.persist(); | |
const key = name.target.name || name.target.id; | |
if (!key) { | |
throw new Error( | |
"no name provided to handleChange. input must have either a name or an id" | |
); | |
} | |
setValues(values => ({ | |
...values, | |
[key]: name.target.value | |
})); | |
} else { | |
return event => { | |
event.persist(); | |
setValues(values => ({ | |
...values, | |
[name]: event.target.value | |
})); | |
}; | |
} | |
} | |
useEffect(() => { | |
validateOnChange && validate(); | |
}, [values]); | |
return { | |
handleChange, | |
handleSubmit, | |
values, | |
errors, | |
hasError: id => errors.findIndex(e => e.id === id) >= 0 | |
}; | |
} | |
export default useForm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: