UNPKG

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