UNPKG

8.76 kBJavaScriptView Raw
1function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2var _excluded = ["className", "color", "cssModule", "caret", "split", "nav", "tag", "innerRef"];
3function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
4function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
6function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
9function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
10function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
11function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
12function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
13function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
14function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
15function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
16function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
17import React from 'react';
18import PropTypes from 'prop-types';
19import classNames from 'classnames';
20import { Reference } from 'react-popper';
21import { DropdownContext } from './DropdownContext';
22import { mapToCssModules, tagPropType } from './utils';
23import Button from './Button';
24var propTypes = {
25 caret: PropTypes.bool,
26 color: PropTypes.string,
27 children: PropTypes.node,
28 className: PropTypes.string,
29 cssModule: PropTypes.object,
30 disabled: PropTypes.bool,
31 onClick: PropTypes.func,
32 'aria-haspopup': PropTypes.bool,
33 split: PropTypes.bool,
34 tag: tagPropType,
35 nav: PropTypes.bool,
36 innerRef: PropTypes.oneOfType([PropTypes.object, PropTypes.string, PropTypes.func])
37};
38var defaultProps = {
39 color: 'secondary',
40 'aria-haspopup': true
41};
42var DropdownToggle = /*#__PURE__*/function (_React$Component) {
43 _inherits(DropdownToggle, _React$Component);
44 var _super = _createSuper(DropdownToggle);
45 function DropdownToggle(props) {
46 var _this;
47 _classCallCheck(this, DropdownToggle);
48 _this = _super.call(this, props);
49 _this.onClick = _this.onClick.bind(_assertThisInitialized(_this));
50 return _this;
51 }
52 _createClass(DropdownToggle, [{
53 key: "onClick",
54 value: function onClick(e) {
55 if (this.props.disabled || this.context.disabled) {
56 e.preventDefault();
57 return;
58 }
59 if (this.props.nav && !this.props.tag) {
60 e.preventDefault();
61 }
62 if (this.props.onClick) {
63 this.props.onClick(e);
64 }
65 this.context.toggle(e);
66 }
67 }, {
68 key: "getRole",
69 value: function getRole() {
70 return this.context.menuRole || this.props['aria-haspopup'];
71 }
72 }, {
73 key: "render",
74 value: function render() {
75 var _this2 = this;
76 var _this$props = this.props,
77 className = _this$props.className,
78 color = _this$props.color,
79 cssModule = _this$props.cssModule,
80 caret = _this$props.caret,
81 split = _this$props.split,
82 nav = _this$props.nav,
83 tag = _this$props.tag,
84 innerRef = _this$props.innerRef,
85 props = _objectWithoutProperties(_this$props, _excluded);
86 var ariaLabel = props['aria-label'] || 'Toggle Dropdown';
87 var classes = mapToCssModules(classNames(className, {
88 'dropdown-toggle': caret || split,
89 'dropdown-toggle-split': split,
90 'nav-link': nav
91 }), cssModule);
92 var children = typeof props.children !== 'undefined' ? props.children : /*#__PURE__*/React.createElement("span", {
93 className: "visually-hidden"
94 }, ariaLabel);
95 var Tag;
96 if (nav && !tag) {
97 Tag = 'a';
98 props.href = '#';
99 } else if (!tag) {
100 Tag = Button;
101 props.color = color;
102 props.cssModule = cssModule;
103 } else {
104 Tag = tag;
105 }
106
107 // extracted the rendering of the Tag component
108 var returnFunction = function returnFunction(_ref) {
109 var ref = _ref.ref;
110 var handleRef = function handleRef(tagRef) {
111 ref(tagRef);
112 var onToggleRef = _this2.context.onToggleRef;
113 if (onToggleRef) onToggleRef(tagRef);
114 };
115 return /*#__PURE__*/React.createElement(Tag, _extends({}, props, _defineProperty({}, typeof Tag === 'string' ? 'ref' : 'innerRef', handleRef), {
116 className: classes,
117 onClick: _this2.onClick,
118 "aria-expanded": _this2.context.isOpen,
119 "aria-haspopup": _this2.getRole(),
120 children: children
121 }));
122 };
123
124 // No Reference component if the component is in Navbar
125 if (this.context.inNavbar) {
126 return /*#__PURE__*/React.createElement(React.Fragment, null, returnFunction({
127 ref: this.context.onToggleRef
128 }));
129 }
130
131 // Normal rendering if component not in NavBar
132 return /*#__PURE__*/React.createElement(Reference, {
133 innerRef: innerRef
134 }, returnFunction);
135 }
136 }]);
137 return DropdownToggle;
138}(React.Component);
139DropdownToggle.propTypes = propTypes;
140DropdownToggle.defaultProps = defaultProps;
141DropdownToggle.contextType = DropdownContext;
142export default DropdownToggle;
\No newline at end of file