From 10a094bee1a148e61604f27c95f865d8fa72799c Mon Sep 17 00:00:00 2001
From: Vladimir Cucu <108150922+vladimir-cucu@users.noreply.github.com>
Date: Tue, 26 Mar 2024 08:11:21 +0200
Subject: [PATCH] fix: stop click event propagation in Modal (#1058)
---
.../ConfirmationModal.test.tsx | 69 +++++++++++++++++++
.../ConfirmationModal/ConfirmationModal.tsx | 20 +++++-
src/components/Modal/Modal.test.tsx | 32 +++++++++
src/components/Modal/Modal.tsx | 58 ++++++++++------
4 files changed, 156 insertions(+), 23 deletions(-)
diff --git a/src/components/ConfirmationModal/ConfirmationModal.test.tsx b/src/components/ConfirmationModal/ConfirmationModal.test.tsx
index a984f374d..73830c53b 100644
--- a/src/components/ConfirmationModal/ConfirmationModal.test.tsx
+++ b/src/components/ConfirmationModal/ConfirmationModal.test.tsx
@@ -55,4 +55,73 @@ describe("ConfirmationModal ", () => {
await userEvent.click(screen.getByText("Proceed"));
expect(onConfirm).toHaveBeenCalled();
});
+
+ it("should stop click event propagation on cancel by default", async () => {
+ const handleExternalClick = jest.fn();
+ render(
+
+
+ Test click propagation
+
+
+ );
+
+ await userEvent.click(screen.getByText("Go back"));
+ expect(handleExternalClick).not.toHaveBeenCalled();
+ });
+
+ it("should propagate click event on cancel", async () => {
+ const handleExternalClick = jest.fn();
+ render(
+
+
+ Test click propagation
+
+
+ );
+
+ await userEvent.click(screen.getByText("Go back"));
+ expect(handleExternalClick).toHaveBeenCalledTimes(1);
+ });
+
+ it("should stop click event propagation on confirm by default", async () => {
+ const handleExternalClick = jest.fn();
+ render(
+
+
+ Test click propagation
+
+
+ );
+
+ await userEvent.click(screen.getByText("Proceed"));
+ expect(handleExternalClick).not.toHaveBeenCalled();
+ });
+
+ it("should propagate click event on confirm", async () => {
+ const handleExternalClick = jest.fn();
+ render(
+
+
+ Test click propagation
+
+
+ );
+
+ await userEvent.click(screen.getByText("Proceed"));
+ expect(handleExternalClick).toHaveBeenCalledTimes(1);
+ });
});
diff --git a/src/components/ConfirmationModal/ConfirmationModal.tsx b/src/components/ConfirmationModal/ConfirmationModal.tsx
index b647790c1..41edfe55e 100644
--- a/src/components/ConfirmationModal/ConfirmationModal.tsx
+++ b/src/components/ConfirmationModal/ConfirmationModal.tsx
@@ -29,7 +29,7 @@ export type Props = PropsWithSpread<
/**
* Function to perform the action prompted by the modal.
*/
- onConfirm: (e: MouseEvent) => void;
+ onConfirm: (event: MouseEvent) => void;
},
Omit
>;
@@ -43,18 +43,32 @@ export const ConfirmationModal = ({
onConfirm,
...props
}: Props): ReactElement => {
+ const handleClick =
+ (action: A | null | undefined) =>
+ (event: MouseEvent) => {
+ if (!props.shouldPropagateClickEvent) {
+ event.stopPropagation();
+ }
+ if (action) {
+ action(event);
+ }
+ };
+
return (
{confirmExtra}
-