UNPKG

react-bootstrap

Version:

Bootstrap 3 components build with React

80 lines (67 loc) 1.71 kB
var React = require('react'); var EventListener = require('./utils/EventListener'); /** * Checks whether a node is within * a root nodes tree * * @param {DOMElement} node * @param {DOMElement} root * @returns {boolean} */ function isNodeInRoot(node, root) { while (node) { if (node === root) { return true; } node = node.parentNode; } return false; } var DropdownStateMixin = { getInitialState: function () { return { open: false }; }, setDropdownState: function (newState, onStateChangeComplete) { if (newState) { this.bindRootCloseHandlers(); } else { this.unbindRootCloseHandlers(); } this.setState({ open: newState }, onStateChangeComplete); }, handleDocumentKeyUp: function (e) { if (e.keyCode === 27) { this.setDropdownState(false); } }, handleDocumentClick: function (e) { // If the click originated from within this component // don't do anything. if (isNodeInRoot(e.target, this.getDOMNode())) { return; } this.setDropdownState(false); }, bindRootCloseHandlers: function () { this._onDocumentClickListener = EventListener.listen(document, 'click', this.handleDocumentClick); this._onDocumentKeyupListener = EventListener.listen(document, 'keyup', this.handleDocumentKeyUp); }, unbindRootCloseHandlers: function () { if (this._onDocumentClickListener) { this._onDocumentClickListener.remove(); } if (this._onDocumentKeyupListener) { this._onDocumentKeyupListener.remove(); } }, componentWillUnmount: function () { this.unbindRootCloseHandlers(); } }; module.exports = DropdownStateMixin;