UNPKG

5.94 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, __extends } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { ChevronRight } from "@blueprintjs/icons";
20import { Classes, DISPLAYNAME_PREFIX } from "../../common";
21import { Collapse } from "../collapse/collapse";
22import { Icon } from "../icon/icon";
23/**
24 * Tree node component.
25 *
26 * @see https://blueprintjs.com/docs/#core/components/tree.tree-node
27 */
28// eslint-disable-next-line @typescript-eslint/ban-types
29var TreeNode = /** @class */ (function (_super) {
30 __extends(TreeNode, _super);
31 function TreeNode() {
32 var _this = _super !== null && _super.apply(this, arguments) || this;
33 _this.handleCaretClick = function (e) {
34 var _a;
35 e.stopPropagation();
36 var _b = _this.props, isExpanded = _b.isExpanded, onCollapse = _b.onCollapse, onExpand = _b.onExpand;
37 (_a = (isExpanded ? onCollapse : onExpand)) === null || _a === void 0 ? void 0 : _a(_this.props, _this.props.path, e);
38 };
39 _this.handleClick = function (e) {
40 var _a, _b;
41 (_b = (_a = _this.props).onClick) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props, _this.props.path, e);
42 };
43 _this.handleContentRef = function (element) {
44 var _a, _b;
45 (_b = (_a = _this.props).contentRef) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props, element);
46 };
47 _this.handleContextMenu = function (e) {
48 var _a, _b;
49 (_b = (_a = _this.props).onContextMenu) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props, _this.props.path, e);
50 };
51 _this.handleDoubleClick = function (e) {
52 var _a, _b;
53 (_b = (_a = _this.props).onDoubleClick) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props, _this.props.path, e);
54 };
55 _this.handleMouseEnter = function (e) {
56 var _a, _b;
57 (_b = (_a = _this.props).onMouseEnter) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props, _this.props.path, e);
58 };
59 _this.handleMouseLeave = function (e) {
60 var _a, _b;
61 (_b = (_a = _this.props).onMouseLeave) === null || _b === void 0 ? void 0 : _b.call(_a, _this.props, _this.props.path, e);
62 };
63 return _this;
64 }
65 /** @deprecated no longer necessary now that the TypeScript parser supports type arguments on JSX element tags */
66 TreeNode.ofType = function () {
67 return TreeNode;
68 };
69 TreeNode.prototype.render = function () {
70 var _a;
71 var _b = this.props, children = _b.children, className = _b.className, disabled = _b.disabled, icon = _b.icon, isExpanded = _b.isExpanded, isSelected = _b.isSelected, label = _b.label;
72 var classes = classNames(Classes.TREE_NODE, (_a = {},
73 _a[Classes.DISABLED] = disabled,
74 _a[Classes.TREE_NODE_SELECTED] = isSelected,
75 _a[Classes.TREE_NODE_EXPANDED] = isExpanded,
76 _a), className);
77 var contentClasses = classNames(Classes.TREE_NODE_CONTENT, "".concat(Classes.TREE_NODE_CONTENT, "-").concat(this.props.depth));
78 var eventHandlers = disabled === true
79 ? {}
80 : {
81 onClick: this.handleClick,
82 onContextMenu: this.handleContextMenu,
83 onDoubleClick: this.handleDoubleClick,
84 onMouseEnter: this.handleMouseEnter,
85 onMouseLeave: this.handleMouseLeave,
86 };
87 return (React.createElement("li", { className: classes },
88 React.createElement("div", __assign({ className: contentClasses, ref: this.handleContentRef }, eventHandlers),
89 this.maybeRenderCaret(),
90 React.createElement(Icon, { className: Classes.TREE_NODE_ICON, icon: icon, "aria-hidden": true, tabIndex: -1 }),
91 React.createElement("span", { className: Classes.TREE_NODE_LABEL }, label),
92 this.maybeRenderSecondaryLabel()),
93 React.createElement(Collapse, { isOpen: isExpanded }, children)));
94 };
95 TreeNode.prototype.maybeRenderCaret = function () {
96 var _a = this.props, children = _a.children, isExpanded = _a.isExpanded, disabled = _a.disabled, _b = _a.hasCaret, hasCaret = _b === void 0 ? React.Children.count(children) > 0 : _b;
97 if (hasCaret) {
98 var caretClasses = classNames(Classes.TREE_NODE_CARET, isExpanded ? Classes.TREE_NODE_CARET_OPEN : Classes.TREE_NODE_CARET_CLOSED);
99 return (React.createElement(ChevronRight, { title: isExpanded ? "Collapse group" : "Expand group", className: caretClasses, onClick: disabled === true ? undefined : this.handleCaretClick }));
100 }
101 return React.createElement("span", { className: Classes.TREE_NODE_CARET_NONE });
102 };
103 TreeNode.prototype.maybeRenderSecondaryLabel = function () {
104 if (this.props.secondaryLabel != null) {
105 return React.createElement("span", { className: Classes.TREE_NODE_SECONDARY_LABEL }, this.props.secondaryLabel);
106 }
107 else {
108 return undefined;
109 }
110 };
111 TreeNode.displayName = "".concat(DISPLAYNAME_PREFIX, ".TreeNode");
112 return TreeNode;
113}(React.Component));
114export { TreeNode };
115//# sourceMappingURL=treeNode.js.map
\No newline at end of file