1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 | var _react = _interopRequireDefault(require("react"));
|
8 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
9 | var _classnames = _interopRequireDefault(require("classnames"));
|
10 | var _reactPopper = require("react-popper");
|
11 | var _DropdownContext = require("./DropdownContext");
|
12 | var _utils = require("./utils");
|
13 | var _Button = _interopRequireDefault(require("./Button"));
|
14 | const _excluded = ["className", "color", "cssModule", "caret", "split", "nav", "tag", "innerRef"];
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 | function _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); }
|
17 | function _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; }
|
18 | function _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; }
|
19 | const propTypes = {
|
20 | caret: _propTypes.default.bool,
|
21 | color: _propTypes.default.string,
|
22 | children: _propTypes.default.node,
|
23 | className: _propTypes.default.string,
|
24 | cssModule: _propTypes.default.object,
|
25 | disabled: _propTypes.default.bool,
|
26 | onClick: _propTypes.default.func,
|
27 | 'aria-haspopup': _propTypes.default.bool,
|
28 | split: _propTypes.default.bool,
|
29 | tag: _utils.tagPropType,
|
30 | nav: _propTypes.default.bool,
|
31 | innerRef: _propTypes.default.oneOfType([_propTypes.default.object, _propTypes.default.string, _propTypes.default.func])
|
32 | };
|
33 | const defaultProps = {
|
34 | color: 'secondary',
|
35 | 'aria-haspopup': true
|
36 | };
|
37 | class DropdownToggle extends _react.default.Component {
|
38 | constructor(props) {
|
39 | super(props);
|
40 | this.onClick = this.onClick.bind(this);
|
41 | }
|
42 | onClick(e) {
|
43 | if (this.props.disabled || this.context.disabled) {
|
44 | e.preventDefault();
|
45 | return;
|
46 | }
|
47 | if (this.props.nav && !this.props.tag) {
|
48 | e.preventDefault();
|
49 | }
|
50 | if (this.props.onClick) {
|
51 | this.props.onClick(e);
|
52 | }
|
53 | this.context.toggle(e);
|
54 | }
|
55 | getRole() {
|
56 | return this.context.menuRole || this.props['aria-haspopup'];
|
57 | }
|
58 | render() {
|
59 | const _this$props = this.props,
|
60 | {
|
61 | className,
|
62 | color,
|
63 | cssModule,
|
64 | caret,
|
65 | split,
|
66 | nav,
|
67 | tag,
|
68 | innerRef
|
69 | } = _this$props,
|
70 | props = _objectWithoutProperties(_this$props, _excluded);
|
71 | const ariaLabel = props['aria-label'] || 'Toggle Dropdown';
|
72 | const classes = (0, _utils.mapToCssModules)((0, _classnames.default)(className, {
|
73 | 'dropdown-toggle': caret || split,
|
74 | 'dropdown-toggle-split': split,
|
75 | 'nav-link': nav
|
76 | }), cssModule);
|
77 | const children = typeof props.children !== 'undefined' ? props.children : _react.default.createElement("span", {
|
78 | className: "visually-hidden"
|
79 | }, ariaLabel);
|
80 | let Tag;
|
81 | if (nav && !tag) {
|
82 | Tag = 'a';
|
83 | props.href = '#';
|
84 | } else if (!tag) {
|
85 | Tag = _Button.default;
|
86 | props.color = color;
|
87 | props.cssModule = cssModule;
|
88 | } else {
|
89 | Tag = tag;
|
90 | }
|
91 |
|
92 |
|
93 | const returnFunction = ({
|
94 | ref
|
95 | }) => {
|
96 | const handleRef = tagRef => {
|
97 | ref(tagRef);
|
98 | const {
|
99 | onToggleRef
|
100 | } = this.context;
|
101 | if (onToggleRef) onToggleRef(tagRef);
|
102 | };
|
103 | return _react.default.createElement(Tag, _extends({}, props, {
|
104 | [typeof Tag === 'string' ? 'ref' : 'innerRef']: handleRef,
|
105 | className: classes,
|
106 | onClick: this.onClick,
|
107 | "aria-expanded": this.context.isOpen,
|
108 | "aria-haspopup": this.getRole(),
|
109 | children: children
|
110 | }));
|
111 | };
|
112 |
|
113 |
|
114 | if (this.context.inNavbar) {
|
115 | return _react.default.createElement(_react.default.Fragment, null, returnFunction({
|
116 | ref: this.context.onToggleRef
|
117 | }));
|
118 | }
|
119 |
|
120 |
|
121 | return _react.default.createElement(_reactPopper.Reference, {
|
122 | innerRef: innerRef
|
123 | }, returnFunction);
|
124 | }
|
125 | }
|
126 | DropdownToggle.propTypes = propTypes;
|
127 | DropdownToggle.defaultProps = defaultProps;
|
128 | DropdownToggle.contextType = _DropdownContext.DropdownContext;
|
129 | var _default = DropdownToggle;
|
130 | exports.default = _default; |
\ | No newline at end of file |