UNPKG

4.98 kBJavaScriptView Raw
1/*
2 * Copyright 2017 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 classNames from "classnames";
18import * as React from "react";
19import { AbstractPureComponent2, Classes, Keys, Utils, } from "../../common";
20import { Icon, IconSize } from "../icon/icon";
21import { Spinner } from "../spinner/spinner";
22var AbstractButton = /** @class */ (function (_super) {
23 __extends(AbstractButton, _super);
24 function AbstractButton() {
25 var _this = _super !== null && _super.apply(this, arguments) || this;
26 _this.state = {
27 isActive: false,
28 };
29 // we're casting as `any` to get around a somewhat opaque safeInvoke error
30 // that "Type argument candidate 'KeyboardEvent<T>' is not a valid type
31 // argument because it is not a supertype of candidate
32 // 'KeyboardEvent<HTMLElement>'."
33 _this.handleKeyDown = function (e) {
34 var _a, _b;
35 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
36 /* eslint-disable deprecation/deprecation */
37 if (Keys.isKeyboardClick(e.which)) {
38 e.preventDefault();
39 if (e.which !== _this.currentKeyDown) {
40 _this.setState({ isActive: true });
41 }
42 }
43 _this.currentKeyDown = e.which;
44 (_b = (_a = _this.props).onKeyDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
45 };
46 _this.handleKeyUp = function (e) {
47 var _a, _b, _c;
48 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
49 /* eslint-disable deprecation/deprecation */
50 if (Keys.isKeyboardClick(e.which)) {
51 _this.setState({ isActive: false });
52 (_a = _this.buttonRef) === null || _a === void 0 ? void 0 : _a.click();
53 }
54 _this.currentKeyDown = undefined;
55 (_c = (_b = _this.props).onKeyUp) === null || _c === void 0 ? void 0 : _c.call(_b, e);
56 };
57 _this.handleBlur = function (e) {
58 var _a, _b;
59 if (_this.state.isActive) {
60 _this.setState({ isActive: false });
61 }
62 (_b = (_a = _this.props).onBlur) === null || _b === void 0 ? void 0 : _b.call(_a, e);
63 };
64 return _this;
65 }
66 AbstractButton.prototype.getCommonButtonProps = function () {
67 var _a;
68 var _b = this.props, active = _b.active, alignText = _b.alignText, fill = _b.fill, large = _b.large, loading = _b.loading, outlined = _b.outlined, minimal = _b.minimal, small = _b.small, tabIndex = _b.tabIndex;
69 var disabled = this.props.disabled || loading;
70 var className = classNames(Classes.BUTTON, (_a = {},
71 _a[Classes.ACTIVE] = !disabled && (active || this.state.isActive),
72 _a[Classes.DISABLED] = disabled,
73 _a[Classes.FILL] = fill,
74 _a[Classes.LARGE] = large,
75 _a[Classes.LOADING] = loading,
76 _a[Classes.MINIMAL] = minimal,
77 _a[Classes.OUTLINED] = outlined,
78 _a[Classes.SMALL] = small,
79 _a), Classes.alignmentClass(alignText), Classes.intentClass(this.props.intent), this.props.className);
80 return {
81 className: className,
82 disabled: disabled,
83 onBlur: this.handleBlur,
84 onClick: disabled ? undefined : this.props.onClick,
85 onKeyDown: this.handleKeyDown,
86 onKeyUp: this.handleKeyUp,
87 tabIndex: disabled ? -1 : tabIndex,
88 };
89 };
90 AbstractButton.prototype.renderChildren = function () {
91 var _a = this.props, children = _a.children, icon = _a.icon, loading = _a.loading, rightIcon = _a.rightIcon, text = _a.text;
92 return [
93 loading && React.createElement(Spinner, { key: "loading", className: Classes.BUTTON_SPINNER, size: IconSize.LARGE }),
94 React.createElement(Icon, { key: "leftIcon", icon: icon }),
95 (!Utils.isReactNodeEmpty(text) || !Utils.isReactNodeEmpty(children)) && (React.createElement("span", { key: "text", className: Classes.BUTTON_TEXT },
96 text,
97 children)),
98 React.createElement(Icon, { key: "rightIcon", icon: rightIcon }),
99 ];
100 };
101 return AbstractButton;
102}(AbstractPureComponent2));
103export { AbstractButton };
104//# sourceMappingURL=abstractButton.js.map
\No newline at end of file