1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 |
|
5 | var core_1 = require("@emotion/core");
|
6 | var react_1 = tslib_1.__importDefault(require("react"));
|
7 | var memoize_one_1 = tslib_1.__importDefault(require("memoize-one"));
|
8 | var analytics_next_1 = require("@kalamazoo/analytics-next");
|
9 | var version_json_1 = require("../version.json");
|
10 | var components_1 = tslib_1.__importDefault(require("@kalamazoo/theme/components"));
|
11 | var theme_1 = require("../theme");
|
12 | var utils_1 = require("./utils");
|
13 | var Content_1 = tslib_1.__importDefault(require("./Content"));
|
14 | var InnerWrapper_1 = tslib_1.__importDefault(require("./InnerWrapper"));
|
15 | var IconWrapper_1 = tslib_1.__importDefault(require("./IconWrapper"));
|
16 | var LoadingSpinner_1 = tslib_1.__importDefault(require("./LoadingSpinner"));
|
17 | var Button = (function (_super) {
|
18 | tslib_1.__extends(Button, _super);
|
19 | function Button() {
|
20 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
21 |
|
22 | _this.button = react_1.default.createRef();
|
23 |
|
24 | _this.getComposedRefs = memoize_one_1.default(utils_1.composeRefs);
|
25 | _this.state = {
|
26 | isActive: false,
|
27 | isFocus: false,
|
28 | isHover: false,
|
29 | };
|
30 | _this.isInteractive = function () { return !_this.props.isDisabled && !_this.props.isLoading; };
|
31 | _this.onMouseEnter = function (e) {
|
32 | _this.setState({ isHover: true });
|
33 | if (_this.props.onMouseEnter) {
|
34 | _this.props.onMouseEnter(e);
|
35 | }
|
36 | };
|
37 | _this.onMouseLeave = function (e) {
|
38 | _this.setState({ isHover: false, isActive: false });
|
39 | if (_this.props.onMouseLeave) {
|
40 | _this.props.onMouseLeave(e);
|
41 | }
|
42 | };
|
43 | _this.onMouseDown = function (e) {
|
44 | e.preventDefault();
|
45 | _this.setState({ isActive: true });
|
46 | if (_this.props.onMouseDown) {
|
47 | _this.props.onMouseDown(e);
|
48 | }
|
49 | };
|
50 | _this.onMouseUp = function (e) {
|
51 | _this.setState({ isActive: false });
|
52 | if (_this.props.onMouseUp) {
|
53 | _this.props.onMouseUp(e);
|
54 | }
|
55 | };
|
56 | _this.onFocus = function (event) {
|
57 | _this.setState({ isFocus: true });
|
58 | if (_this.props.onFocus) {
|
59 | _this.props.onFocus(event);
|
60 | }
|
61 | };
|
62 | _this.onBlur = function (event) {
|
63 | _this.setState({ isFocus: false });
|
64 | if (_this.props.onBlur) {
|
65 | _this.props.onBlur(event);
|
66 | }
|
67 | };
|
68 | _this.getElement = function () {
|
69 | var _a = _this.props, href = _a.href, isDisabled = _a.isDisabled;
|
70 | if (href) {
|
71 | return isDisabled ? 'span' : 'a';
|
72 | }
|
73 | return 'button';
|
74 | };
|
75 |
|
76 |
|
77 | _this.onInnerClick = function (e) {
|
78 | if (!_this.isInteractive()) {
|
79 | e.stopPropagation();
|
80 | }
|
81 | return true;
|
82 | };
|
83 | return _this;
|
84 | }
|
85 | Button.prototype.componentDidMount = function () {
|
86 | if (this.props.autoFocus && this.button instanceof HTMLButtonElement) {
|
87 | this.button.focus();
|
88 | }
|
89 | };
|
90 | Button.prototype.render = function () {
|
91 | var _this = this;
|
92 | var _a = this.props, _b = _a.appearance, appearance = _b === void 0 ? 'default' : _b, children = _a.children, className = _a.className, CustomComponent = _a.component, consumerRef = _a.consumerRef, iconAfter = _a.iconAfter, iconBefore = _a.iconBefore, _c = _a.isDisabled, isDisabled = _c === void 0 ? false : _c, _d = _a.isLoading, isLoading = _d === void 0 ? false : _d, _e = _a.isSelected, isSelected = _e === void 0 ? false : _e, _f = _a.shouldFitContainer, shouldFitContainer = _f === void 0 ? false : _f, _g = _a.spacing, spacing = _g === void 0 ? 'default' : _g, _h = _a.theme, theme = _h === void 0 ? function (current, props) { return current(props); } : _h, testId = _a.testId, rest = tslib_1.__rest(_a, ["appearance", "children", "className", "component", "consumerRef", "iconAfter", "iconBefore", "isDisabled", "isLoading", "isSelected", "shouldFitContainer", "spacing", "theme", "testId"]);
|
93 | var attributes = tslib_1.__assign(tslib_1.__assign({}, this.state), { isSelected: isSelected, isDisabled: isDisabled });
|
94 | var StyledButton = CustomComponent || this.getElement();
|
95 | var iconIsOnlyChild = !!((iconBefore && !iconAfter && !children) ||
|
96 | (iconAfter && !iconBefore && !children));
|
97 | var specifiers = function (styles) {
|
98 | if (StyledButton === 'a') {
|
99 | return {
|
100 | 'a&': styles,
|
101 | };
|
102 | }
|
103 | else if (StyledButton === CustomComponent) {
|
104 | return {
|
105 | '&, a&, &:hover, &:active, &:focus': styles,
|
106 | };
|
107 | }
|
108 | return styles;
|
109 | };
|
110 | return (core_1.jsx(theme_1.Theme.Provider, { value: theme },
|
111 | core_1.jsx(components_1.default.Consumer, null, function (_a) {
|
112 | var mode = _a.mode;
|
113 | return (core_1.jsx(theme_1.Theme.Consumer, tslib_1.__assign({ mode: mode, state: utils_1.mapAttributesToState(attributes), iconIsOnlyChild: iconIsOnlyChild }, _this.props), function (_a) {
|
114 | var buttonStyles = _a.buttonStyles, spinnerStyles = _a.spinnerStyles;
|
115 | return (core_1.jsx(StyledButton, tslib_1.__assign({}, utils_1.filterProps(rest, StyledButton), { "data-testid": testId, ref: _this.getComposedRefs(_this.button, consumerRef), onMouseEnter: _this.onMouseEnter, onMouseLeave: _this.onMouseLeave, onMouseDown: _this.onMouseDown, onMouseUp: _this.onMouseUp, onFocus: _this.onFocus, onBlur: _this.onBlur, disabled: isDisabled, className: className, css: specifiers(buttonStyles) }),
|
116 | core_1.jsx(InnerWrapper_1.default, { onClick: _this.onInnerClick, fit: !!shouldFitContainer },
|
117 | isLoading && (core_1.jsx(LoadingSpinner_1.default, { spacing: spacing, appearance: appearance, isSelected: isSelected, isDisabled: isDisabled, styles: spinnerStyles })),
|
118 | iconBefore && (core_1.jsx(IconWrapper_1.default, { isLoading: isLoading, spacing: spacing, isOnlyChild: iconIsOnlyChild, icon: iconBefore })),
|
119 | children && (core_1.jsx(Content_1.default, { isLoading: isLoading, followsIcon: !!iconBefore, spacing: spacing }, children)),
|
120 | iconAfter && (core_1.jsx(IconWrapper_1.default, { isLoading: isLoading, spacing: spacing, isOnlyChild: iconIsOnlyChild, icon: iconAfter })))));
|
121 | }));
|
122 | })));
|
123 | };
|
124 | Button.defaultProps = {
|
125 | appearance: 'default',
|
126 | autoFocus: false,
|
127 | isDisabled: false,
|
128 | isLoading: false,
|
129 | isSelected: false,
|
130 | shouldFitContainer: false,
|
131 | spacing: 'default',
|
132 | type: 'button',
|
133 | };
|
134 | return Button;
|
135 | }(react_1.default.Component));
|
136 | exports.Button = Button;
|
137 | var createAndFireEventOnAtlaskit = analytics_next_1.createAndFireEvent('atlaskit');
|
138 | var ButtonWithRef = react_1.default.forwardRef(function (props, ref) { return core_1.jsx(Button, tslib_1.__assign({}, props, { consumerRef: ref })); });
|
139 | ButtonWithRef.displayName = 'Button';
|
140 | exports.default = analytics_next_1.withAnalyticsContext({
|
141 | componentName: 'button',
|
142 | packageName: version_json_1.name,
|
143 | packageVersion: version_json_1.version,
|
144 | })(analytics_next_1.withAnalyticsEvents({
|
145 | onClick: createAndFireEventOnAtlaskit({
|
146 | action: 'clicked',
|
147 | actionSubject: 'button',
|
148 | attributes: {
|
149 | componentName: 'button',
|
150 | packageName: version_json_1.name,
|
151 | packageVersion: version_json_1.version,
|
152 | },
|
153 | }),
|
154 | })(ButtonWithRef));
|
155 |
|
\ | No newline at end of file |