From 68bc74fd6de97a0a6bb10b2aed8830ee2bf7aa8e Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Fri, 25 Feb 2022 16:24:42 -0500 Subject: [PATCH] Allow validation options to be overridden for submit --- README.md | 1 + src/useSubmit.ts | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fa45533..9c11351 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,7 @@ Create a submit handler for the form. - `form` **[Form](#form)<Value, Result>** - `fn` **SubmitFn<Result>** +- `opts` **ValidateOptions** (optional, default `{touch:true}`) #### Examples diff --git a/src/useSubmit.ts b/src/useSubmit.ts index e4b83eb..e427424 100644 --- a/src/useSubmit.ts +++ b/src/useSubmit.ts @@ -1,5 +1,5 @@ import { FormEvent } from "react"; -import { Form, Submit } from "./types"; +import { Form, Submit, ValidateOptions } from "./types"; import { toPromise } from "./utilities/toPromise"; import { useMounted } from "./utilities/useMounted"; import { useEventCallback } from "./utilities/useEventCallback"; @@ -20,7 +20,8 @@ type SubmitFn = (values: T) => void | Promise; */ export function useSubmit( form: Form, - fn: SubmitFn + fn: SubmitFn, + opts: ValidateOptions = { touch: true } ): Submit { const isMounted = useMounted(); @@ -36,7 +37,7 @@ export function useSubmit( form.setSubmitting(true); - return toPromise(() => form.validate({ touch: true })) + return toPromise(() => form.validate(opts)) .then(result => { if (!result.valid) return;