UNPKG

@undermuz/react-json-form

Version:
38 lines (37 loc) 944 B
// src/useSubmit.ts import { useCallback } from "react"; var hasErrors = (errors) => errors !== null && errors !== void 0 && Object.keys(errors).length > 0; var useSubmit = (ref, onSubmit) => { return useCallback( (event) => { event?.preventDefault(); if (!ref.current) { console.error("ref is null"); return; } if (Array.isArray(ref.current)) { const formErrors2 = ref.current.map((r) => r.validate(false)); const fromValues = ref.current.map((r) => r.values()); onSubmit( fromValues, formErrors2, formErrors2.some((e) => hasErrors(e)) ); return; } ref.current.setTouched(null, true, false); const formErrors = ref.current.validate(false); return onSubmit( ref.current.values(), formErrors, !hasErrors(formErrors) ); }, [onSubmit] ); }; export { useSubmit };