UNPKG

4.67 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || (function () {
2 var extendStatics = Object.setPrototypeOf ||
3 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
5 return function (d, b) {
6 extendStatics(d, b);
7 function __() { this.constructor = d; }
8 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
9 };
10})();
11import { MalibuIcon } from '@heroku/react-malibu';
12import classnames from 'classnames';
13import * as React from 'react';
14import OutsideClickHandler from 'react-outside-click-handler';
15import { Manager, Popper, Reference } from 'react-popper';
16import { default as HKButton, Type } from './HKButton';
17export var Align;
18(function (Align) {
19 Align["Left"] = "left";
20 Align["Right"] = "right";
21})(Align || (Align = {}));
22var HKDropdown = /** @class */ (function (_super) {
23 __extends(HKDropdown, _super);
24 function HKDropdown() {
25 var _this = _super !== null && _super.apply(this, arguments) || this;
26 _this.state = {
27 showDropdown: false,
28 };
29 _this.handleDropdown = function () {
30 _this.setState(function (prevState) { return ({ showDropdown: !prevState.showDropdown }); });
31 };
32 _this.testId = function () {
33 var name = _this.props.name;
34 return name + "-dropdown-button";
35 };
36 _this.handleContentClick = function () {
37 return _this.props.closeOnClick && _this.setState({ showDropdown: false });
38 };
39 _this.handleClickOutside = function (e) {
40 // When closing by clicking on the menu button again,
41 // both this handler and handleDropdown will fire.
42 // Make sure we noop in that scenario so that the dropdown actually closes.
43 var path = e.path || (e.composedPath && e.composedPath());
44 if (!path) {
45 _this.setState({
46 showDropdown: false,
47 });
48 return;
49 }
50 var eventNodes = path.filter(function (node) {
51 return node.nodeType === 1;
52 });
53 var didClickButton = eventNodes.some(function (node) {
54 return (node.hasAttribute('data-testid') &&
55 node.getAttribute('data-testid') === _this.testId());
56 });
57 if (!didClickButton) {
58 _this.setState({
59 showDropdown: false,
60 });
61 }
62 };
63 return _this;
64 }
65 HKDropdown.prototype.render = function () {
66 var _this = this;
67 var _a = this.props, align = _a.align, children = _a.children, className = _a.className, contentClassName = _a.contentClassName, disabled = _a.disabled, name = _a.name, title = _a.title;
68 var showDropdown = this.state.showDropdown;
69 var popperPlacement = align === Align.Right ? 'bottom-end' : 'bottom-start';
70 return (React.createElement(Manager, null,
71 React.createElement(Reference, null, function (_a) {
72 var ref = _a.ref;
73 return (React.createElement("div", { className: 'relative dib', ref: ref },
74 React.createElement(HKButton, { onClick: _this.handleDropdown, "data-testid": _this.testId(), className: classnames({ ph1: !title }, className), type: Type.Secondary, disabled: disabled },
75 title,
76 React.createElement(MalibuIcon, { key: 'icon', name: 'caret-16', size: 16, fillClass: 'fill-purple', extraClasses: classnames({ pl1: title }) }))));
77 }),
78 showDropdown && (React.createElement(OutsideClickHandler, { onOutsideClick: this.handleClickOutside },
79 React.createElement(Popper, { placement: popperPlacement }, function (_a) {
80 var ref = _a.ref, style = _a.style, placement = _a.placement;
81 return (React.createElement("div", { className: 'z-max', onClick: _this.handleContentClick, "data-testid": name + "-dropdown-content", ref: ref, style: style, "data-placement": placement },
82 React.createElement("ul", { className: classnames(contentClassName, 'list br1 pl0 pv1 mv1 shadow-outer-2 bg-white') }, children)));
83 })))));
84 };
85 HKDropdown.displayName = 'HKDropdown';
86 HKDropdown.defaultProps = {
87 closeOnClick: true,
88 disabled: false,
89 name: 'hkdropdown',
90 };
91 return HKDropdown;
92}(React.Component));
93export default HKDropdown;
94//# sourceMappingURL=HKDropdown.js.map
\No newline at end of file