UNPKG

5.59 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __decorate, __extends, __rest } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { polyfill } from "react-lifecycles-compat";
20import { AbstractPureComponent2, Classes, Position } from "../../common";
21import { DISPLAYNAME_PREFIX } from "../../common/props";
22import { Icon } from "../icon/icon";
23import { Popover, PopoverInteractionKind } from "../popover/popover";
24import { Text } from "../text/text";
25// this cyclic import can be removed in v4.0 (https://github.com/palantir/blueprint/issues/3829)
26// eslint-disable-next-line import/no-cycle
27import { Menu } from "./menu";
28var MenuItem = /** @class */ (function (_super) {
29 __extends(MenuItem, _super);
30 function MenuItem() {
31 return _super !== null && _super.apply(this, arguments) || this;
32 }
33 MenuItem.prototype.render = function () {
34 var _a, _b;
35 var _c = this.props, active = _c.active, className = _c.className, children = _c.children, disabled = _c.disabled, icon = _c.icon, intent = _c.intent, labelClassName = _c.labelClassName, labelElement = _c.labelElement, multiline = _c.multiline, popoverProps = _c.popoverProps, shouldDismissPopover = _c.shouldDismissPopover, text = _c.text, textClassName = _c.textClassName, _d = _c.tagName, tagName = _d === void 0 ? "a" : _d, htmlTitle = _c.htmlTitle, htmlProps = __rest(_c, ["active", "className", "children", "disabled", "icon", "intent", "labelClassName", "labelElement", "multiline", "popoverProps", "shouldDismissPopover", "text", "textClassName", "tagName", "htmlTitle"]);
36 var hasSubmenu = children != null;
37 var intentClass = Classes.intentClass(intent);
38 var anchorClasses = classNames(Classes.MENU_ITEM, intentClass, (_a = {},
39 _a[Classes.ACTIVE] = active,
40 _a[Classes.INTENT_PRIMARY] = active && intentClass == null,
41 _a[Classes.DISABLED] = disabled,
42 // prevent popover from closing when clicking on submenu trigger or disabled item
43 _a[Classes.POPOVER_DISMISS] = shouldDismissPopover && !disabled && !hasSubmenu,
44 _a), className);
45 var target = React.createElement(tagName, __assign(__assign(__assign({ tabIndex: 0 }, htmlProps), (disabled ? DISABLED_PROPS : {})), { className: anchorClasses }), React.createElement(Icon, { icon: icon }), React.createElement(Text, { className: classNames(Classes.FILL, textClassName), ellipsize: !multiline, title: htmlTitle }, text), this.maybeRenderLabel(labelElement), hasSubmenu ? React.createElement(Icon, { title: "Open sub menu", icon: "caret-right" }) : undefined);
46 var liClasses = classNames((_b = {}, _b[Classes.MENU_SUBMENU] = hasSubmenu, _b));
47 return React.createElement("li", { className: liClasses }, this.maybeRenderPopover(target, children));
48 };
49 MenuItem.prototype.maybeRenderLabel = function (labelElement) {
50 var _a = this.props, label = _a.label, labelClassName = _a.labelClassName;
51 if (label == null && labelElement == null) {
52 return null;
53 }
54 return (React.createElement("span", { className: classNames(Classes.MENU_ITEM_LABEL, labelClassName) },
55 label,
56 labelElement));
57 };
58 MenuItem.prototype.maybeRenderPopover = function (target, children) {
59 if (children == null) {
60 return target;
61 }
62 var _a = this.props, disabled = _a.disabled, popoverProps = _a.popoverProps;
63 return (
64 /* eslint-disable-next-line deprecation/deprecation */
65 React.createElement(Popover, __assign({ autoFocus: false, captureDismiss: false, disabled: disabled, enforceFocus: false, hoverCloseDelay: 0, interactionKind: PopoverInteractionKind.HOVER, modifiers: SUBMENU_POPOVER_MODIFIERS, position: Position.RIGHT_TOP, usePortal: false }, popoverProps, { content: React.createElement(Menu, null, children), minimal: true, popoverClassName: classNames(Classes.MENU_SUBMENU, popoverProps === null || popoverProps === void 0 ? void 0 : popoverProps.popoverClassName), target: target })));
66 };
67 MenuItem.defaultProps = {
68 disabled: false,
69 multiline: false,
70 popoverProps: {},
71 shouldDismissPopover: true,
72 text: "",
73 };
74 MenuItem.displayName = DISPLAYNAME_PREFIX + ".MenuItem";
75 MenuItem = __decorate([
76 polyfill
77 ], MenuItem);
78 return MenuItem;
79}(AbstractPureComponent2));
80export { MenuItem };
81var SUBMENU_POPOVER_MODIFIERS = {
82 // 20px padding - scrollbar width + a bit
83 flip: { boundariesElement: "viewport", padding: 20 },
84 // shift popover up 5px so MenuItems align
85 offset: { offset: -5 },
86 preventOverflow: { boundariesElement: "viewport", padding: 20 },
87};
88// props to ignore when disabled
89var DISABLED_PROPS = {
90 href: undefined,
91 onClick: undefined,
92 onMouseDown: undefined,
93 onMouseEnter: undefined,
94 onMouseLeave: undefined,
95 tabIndex: -1,
96};
97//# sourceMappingURL=menuItem.js.map
\No newline at end of file