forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropdownStateMixin.js
47 lines (38 loc) · 1.02 KB
/
DropdownStateMixin.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
import React from './react-es6';
var DropdownStateMixin = {
getInitialState: function () {
return {
open: false
};
},
setDropdownState: function (newState, onStateChangeComplete) {
if (newState) {
this.bindRootCloseHandlers();
} else {
this.unbindRootCloseHandlers();
}
this.setState({
open: newState
}, onStateChangeComplete);
},
handleKeyUp: function (e) {
if (e.keyCode === 27) {
this.setDropdownState(false);
}
},
handleClickOutside: function () {
this.setDropdownState(false);
},
bindRootCloseHandlers: function () {
document.addEventListener('click', this.handleClickOutside);
document.addEventListener('keyup', this.handleKeyUp);
},
unbindRootCloseHandlers: function () {
document.removeEventListener('click', this.handleClickOutside);
document.removeEventListener('keyup', this.handleKeyUp);
},
componentWillUnmount: function () {
this.unbindRootCloseHandlers();
}
};
export default = DropdownStateMixin;