Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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