Skip to content

Commit

Permalink
[popover2] feat(ContextMenu2): allow specifying custom wrapper tag (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya authored May 10, 2021
1 parent f5aa611 commit 06b2ece
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
24 changes: 19 additions & 5 deletions packages/popover2/src/contextMenu2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export interface ContextMenu2Props
* React state (which is an error to do in the render code path of this component).
*/
onContextMenu?: React.MouseEventHandler<HTMLElement>;

/**
* HTML tag to use for container element. Only used if this component's children are specified as
* React node(s), not when it is a render function (in that case, you get to render whatever tag
* you wish).
*
* @default "div"
*/
tagName?: keyof JSX.IntrinsicElements;
}

export const ContextMenu2: React.FC<ContextMenu2Props> = ({
Expand All @@ -111,6 +120,7 @@ export const ContextMenu2: React.FC<ContextMenu2Props> = ({
transitionDuration = 100,
onContextMenu,
popoverClassName,
tagName = "div",
...restProps
}) => {
const [targetOffset, setTargetOffset] = React.useState<Offset>({ left: 0, top: 0 });
Expand Down Expand Up @@ -210,11 +220,15 @@ export const ContextMenu2: React.FC<ContextMenu2Props> = ({
ref: containerRef,
});
} else {
return (
<div className={containerClassName} ref={containerRef} onContextMenu={handleContextMenu}>
{maybePopover}
{children}
</div>
return React.createElement(
tagName,
{
className: containerClassName,
onContextMenu: handleContextMenu,
ref: containerRef,
},
maybePopover,
children,
);
}
};
Expand Down
7 changes: 6 additions & 1 deletion packages/popover2/test/contextMenu2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as React from "react";

import { Menu, MenuItem } from "@blueprintjs/core";

import { ContextMenu2, ContextMenu2Props, Popover2 } from "../src";
import { Classes, ContextMenu2, ContextMenu2Props, Popover2 } from "../src";

const MENU_ITEMS = [
<MenuItem key="left" icon="align-left" text="Align Left" />,
Expand All @@ -45,6 +45,11 @@ describe("ContextMenu2", () => {
assert.isTrue(ctxMenu.find(Popover2).prop("isOpen"));
});

it("renders custom HTML tag if specified", () => {
const ctxMenu = mountTestMenu({ tagName: "span" });
assert.isTrue(ctxMenu.find(`span.${Classes.CONTEXT_MENU2}`).exists());
});

function mountTestMenu(props: Partial<ContextMenu2Props> = {}) {
return mount(
<ContextMenu2 content={MENU} transitionDuration={0} {...props}>
Expand Down

1 comment on commit 06b2ece

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[popover2] feat(ContextMenu2): allow specifying custom wrapper tag (#4709)

Previews: documentation | landing | table

Please sign in to comment.