From 6c4b9d948d0e15872ee230b6290f5f760add6064 Mon Sep 17 00:00:00 2001 From: Jared White Date: Tue, 23 Apr 2024 15:02:43 -0700 Subject: [PATCH] feat: add onClose feature to CommonMessage (#81) --- src/blocks/__stories__/Alert.stories.tsx | 2 +- src/blocks/__tests__/CommonMessage.test.tsx | 35 +++++++++- src/blocks/shared/CommonMessage.tsx | 72 ++++++++++++--------- 3 files changed, 76 insertions(+), 33 deletions(-) diff --git a/src/blocks/__stories__/Alert.stories.tsx b/src/blocks/__stories__/Alert.stories.tsx index 050a069..4b7c52e 100644 --- a/src/blocks/__stories__/Alert.stories.tsx +++ b/src/blocks/__stories__/Alert.stories.tsx @@ -53,7 +53,7 @@ export const withLink = () => ( export const wideContainerLayout = () => ( <> - + alert("Closing!")}> An alert that scales to full container width diff --git a/src/blocks/__tests__/CommonMessage.test.tsx b/src/blocks/__tests__/CommonMessage.test.tsx index 0063cea..b4486c7 100644 --- a/src/blocks/__tests__/CommonMessage.test.tsx +++ b/src/blocks/__tests__/CommonMessage.test.tsx @@ -1,4 +1,4 @@ -import { render, cleanup } from "@testing-library/react" +import { render, cleanup, fireEvent } from "@testing-library/react" import CommonMessage from "../shared/CommonMessage" afterEach(cleanup) @@ -6,7 +6,11 @@ afterEach(cleanup) describe("", () => { it("displays the message", () => { const content = "Common message here" - const { getByText, container } = render({content}) + const { getByText, container } = render( + + {content} + + ) expect(getByText(content)).toBeInTheDocument() expect(container.querySelector("#test-id")).not.toBeNull() expect(container.querySelector("#test-id.test-class")).not.toBeNull() @@ -15,11 +19,36 @@ describe("", () => { it("supports variants", () => { const content = "Common message here" - const { getByText, container } = render({content}) + const { getByText, container } = render( + + {content} + + ) expect(getByText(content)).toBeInTheDocument() expect(container.querySelector("#test-id.test-class")).not.toBeNull() expect(container.querySelector("#test-id[data-variant=warn]")).not.toBeNull() expect(container.querySelector("button[aria-label=Close]")).not.toBeNull() expect(container.querySelector("svg.fa-clock")).not.toBeNull() }) + + it("supports a custom onClose callback", () => { + let wasClosed = false + const content = "Click me" + const { getByLabelText } = render( + { + wasClosed = true + }} + > + {content} + + ) + + fireEvent.click(getByLabelText("Close")) + expect(wasClosed).toBeTruthy() + }) }) diff --git a/src/blocks/shared/CommonMessage.tsx b/src/blocks/shared/CommonMessage.tsx index f734072..4aa1993 100644 --- a/src/blocks/shared/CommonMessage.tsx +++ b/src/blocks/shared/CommonMessage.tsx @@ -45,6 +45,8 @@ export interface CommonMessageProps { customIcon?: React.ReactNode /** If the component can hide via a close icon */ closeable?: boolean + /** A callback function when the component has closed */ + onClose?: () => void /** Scale to fit component to its container */ fullwidth?: boolean /** Element ID */ @@ -59,36 +61,48 @@ export interface CommonMessageProps { tabIndex?: number } -const CommonMessage = forwardRef((props: CommonMessageProps, ref: React.ForwardedRef) => { - const [visible, toggler] = useToggle(true) - const classNames = ["seeds-common-message"] - if (props.fullwidth) classNames.push("is-fullwidth") - if (props.className) classNames.push(props.className) +const CommonMessage = forwardRef( + (props: CommonMessageProps, ref: React.ForwardedRef) => { + const [visible, toggler] = useToggle(true) + const classNames = ["seeds-common-message"] + if (props.fullwidth) classNames.push("is-fullwidth") + if (props.className) classNames.push(props.className) - const variant = props.variant || "primary" + const variant = props.variant || "primary" - return props.children ? ( - - ) : null -}) + return props.children ? ( + + ) : null + } +) export default CommonMessage