From dcd12547a902061907dc73384fe39467c7f3d398 Mon Sep 17 00:00:00 2001 From: mainframev Date: Tue, 28 Mar 2023 20:26:01 +0200 Subject: [PATCH] chore: fixes after review --- .../src/Modal/Modal.stories.tsx | 17 --------- .../ModalFooter/__tests__/index.test.tsx | 35 +++++++++++++++++++ .../src/Modal/ModalFooter/index.tsx | 12 +++---- .../src/Modal/__tests__/index.test.tsx | 4 ++- 4 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 packages/orbit-components/src/Modal/ModalFooter/__tests__/index.test.tsx diff --git a/packages/orbit-components/src/Modal/Modal.stories.tsx b/packages/orbit-components/src/Modal/Modal.stories.tsx index bde09698d7..fcd0e89062 100644 --- a/packages/orbit-components/src/Modal/Modal.stories.tsx +++ b/packages/orbit-components/src/Modal/Modal.stories.tsx @@ -89,23 +89,6 @@ Sizes.story = { }, }; -export const ModalFooterOnly = () => { - const isBackButton = boolean("isBackButton", false); - const isNextButton = boolean("isNextButton", true); - - return ( - - - The dinosaurs looked at Chuck Norris the wrong way once. You know what happened to them. - - - {isBackButton ? : null} - {isNextButton ? : null} - - - ); -}; - export const ShortModal = () => { const { Container, onClose } = useModal(); return ( diff --git a/packages/orbit-components/src/Modal/ModalFooter/__tests__/index.test.tsx b/packages/orbit-components/src/Modal/ModalFooter/__tests__/index.test.tsx new file mode 100644 index 0000000000..6b6cad4584 --- /dev/null +++ b/packages/orbit-components/src/Modal/ModalFooter/__tests__/index.test.tsx @@ -0,0 +1,35 @@ +import { render, screen } from "@testing-library/react"; +import React from "react"; + +import ModalFooter from ".."; +import Button from "../../../Button"; +import { getBreakpointWidth } from "../../../utils/mediaQuery"; +import theme from "../../../defaultTheme"; + +describe("ModalFooter", () => { + it("should render save button with flex-end", () => { + render( + + {null} + + , + ); + + expect(screen.getByTestId("footer")).toHaveStyleRule("justify-content", "flex-end", { + media: getBreakpointWidth("largeMobile", theme), + }); + }); + + it("should render buttons with space-between", () => { + render( + + + + , + ); + + expect(screen.getByTestId("footer")).toHaveStyleRule("justify-content", "space-between", { + media: getBreakpointWidth("largeMobile", theme), + }); + }); +}); diff --git a/packages/orbit-components/src/Modal/ModalFooter/index.tsx b/packages/orbit-components/src/Modal/ModalFooter/index.tsx index 9fbfbb5bd5..38a6bbd91c 100644 --- a/packages/orbit-components/src/Modal/ModalFooter/index.tsx +++ b/packages/orbit-components/src/Modal/ModalFooter/index.tsx @@ -68,16 +68,17 @@ StyledModalFooter.defaultProps = { const getChildFlex = (flex: Props["flex"], key: number) => Array.isArray(flex) && flex.length !== 1 ? flex[key] || flex[0] : flex; -const wrappedChildren = (children: React.ReactElement[], flex: Props["flex"]) => { +const wrappedChildren = (children: React.ReactNode, flex: Props["flex"]) => { return React.Children.map(children, (child, key) => { + if (!React.isValidElement(child)) return null; return ( {React.cloneElement(child, { - // @ts-expect-error React.cloneElement issue + // @ts-expect-error React.cloneElement issue ref: child.ref ? node => { // Call the original ref, if any - // @ts-expect-error React.cloneElement issue + // @ts-expect-error React.cloneElement issue const { ref } = child; if (typeof ref === "function") { ref(node); @@ -95,7 +96,6 @@ const wrappedChildren = (children: React.ReactElement[], flex: Props["f const ModalFooter = ({ dataTest, children, flex = "0 1 auto" }: Props) => { const { isMobileFullPage, setFooterHeight } = React.useContext(ModalContext); const containerRef = React.useRef(null); - const filteredChildren = React.Children.toArray(children).filter(React.isValidElement); useModalContextFunctions(); @@ -117,9 +117,9 @@ const ModalFooter = ({ dataTest, children, flex = "0 1 auto" }: Props) => { ref={containerRef} data-test={dataTest} isMobileFullPage={isMobileFullPage} - childrenLength={React.Children.count(filteredChildren)} + childrenLength={React.Children.toArray(children).length} > - {flex && wrappedChildren(filteredChildren, flex)} + {flex && wrappedChildren(children, flex)} ); }; diff --git a/packages/orbit-components/src/Modal/__tests__/index.test.tsx b/packages/orbit-components/src/Modal/__tests__/index.test.tsx index cc0e7d2c11..52475f66fa 100644 --- a/packages/orbit-components/src/Modal/__tests__/index.test.tsx +++ b/packages/orbit-components/src/Modal/__tests__/index.test.tsx @@ -35,7 +35,9 @@ describe("Modal", () => { header content section content - footer content + +

footer content

+
, );