UNPKG

3.15 kBJavaScriptView Raw
1import _objectWithoutPropertiesLoose from "@babel/runtime-corejs2/helpers/esm/objectWithoutPropertiesLoose";
2import _inheritsLoose from "@babel/runtime-corejs2/helpers/esm/inheritsLoose";
3import _extends from "@babel/runtime-corejs2/helpers/esm/extends";
4import classNames from 'classnames';
5import React from 'react';
6import PropTypes from 'prop-types';
7import Dropdown from './Dropdown';
8import splitComponentProps from './utils/splitComponentProps';
9import ValidComponentChildren from './utils/ValidComponentChildren';
10
11var propTypes = _extends({}, Dropdown.propTypes, {
12 // Toggle props.
13 title: PropTypes.node.isRequired,
14 noCaret: PropTypes.bool,
15 active: PropTypes.bool,
16 activeKey: PropTypes.any,
17 activeHref: PropTypes.string,
18 // Override generated docs from <Dropdown>.
19
20 /**
21 * @private
22 */
23 children: PropTypes.node
24});
25
26var NavDropdown =
27/*#__PURE__*/
28function (_React$Component) {
29 _inheritsLoose(NavDropdown, _React$Component);
30
31 function NavDropdown() {
32 return _React$Component.apply(this, arguments) || this;
33 }
34
35 var _proto = NavDropdown.prototype;
36
37 _proto.isActive = function isActive(_ref, activeKey, activeHref) {
38 var _this = this;
39
40 var props = _ref.props;
41
42 if (props.active || activeKey != null && props.eventKey === activeKey || activeHref && props.href === activeHref) {
43 return true;
44 }
45
46 if (ValidComponentChildren.some(props.children, function (child) {
47 return _this.isActive(child, activeKey, activeHref);
48 })) {
49 return true;
50 }
51
52 return props.active;
53 };
54
55 _proto.render = function render() {
56 var _this2 = this;
57
58 var _this$props = this.props,
59 title = _this$props.title,
60 activeKey = _this$props.activeKey,
61 activeHref = _this$props.activeHref,
62 className = _this$props.className,
63 style = _this$props.style,
64 children = _this$props.children,
65 props = _objectWithoutPropertiesLoose(_this$props, ["title", "activeKey", "activeHref", "className", "style", "children"]);
66
67 var active = this.isActive(this, activeKey, activeHref);
68 delete props.active; // Accessed via this.isActive().
69
70 delete props.eventKey; // Accessed via this.isActive().
71
72 var _splitComponentProps = splitComponentProps(props, Dropdown.ControlledComponent),
73 dropdownProps = _splitComponentProps[0],
74 toggleProps = _splitComponentProps[1]; // Unlike for the other dropdowns, styling needs to go to the `<Dropdown>`
75 // rather than the `<Dropdown.Toggle>`.
76
77
78 return React.createElement(Dropdown, _extends({}, dropdownProps, {
79 componentClass: "li",
80 className: classNames(className, {
81 active: active
82 }),
83 style: style
84 }), React.createElement(Dropdown.Toggle, _extends({}, toggleProps, {
85 useAnchor: true
86 }), title), React.createElement(Dropdown.Menu, null, ValidComponentChildren.map(children, function (child) {
87 return React.cloneElement(child, {
88 active: _this2.isActive(child, activeKey, activeHref)
89 });
90 })));
91 };
92
93 return NavDropdown;
94}(React.Component);
95
96NavDropdown.propTypes = propTypes;
97export default NavDropdown;
\No newline at end of file