Skip to content

Commit ac612d9

Browse files
committed
feat(ModalTopActions): allow renderAction to be either used directly or as a function
1 parent 8388271 commit ac612d9

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

packages/core/src/components/ModalNew/ModalTopActions/ModalTopActions.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import React from "react";
22
import styles from "./ModalTopActions.module.scss";
3-
import { ModalTopActionsButtonColor, ModalTopActionsProps } from "./ModalTopActions.types";
3+
import { ModalTopActionsButtonColor, ModalTopActionsColor, ModalTopActionsProps } from "./ModalTopActions.types";
44
import Flex from "../../Flex/Flex";
55
import IconButton from "../../IconButton/IconButton";
66
import { CloseSmall } from "../../Icon/Icons";
77
import { ButtonColor } from "../../Button/ButtonConstants";
88

9+
const colorToButtonColor: Record<ModalTopActionsColor, ModalTopActionsButtonColor> = {
10+
dark: ButtonColor.ON_INVERTED_BACKGROUND,
11+
light: ButtonColor.ON_PRIMARY_COLOR
12+
};
13+
914
const ModalTopActions = ({ renderAction, color, closeButtonAriaLabel, onClose }: ModalTopActionsProps) => {
10-
const buttonColor: ModalTopActionsButtonColor =
11-
color === "dark"
12-
? ButtonColor.ON_INVERTED_BACKGROUND
13-
: color === "light"
14-
? ButtonColor.ON_PRIMARY_COLOR
15-
: ButtonColor.PRIMARY;
15+
const buttonColor = colorToButtonColor[color] || ButtonColor.PRIMARY;
1616

1717
return (
1818
<Flex className={styles.actions}>
19-
{/* this allows passing back to consumer the color he chose, so he won't have to define it twice */}
20-
{renderAction && renderAction(buttonColor)}
19+
{typeof renderAction === "function" ? renderAction(buttonColor) : renderAction}
2120
<IconButton
2221
icon={CloseSmall}
2322
onClick={onClose}

packages/core/src/components/ModalNew/ModalTopActions/ModalTopActions.types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ export type ModalTopActionsButtonColor =
1010
| ButtonColor.ON_INVERTED_BACKGROUND;
1111

1212
export interface ModalTopActionsProps {
13-
renderAction?: (color?: ModalTopActionsButtonColor) => React.ReactElement<typeof MenuButton | typeof IconButton>;
13+
/**
14+
* action can be passed either as a function or direct
15+
* it allows passing back to consumer the color he chose, so he won't have to define it twice
16+
*/
17+
renderAction?:
18+
| React.ReactElement<typeof MenuButton | typeof IconButton>
19+
| ((color?: ModalTopActionsButtonColor) => React.ReactElement<typeof MenuButton | typeof IconButton>);
1420
color?: ModalTopActionsColor;
1521
closeButtonAriaLabel?: string;
1622
onClose?: React.MouseEventHandler<HTMLDivElement>;

packages/core/src/components/ModalNew/ModalTopActions/__tests__/ModalTopActions.test.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe("ModalTopActions", () => {
3232
expect(() => getByLabelText(closeButtonAriaLabel)).not.toThrow();
3333
});
3434

35-
it("renders the action button using the renderAction prop", () => {
35+
it("renders the action button using the renderAction prop as a function", () => {
3636
const renderAction = jest.fn(color => <IconButton data-testid="extra-action" icon={FeedbackIcon} color={color} />);
3737
const { getByTestId } = render(<ModalTopActions renderAction={renderAction} />);
3838

@@ -46,6 +46,15 @@ describe("ModalTopActions", () => {
4646
expect(renderAction).toHaveBeenCalledWith(ButtonColor.ON_INVERTED_BACKGROUND);
4747
});
4848

49+
it("renders the action button using the renderAction prop directly", () => {
50+
const renderAction = (
51+
<IconButton data-testid="extra-action" icon={FeedbackIcon} color={IconButton.colors.ON_PRIMARY_COLOR} />
52+
);
53+
const { getByTestId } = render(<ModalTopActions renderAction={renderAction} />);
54+
55+
expect(within(getByTestId("extra-action")).getByTestId("icon")).toBeInTheDocument();
56+
});
57+
4958
it("applies the correct color when 'dark' is passed", () => {
5059
const { getByLabelText } = render(<ModalTopActions color="dark" closeButtonAriaLabel={closeButtonAriaLabel} />);
5160
expect(getByLabelText(closeButtonAriaLabel)).toHaveClass(camelCase("color-" + ButtonColor.ON_INVERTED_BACKGROUND));

0 commit comments

Comments
 (0)