feat(hooks): return setFieldValue from useField

This commit is contained in:
Aminejv 2021-08-24 20:45:05 +01:00
parent c38f84733d
commit 2e26c97ae3

View File

@ -173,15 +173,16 @@ export const useField = ({
if (event) event(e); if (event) event(e);
}); });
/** ---------- NOTE(amine): Input Handlers ---------- */ const setFieldValue = (value) =>
const handleFieldChange = (e) =>
setState((prev) => ({ setState((prev) => ({
...prev, ...prev,
value: e.target.value, value,
error: undefined, error: undefined,
touched: false, touched: false,
})); }));
/** ---------- NOTE(amine): Input Handlers ---------- */
const handleFieldChange = (e) => setFieldValue(e.target.value);
const handleOnBlur = () => { const handleOnBlur = () => {
// NOTE(amine): validate the inputs onBlur and touch the current input // NOTE(amine): validate the inputs onBlur and touch the current input
let error = {}; let error = {};
@ -204,10 +205,10 @@ export const useField = ({
if (!onSubmit) return; if (!onSubmit) return;
setState((prev) => ({ ...prev, isSubmitting: true })); setState((prev) => ({ ...prev, isSubmitting: true }));
onSubmit(state.value) onSubmit(state.value)
.then(() => { ?.then(() => {
setState((prev) => ({ ...prev, isSubmitting: false })); setState((prev) => ({ ...prev, isSubmitting: false }));
}) })
.catch(() => { ?.catch(() => {
setState((prev) => ({ ...prev, isSubmitting: false })); setState((prev) => ({ ...prev, isSubmitting: false }));
}); });
}; };
@ -223,7 +224,7 @@ export const useField = ({
onSubmit: handleFormOnSubmit, onSubmit: handleFormOnSubmit,
}); });
return { getFieldProps, value: state.value, isSubmitting: state.isSubmitting }; return { getFieldProps, value: state.value, setFieldValue, isSubmitting: state.isSubmitting };
}; };
export const useIntersection = ({ onIntersect, ref }, dependencies = []) => { export const useIntersection = ({ onIntersect, ref }, dependencies = []) => {