UNPKG

5.56 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2017 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.AbstractButton = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var common_1 = require("../../common");
23var icon_1 = require("../icon/icon");
24var spinner_1 = require("../spinner/spinner");
25var AbstractButton = /** @class */ (function (_super) {
26 tslib_1.__extends(AbstractButton, _super);
27 function AbstractButton() {
28 var _this = _super !== null && _super.apply(this, arguments) || this;
29 _this.state = {
30 isActive: false,
31 };
32 // we're casting as `any` to get around a somewhat opaque safeInvoke error
33 // that "Type argument candidate 'KeyboardEvent<T>' is not a valid type
34 // argument because it is not a supertype of candidate
35 // 'KeyboardEvent<HTMLElement>'."
36 _this.handleKeyDown = function (e) {
37 var _a, _b;
38 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
39 /* eslint-disable deprecation/deprecation */
40 if (common_1.Keys.isKeyboardClick(e.which)) {
41 e.preventDefault();
42 if (e.which !== _this.currentKeyDown) {
43 _this.setState({ isActive: true });
44 }
45 }
46 _this.currentKeyDown = e.which;
47 (_b = (_a = _this.props).onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
48 };
49 _this.handleKeyUp = function (e) {
50 var _a, _b, _c;
51 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
52 /* eslint-disable deprecation/deprecation */
53 if (common_1.Keys.isKeyboardClick(e.which)) {
54 _this.setState({ isActive: false });
55 (_a = _this.buttonRef) === null || _a === void 0 ? void 0 : _a.click();
56 }
57 _this.currentKeyDown = undefined;
58 (_c = (_b = _this.props).onKeyUp) === null || _c === void 0 ? void 0 : _c.call(_b, e);
59 };
60 _this.handleBlur = function (e) {
61 var _a, _b;
62 if (_this.state.isActive) {
63 _this.setState({ isActive: false });
64 }
65 (_b = (_a = _this.props).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, e);
66 };
67 return _this;
68 }
69 AbstractButton.prototype.getCommonButtonProps = function () {
70 var _a;
71 var _b = this.props, _c = _b.active, active = _c === void 0 ? false : _c, alignText = _b.alignText, fill = _b.fill, large = _b.large, _d = _b.loading, loading = _d === void 0 ? false : _d, outlined = _b.outlined, minimal = _b.minimal, small = _b.small, tabIndex = _b.tabIndex;
72 var disabled = this.props.disabled || loading;
73 var className = (0, classnames_1.default)(common_1.Classes.BUTTON, (_a = {},
74 _a[common_1.Classes.ACTIVE] = !disabled && (active || this.state.isActive),
75 _a[common_1.Classes.DISABLED] = disabled,
76 _a[common_1.Classes.FILL] = fill,
77 _a[common_1.Classes.LARGE] = large,
78 _a[common_1.Classes.LOADING] = loading,
79 _a[common_1.Classes.MINIMAL] = minimal,
80 _a[common_1.Classes.OUTLINED] = outlined,
81 _a[common_1.Classes.SMALL] = small,
82 _a), common_1.Classes.alignmentClass(alignText), common_1.Classes.intentClass(this.props.intent), this.props.className);
83 return {
84 className: className,
85 disabled: disabled,
86 onBlur: this.handleBlur,
87 onClick: disabled ? undefined : this.props.onClick,
88 onKeyDown: this.handleKeyDown,
89 onKeyUp: this.handleKeyUp,
90 tabIndex: disabled ? -1 : tabIndex,
91 };
92 };
93 AbstractButton.prototype.renderChildren = function () {
94 var _a = this.props, children = _a.children, icon = _a.icon, loading = _a.loading, rightIcon = _a.rightIcon, text = _a.text;
95 var maybeHasText = !common_1.Utils.isReactNodeEmpty(text) || !common_1.Utils.isReactNodeEmpty(children);
96 return [
97 loading && React.createElement(spinner_1.Spinner, { key: "loading", className: common_1.Classes.BUTTON_SPINNER, size: icon_1.IconSize.LARGE }),
98 // The icon is purely decorative if text is provided
99 React.createElement(icon_1.Icon, { key: "leftIcon", icon: icon, "aria-hidden": maybeHasText, tabIndex: maybeHasText ? -1 : undefined }),
100 maybeHasText && (React.createElement("span", { key: "text", className: common_1.Classes.BUTTON_TEXT },
101 text,
102 children)),
103 React.createElement(icon_1.Icon, { key: "rightIcon", icon: rightIcon }),
104 ];
105 };
106 return AbstractButton;
107}(common_1.AbstractPureComponent2));
108exports.AbstractButton = AbstractButton;
109//# sourceMappingURL=abstractButton.js.map
\No newline at end of file