diff --git a/package.json b/package.json index 4d85c8e..2543a4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@buildo/formo", - "version": "2.0.0-beta.9", + "version": "2.0.0", "main": "lib/index.js", "license": "MIT", "files": [ diff --git a/src/useFormo.ts b/src/useFormo.ts index 160627c..c0940b1 100644 --- a/src/useFormo.ts +++ b/src/useFormo.ts @@ -876,6 +876,7 @@ export function useFormo< async function handleSubmit(): Promise> { setAllTouched(); + setFormErrors(undefined); dispatch({ type: "setSubmitting", isSubmitting: true }); setSubmissionCount((count) => count + 1); try { diff --git a/test/useFormo.test.ts b/test/useFormo.test.ts index 04f59c4..cec93c4 100644 --- a/test/useFormo.test.ts +++ b/test/useFormo.test.ts @@ -677,4 +677,44 @@ describe("formo", () => { expect(onSubmit).toHaveBeenLastCalledWith("token"); }); }); + + test("formErrors are reset before next onSubmit", async () => { + const { result } = renderHook(() => + useFormo( + { + initialValues: { + username: "username", + }, + fieldValidators: (_) => ({ + username: validators.validator((u) => + u === "invalid" ? failure("InvalidUsername") : success(u) + ), + }), + }, + { + onSubmit: ({ username }) => + username === "username" + ? Promise.resolve(failure(["FormError"])) + : Promise.resolve(success(null)), + } + ) + ); + + await act(async () => { + await result.current.handleSubmit(); + }); + + expect(result.current.formErrors).toEqual(["FormError"]); + + await act(async () => { + await result.current.fieldProps("username").onChange("invalid"); + await result.current.handleSubmit(); + }); + + // form error should have been reset + expect(result.current.formErrors).toEqual(undefined); + expect(result.current.fieldProps("username").issues).toEqual([ + "InvalidUsername", + ]); + }); });