UNPKG

@undermuz/use-form

Version:
62 lines (61 loc) 1.61 kB
// src/useForm/useFormControl.ts import { FORM_ACTIONS } from "./reducer.js"; import { useCallback } from "react"; import { useSetCustomErrorByName, useSetCustomErrors, useSetErrors, useSetFieldTouched, useSetFieldValue, useSetTests, useSetTouched, useSetValidate, useSetValues, useValidate } from "./helpers.js"; var useFormControl = (props, store, dispatch) => { const setValues = useSetValues(props, store, dispatch); const getValues = useCallback(() => { return store.getState().values; }, []); const setTouched = useSetTouched(props, store, dispatch); const setTests = useSetTests(props, store, dispatch); const setValidate = useSetValidate(props, store, dispatch); const setErrors = useSetErrors(props, store, dispatch); const setCustomErrors = useSetCustomErrors(props, store, dispatch); const setCustomErrorByName = useSetCustomErrorByName(props, store, dispatch); const setValue = useSetFieldValue(props, store, dispatch); const setTouchedByName = useSetFieldTouched(props, store, dispatch); const validate = useValidate(props, store, dispatch); const send = useCallback((api) => { return new Promise((onResolve, onReject) => { dispatch({ type: FORM_ACTIONS.SEND_FORM, payload: { api, onResolve, onReject } }); }); }, []); return { send, validate, setValue, getValues, setTouchedByName, setTouched, setValues, setTests, setValidate, setErrors, setCustomErrors, setCustomErrorByName }; }; export { useFormControl };