UNPKG

3.56 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
4import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
5import React from 'react';
6import PropTypes from 'prop-types';
7import classNames from 'classnames';
8import { Reference } from 'react-popper';
9import { DropdownContext } from './DropdownContext';
10import { mapToCssModules, tagPropType } from './utils';
11import Button from './Button';
12var propTypes = {
13 caret: PropTypes.bool,
14 color: PropTypes.string,
15 children: PropTypes.node,
16 className: PropTypes.string,
17 cssModule: PropTypes.object,
18 disabled: PropTypes.bool,
19 onClick: PropTypes.func,
20 'aria-haspopup': PropTypes.bool,
21 split: PropTypes.bool,
22 tag: tagPropType,
23 nav: PropTypes.bool
24};
25var defaultProps = {
26 'aria-haspopup': true,
27 color: 'secondary'
28};
29
30var DropdownToggle =
31/*#__PURE__*/
32function (_React$Component) {
33 _inheritsLoose(DropdownToggle, _React$Component);
34
35 function DropdownToggle(props) {
36 var _this;
37
38 _this = _React$Component.call(this, props) || this;
39 _this.onClick = _this.onClick.bind(_assertThisInitialized(_this));
40 return _this;
41 }
42
43 var _proto = DropdownToggle.prototype;
44
45 _proto.onClick = function onClick(e) {
46 if (this.props.disabled) {
47 e.preventDefault();
48 return;
49 }
50
51 if (this.props.nav && !this.props.tag) {
52 e.preventDefault();
53 }
54
55 if (this.props.onClick) {
56 this.props.onClick(e);
57 }
58
59 this.context.toggle(e);
60 };
61
62 _proto.render = function render() {
63 var _this2 = this;
64
65 var _this$props = this.props,
66 className = _this$props.className,
67 color = _this$props.color,
68 cssModule = _this$props.cssModule,
69 caret = _this$props.caret,
70 split = _this$props.split,
71 nav = _this$props.nav,
72 tag = _this$props.tag,
73 props = _objectWithoutPropertiesLoose(_this$props, ["className", "color", "cssModule", "caret", "split", "nav", "tag"]);
74
75 var ariaLabel = props['aria-label'] || 'Toggle Dropdown';
76 var classes = mapToCssModules(classNames(className, {
77 'dropdown-toggle': caret || split,
78 'dropdown-toggle-split': split,
79 'nav-link': nav
80 }), cssModule);
81 var children = props.children || React.createElement("span", {
82 className: "sr-only"
83 }, ariaLabel);
84 var Tag;
85
86 if (nav && !tag) {
87 Tag = 'a';
88 props.href = '#';
89 } else if (!tag) {
90 Tag = Button;
91 props.color = color;
92 props.cssModule = cssModule;
93 } else {
94 Tag = tag;
95 }
96
97 if (this.context.inNavbar) {
98 return React.createElement(Tag, _extends({}, props, {
99 className: classes,
100 onClick: this.onClick,
101 "aria-expanded": this.context.isOpen,
102 children: children
103 }));
104 }
105
106 return React.createElement(Reference, null, function (_ref) {
107 var _ref2;
108
109 var ref = _ref.ref;
110 return React.createElement(Tag, _extends({}, props, (_ref2 = {}, _ref2[typeof Tag === 'string' ? 'ref' : 'innerRef'] = ref, _ref2), {
111 className: classes,
112 onClick: _this2.onClick,
113 "aria-expanded": _this2.context.isOpen,
114 children: children
115 }));
116 });
117 };
118
119 return DropdownToggle;
120}(React.Component);
121
122DropdownToggle.propTypes = propTypes;
123DropdownToggle.defaultProps = defaultProps;
124DropdownToggle.contextType = DropdownContext;
125export default DropdownToggle;
\No newline at end of file