Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
stop propagation of form submit event (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
diogoViana95 authored Mar 31, 2024
1 parent e9f1cea commit 3db10af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-maps-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@deadcow-enterprises/react-form-hook": patch
---

stop propagation of form submit event
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v18.18.2
v20.10.0
18 changes: 12 additions & 6 deletions src/hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderHook, act } from "@testing-library/react";
import { z } from "zod";
import { act, renderHook } from "@testing-library/react";
import { FormEvent } from "react";
import { describe, it, expect, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { z } from "zod";
import { useForm } from "./hook";
import { FormError } from "./types";
interface FormType {
Expand Down Expand Up @@ -214,14 +214,16 @@ describe("Hook", () => {

const fn = vi.fn();

const ev: Pick<FormEvent, "preventDefault"> = {
const ev: Pick<FormEvent, "preventDefault" | "stopPropagation"> = {
preventDefault: vi.fn(),
stopPropagation: vi.fn(),
};

act(() => {
result.current.handleSubmit(fn)(ev as any);
});
expect(ev.preventDefault).toBeCalled();
expect(ev.stopPropagation).toBeCalled();
expect(fn).toBeCalledWith(formInitialValue);
});
it("Should run promise callback function on submit", () => {
Expand All @@ -246,14 +248,16 @@ describe("Hook", () => {

const fn = vi.fn<any, Promise<void>>();

const ev: Pick<FormEvent, "preventDefault"> = {
const ev: Pick<FormEvent, "preventDefault" | "stopPropagation"> = {
preventDefault: vi.fn(),
stopPropagation: vi.fn(),
};

act(() => {
result.current.handleSubmit(fn)(ev as any);
});
expect(ev.preventDefault).toBeCalled();
expect(ev.stopPropagation).toBeCalled();
expect(fn).toBeCalledWith(formInitialValue);
});
it("Should not run callback function on submit if there are errors", () => {
Expand Down Expand Up @@ -289,14 +293,16 @@ describe("Hook", () => {

const fn = vi.fn();

const ev: Pick<FormEvent, "preventDefault"> = {
const ev: Pick<FormEvent, "preventDefault" | "stopPropagation"> = {
preventDefault: vi.fn(),
stopPropagation: vi.fn(),
};

act(() => {
result.current.handleSubmit(fn)(ev as any);
});
expect(ev.preventDefault).toBeCalled();
expect(ev.stopPropagation).toBeCalled();
expect(fn).not.toHaveBeenCalled();
expect(result.current.errors).toStrictEqual({
propertyA: ["Should be X characters long"],
Expand Down
1 change: 1 addition & 0 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function useForm<T extends Record<string, any>>(
},
handleSubmit: (cb) => {
return async (ev) => {
ev.stopPropagation();
ev.preventDefault();
const valid = updateValidity();
if (!valid) {
Expand Down

0 comments on commit 3db10af

Please sign in to comment.