UNPKG

4.03 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";
17/**
18 * @fileoverview This component is DEPRECATED, and the code is frozen.
19 * All changes & bugfixes should be made to ContextMenu2 instead.
20 */
21/* eslint-disable deprecation/deprecation */
22import * as React from "react";
23import * as ReactDOM from "react-dom";
24import { isDarkTheme, isFunction } from "../common/utils";
25import { show as showLegacyContextMenu } from "./contextMenuLegacy";
26import { getDisplayName } from "./legacyCommon";
27export var CONTEXTMENU_WARN_DECORATOR_NO_METHOD = "[Blueprint] @ContextMenuTarget-decorated class should implement renderContextMenu.";
28export var CONTEXTMENU_WARN_DECORATOR_NEEDS_REACT_ELEMENT = "[Blueprint] \"@ContextMenuTarget-decorated components must return a single JSX.Element or an empty render.";
29/**
30 * ContextMenuTarget decorator.
31 *
32 * @see https://blueprintjs.com/docs/#core/components/context-menu.decorator-usage
33 * @deprecated use ContextMenu2
34 */
35export function ContextMenuTargetLegacy(WrappedComponent) {
36 var _a;
37 if (!isFunction(WrappedComponent.prototype.renderContextMenu)) {
38 console.warn(CONTEXTMENU_WARN_DECORATOR_NO_METHOD);
39 }
40 return _a = /** @class */ (function (_super) {
41 __extends(ContextMenuTargetClass, _super);
42 function ContextMenuTargetClass() {
43 return _super !== null && _super.apply(this, arguments) || this;
44 }
45 ContextMenuTargetClass.prototype.render = function () {
46 var _this = this;
47 var element = _super.prototype.render.call(this);
48 if (element == null) {
49 // always return `element` in case caller is distinguishing between `null` and `undefined`
50 return element;
51 }
52 if (!React.isValidElement(element)) {
53 console.warn(CONTEXTMENU_WARN_DECORATOR_NEEDS_REACT_ELEMENT);
54 return element;
55 }
56 var oldOnContextMenu = element.props.onContextMenu;
57 var onContextMenu = function (e) {
58 // support nested menus (inner menu target would have called preventDefault())
59 if (e.defaultPrevented) {
60 return;
61 }
62 if (isFunction(_this.renderContextMenu)) {
63 var menu = _this.renderContextMenu(e);
64 if (menu != null) {
65 // HACKHACK: see https://github.com/palantir/blueprint/issues/3979
66 /* eslint-disable-next-line react/no-find-dom-node */
67 var darkTheme = isDarkTheme(ReactDOM.findDOMNode(_this));
68 e.preventDefault();
69 showLegacyContextMenu(menu, { left: e.clientX, top: e.clientY }, _this.onContextMenuClose, darkTheme);
70 }
71 }
72 oldOnContextMenu === null || oldOnContextMenu === void 0 ? void 0 : oldOnContextMenu(e);
73 };
74 return React.cloneElement(element, { onContextMenu: onContextMenu });
75 };
76 return ContextMenuTargetClass;
77 }(WrappedComponent)),
78 _a.displayName = "ContextMenuTarget(".concat(getDisplayName(WrappedComponent), ")"),
79 _a;
80}
81//# sourceMappingURL=contextMenuTargetLegacy.js.map
\No newline at end of file