-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathDropdown.js
More file actions
201 lines (174 loc) · 4.31 KB
/
Dropdown.js
File metadata and controls
201 lines (174 loc) · 4.31 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
import classNames from "classnames";
import { Manager } from "react-popper";
import omit from "lodash.omit";
import { DropdownContext } from "./DropdownContext";
import { KEYCODES, EVENTS } from "./../constants";
/**
* You can use dropdowns to display accessible contextual overlays for displaying lists of links and more.
*/
class Dropdown extends React.Component {
constructor(props) {
super(props);
this.handleListeners = this.handleListeners.bind(this);
this.addListeners = this.addListeners.bind(this);
this.removeListeners = this.removeListeners.bind(this);
this.handleDocumentClick = this.handleDocumentClick.bind(this);
this.getContainer = this.getContainer.bind(this);
this.toggle = this.toggle.bind(this);
}
componentDidMount() {
this.handleListeners();
}
componentWillUnmount() {
this.removeListeners();
}
componentDidUpdate(prevProps) {
if (this.props.open !== prevProps.open) {
this.handleListeners();
}
}
handleListeners() {
if (this.props.open) {
this.addListeners();
return;
}
this.removeListeners();
}
addListeners() {
EVENTS.CLICK.forEach(e =>
document.addEventListener(e, this.handleDocumentClick, true)
);
}
removeListeners() {
EVENTS.CLICK.forEach(e =>
document.removeEventListener(e, this.handleDocumentClick, true)
);
}
getContainer() {
return ReactDOM.findDOMNode(this); // eslint-disable-line react/no-find-dom-node
}
handleDocumentClick(e) {
if (
e &&
(e.which === 3 || (e.type === "keyup" && e.which !== KEYCODES.TAB))
)
return;
const container = this.getContainer();
if (
container.contains(e.target) &&
container !== e.target &&
(e.type !== "keyup" || e.which === KEYCODES.TAB)
) {
return;
}
this.toggle(e);
}
toggle(e) {
if (this.props.disabled) {
return e && e.preventDefault();
}
return this.props.toggle(e);
}
render() {
const props = omit(this.props, [
"toggle",
"disabled",
"inNavbar",
"direction"
]);
const {
className,
children,
dropup,
open,
group,
size,
nav,
setActiveFromChild,
active,
addonType,
...attrs
} = props;
const direction =
this.props.direction === "down" && dropup ? "up" : this.props.direction;
attrs.tag = attrs.tag || (nav ? "li" : "div");
let subItemIsActive = false;
if (setActiveFromChild) {
React.Children.map(
this.props.children[1].props.children,
dropdownItem => {
if (dropdownItem && dropdownItem.props.active) subItemIsActive = true;
}
);
}
const classes = classNames(
className,
direction !== "down" && `drop${direction}`,
nav && active && "active",
setActiveFromChild && subItemIsActive && "active",
addonType && `input-group-${addonType}`,
group && "btn-group",
!!size && `btn-group-${size}`,
!group && !addonType && "dropdown",
open && "show",
nav && "nav-item"
);
const toggle = this.toggle;
return (
<DropdownContext.Provider value={{ toggle, open, direction, nav }}>
<Manager {...attrs}>
<DropdownContext.Consumer>
{() => <div className={classes}>{children}</div>}
</DropdownContext.Consumer>
</Manager>
</DropdownContext.Provider>
);
}
}
Dropdown.propTypes = {
/**
* The class name.
*/
className: PropTypes.string,
/**
* Whether it is open, or not.
*/
open: PropTypes.bool,
/**
* Whether it is disabled, or not.
*/
disabled: PropTypes.bool,
/**
* The toggle function.
*/
toggle: PropTypes.func,
/**
* Whether it is located inside a navbar, or not.
*/
inNavbar: PropTypes.bool,
/**
* Whether it is a drop-up, or not.
*/
dropup: PropTypes.bool,
/**
* The component's tag type.
*/
tag: PropTypes.string,
/**
* Whether it is located inside a Nav, or not.
*/
nav: PropTypes.bool,
/**
* The direction.
*/
direction: PropTypes.oneOf(["up", "down", "left", "right"])
};
Dropdown.defaultProps = {
open: false,
direction: "down",
nav: false
};
export default Dropdown;