Skip to content

Commit

Permalink
Merge pull request #15 from rzane/do-not-submit-twice
Browse files Browse the repository at this point in the history
Avoid double submission
  • Loading branch information
rzane committed Aug 19, 2020
2 parents 23cf95e + 03ba59c commit 1711cf6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/useSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function useSubmit<Value, Result>(
event.stopPropagation();
}

if (form.isSubmitting) {
return;
}

form.setSubmitting(true);

try {
Expand Down
8 changes: 8 additions & 0 deletions test/useSubmit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ test("prevents default and stops propagation", async () => {
expect(event.preventDefault).toHaveBeenCalled();
expect(event.stopPropagation).toHaveBeenCalled();
});

test("makes no attempt to submit when already submitting", async () => {
const form: any = { isSubmitting: true };
const submit = jest.fn();
const onSubmit = renderHook(() => useSubmit(form, submit));
await onSubmit.result.current();
expect(submit).not.toHaveBeenCalled();
});

0 comments on commit 1711cf6

Please sign in to comment.