UNPKG

3.55 kBJavaScriptView Raw
1/*
2 * Copyright 2016 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 { __extends } from "tslib";
17import * as React from "react";
18import * as ReactDOM from "react-dom";
19import { CONTEXTMENU_WARN_DECORATOR_NEEDS_REACT_ELEMENT, CONTEXTMENU_WARN_DECORATOR_NO_METHOD, } from "../../common/errors";
20import { getDisplayName, isFunction } from "../../common/utils";
21import { isDarkTheme } from "../../common/utils/isDarkTheme";
22import * as ContextMenu from "./contextMenu";
23/* eslint-disable deprecation/deprecation */
24/** @deprecated use ContextMenu2 */
25export function ContextMenuTarget(WrappedComponent) {
26 var _a;
27 if (!isFunction(WrappedComponent.prototype.renderContextMenu)) {
28 console.warn(CONTEXTMENU_WARN_DECORATOR_NO_METHOD);
29 }
30 return _a = /** @class */ (function (_super) {
31 __extends(ContextMenuTargetClass, _super);
32 function ContextMenuTargetClass() {
33 return _super !== null && _super.apply(this, arguments) || this;
34 }
35 ContextMenuTargetClass.prototype.render = function () {
36 var _this = this;
37 var element = _super.prototype.render.call(this);
38 if (element == null) {
39 // always return `element` in case caller is distinguishing between `null` and `undefined`
40 return element;
41 }
42 if (!React.isValidElement(element)) {
43 console.warn(CONTEXTMENU_WARN_DECORATOR_NEEDS_REACT_ELEMENT);
44 return element;
45 }
46 var oldOnContextMenu = element.props.onContextMenu;
47 var onContextMenu = function (e) {
48 // support nested menus (inner menu target would have called preventDefault())
49 if (e.defaultPrevented) {
50 return;
51 }
52 if (isFunction(_this.renderContextMenu)) {
53 var menu = _this.renderContextMenu(e);
54 if (menu != null) {
55 // HACKHACK: see https://github.com/palantir/blueprint/issues/3979
56 /* eslint-disable-next-line react/no-find-dom-node */
57 var darkTheme = isDarkTheme(ReactDOM.findDOMNode(_this));
58 e.preventDefault();
59 ContextMenu.show(menu, { left: e.clientX, top: e.clientY }, _this.onContextMenuClose, darkTheme);
60 }
61 }
62 oldOnContextMenu === null || oldOnContextMenu === void 0 ? void 0 : oldOnContextMenu(e);
63 };
64 return React.cloneElement(element, { onContextMenu: onContextMenu });
65 };
66 return ContextMenuTargetClass;
67 }(WrappedComponent)),
68 _a.displayName = "ContextMenuTarget(" + getDisplayName(WrappedComponent) + ")",
69 _a;
70}
71//# sourceMappingURL=contextMenuTarget.js.map
\No newline at end of file