react-bootstrap
Version:
Bootstrap 3 components build with React
47 lines (38 loc) • 1.04 kB
JavaScript
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;