-
Notifications
You must be signed in to change notification settings - Fork 54
/
NavDropdown.js
95 lines (80 loc) · 2.17 KB
/
NavDropdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import Dropdown from './Dropdown';
import splitComponentProps from './utils/splitComponentProps';
import ValidComponentChildren from './utils/ValidComponentChildren';
const propTypes = {
...Dropdown.propTypes,
// Toggle props.
title: PropTypes.node.isRequired,
noCaret: PropTypes.bool,
active: PropTypes.bool,
activeKey: PropTypes.any,
activeHref: PropTypes.string,
// Override generated docs from <Dropdown>.
/**
* @private
*/
children: PropTypes.node
};
class NavDropdown extends React.Component {
isActive({ props }, activeKey, activeHref) {
if (
props.active ||
(activeKey != null && props.eventKey === activeKey) ||
(activeHref && props.href === activeHref)
) {
return true;
}
if (
ValidComponentChildren.some(props.children, child =>
this.isActive(child, activeKey, activeHref)
)
) {
return true;
}
return props.active;
}
render() {
const {
title,
activeKey,
activeHref,
className,
style,
children,
...props
} = this.props;
const active = this.isActive(this, activeKey, activeHref);
delete props.active; // Accessed via this.isActive().
delete props.eventKey; // Accessed via this.isActive().
const [dropdownProps, toggleProps] = splitComponentProps(
props,
Dropdown.ControlledComponent
);
// Unlike for the other dropdowns, styling needs to go to the `<Dropdown>`
// rather than the `<Dropdown.Toggle>`.
return (
<Dropdown
{...dropdownProps}
componentClass="li"
className={classNames(className, { active })}
style={style}
>
<Dropdown.Toggle {...toggleProps} useAnchor>
{title}
</Dropdown.Toggle>
<Dropdown.Menu>
{ValidComponentChildren.map(children, child =>
React.cloneElement(child, {
active: this.isActive(child, activeKey, activeHref)
})
)}
</Dropdown.Menu>
</Dropdown>
);
}
}
NavDropdown.propTypes = propTypes;
export default NavDropdown;