From 79ae416ff234321121c74cd710dbefa991d5db79 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 16 Mar 2024 14:31:54 +1000 Subject: [PATCH] feat: add resetValidation support --- README.md | 1 + packages/react-zorm/src/types.tsx | 1 + packages/react-zorm/src/use-zorm.tsx | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/README.md b/README.md index d8f1a1d..faf0469 100644 --- a/README.md +++ b/README.md @@ -468,6 +468,7 @@ Zod schema to parse the form with. - `validation: SafeParseReturnType | null`: The current Zod validation status returned by [`safeParse()`][safeparse] +- `validationReset(): void`: Reset the validation status - `validate(): SafeParseReturnType`: Manually invoke validation - `fields: FieldChain`: The fields chain - `errors: ErroChain`: The error chain diff --git a/packages/react-zorm/src/types.tsx b/packages/react-zorm/src/types.tsx index 8a3a098..a09199c 100644 --- a/packages/react-zorm/src/types.tsx +++ b/packages/react-zorm/src/types.tsx @@ -117,6 +117,7 @@ export interface Zorm { errors: ErrorChainFromSchema & ErrorGetter; validate(): SafeParseResult; validation: SafeParseResult | null; + validationReset(): void; customIssues: ZodIssue[]; } diff --git a/packages/react-zorm/src/use-zorm.tsx b/packages/react-zorm/src/use-zorm.tsx index 4964c3f..f097c1c 100644 --- a/packages/react-zorm/src/use-zorm.tsx +++ b/packages/react-zorm/src/use-zorm.tsx @@ -77,6 +77,11 @@ export function useZorm>( return res; }, [getForm, schema]); + const validationReset = useCallback(() => { + setValidation(null); + submittedOnceRef.current = false; + }, []); + const changeHandler = useCallback(() => { if (!submittedOnceRef.current) { return; @@ -174,6 +179,7 @@ export function useZorm>( return formRef.current; }, validation, + validationReset, fields, errors, customIssues: customIssues, @@ -185,5 +191,6 @@ export function useZorm>( schema, validate, validation, + validationReset, ]); }