Skip to content

Commit

Permalink
fix(menu): fixed DropdownMenu not being able to provide style and cla…
Browse files Browse the repository at this point in the history
…ssName to Menu

Closes #989
  • Loading branch information
mlaursen committed Oct 23, 2020
1 parent 6b5bb41 commit 7823fea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
16 changes: 15 additions & 1 deletion packages/menu/src/DropdownMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from "react";
import React, { CSSProperties, forwardRef } from "react";
import { useIcon } from "@react-md/icon";
import { RenderConditionalPortalProps } from "@react-md/portal";

Expand Down Expand Up @@ -38,6 +38,16 @@ export interface BaseDropdownMenuProps
*/
menuLabelledBy?: string;

/**
* An optional style object to pass to the `menuRenderer`/`Menu` component.
*/
menuStyle?: CSSProperties;

/**
* An optional className to pass to the `menuRenderer`/`Menu` component.
*/
menuClassName?: string;

/**
* A custom menu renderer to use. This defaults to just rendering the `Menu`
* component with the base required props and a generated id from the button
Expand Down Expand Up @@ -82,6 +92,8 @@ export const DropdownMenu = forwardRef<HTMLButtonElement, DropdownMenuProps>(
anchor,
menuLabel,
menuLabelledBy,
menuStyle,
menuClassName,
menuRenderer = defaultMenuRenderer,
items,
itemRenderer = defaultMenuItemRenderer,
Expand Down Expand Up @@ -143,6 +155,8 @@ export const DropdownMenu = forwardRef<HTMLButtonElement, DropdownMenuProps>(
"aria-labelledby": labelledBy as string,
id: `${id}-menu`,
controlId: id,
style: menuStyle,
className: menuClassName,
anchor,
positionOptions,
closeOnScroll,
Expand Down
3 changes: 2 additions & 1 deletion packages/menu/src/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const Menu = forwardRef<HTMLDivElement, MenuProps>(function Menu(
role = "menu",
tabIndex = -1,
controlId,
style: propStyle,
className,
visible,
onRequestClose,
Expand Down Expand Up @@ -263,7 +264,7 @@ export const Menu = forwardRef<HTMLDivElement, MenuProps>(function Menu(
ref={ref}
role={role}
tabIndex={tabIndex}
style={style}
style={{ ...propStyle, ...style }}
className={cn(block({ horizontal }), className)}
onClick={onClick}
onKeyDown={onKeyDown}
Expand Down
22 changes: 21 additions & 1 deletion packages/menu/src/__tests__/DropdownMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable react/jsx-key */
import React from "react";
import React, { CSSProperties } from "react";
import { fireEvent, render } from "@testing-library/react";

import { DropdownMenu } from "../DropdownMenu";
Expand Down Expand Up @@ -56,4 +56,24 @@ describe("DropdownMenu", () => {
rerender(<DropdownMenu {...PROPS} items={items4} />);
expect(container).toMatchSnapshot();
});

it("should pass the menuStyle and menuClassName props to the menu correctly", () => {
const menuStyle: CSSProperties = { color: "red" };
const menuClassName = "my-custom-class-name";
const { getByRole } = render(
<DropdownMenu
{...PROPS}
menuStyle={menuStyle}
menuClassName={menuClassName}
items={["Item 1", "Item 2", "Item 3"]}
/>
);

const button = getByRole("button");
fireEvent.click(button);

const menu = getByRole("menu");
expect(menu.style.color).toBe(menuStyle.color);
expect(menu.className).toContain(menuClassName);
});
});
4 changes: 3 additions & 1 deletion packages/menu/src/defaultMenuRenderer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactElement, ReactNode } from "react";
import React, { CSSProperties, ReactElement, ReactNode } from "react";
import { List } from "@react-md/list";
import { RenderConditionalPortalProps } from "@react-md/portal";
import { LabelRequiredForA11y } from "@react-md/utils";
Expand Down Expand Up @@ -33,6 +33,8 @@ export interface AllInjectedMenuProps
RenderConditionalPortalProps {
"aria-label"?: string;
"aria-labelledby"?: string;
style?: CSSProperties;
className?: string;
onClick?: React.MouseEventHandler<HTMLDivElement>;
onKeyDown?: React.KeyboardEventHandler<HTMLDivElement>;
}
Expand Down

0 comments on commit 7823fea

Please sign in to comment.