react-bootstrap
Version:
Bootstrap 3 components build with React
85 lines (70 loc) • 2.26 kB
JavaScript
var React = require('react');
var CustomPropTypes = require('./utils/CustomPropTypes');
module.exports = {
propTypes: {
container: CustomPropTypes.mountable
},
getDefaultProps: function () {
return {
container: {
// Provide `getDOMNode` fn mocking a React component API. The `document.body`
// reference needs to be contained within this function so that it is not accessed
// in environments where it would not be defined, e.g. nodejs. Equally this is needed
// before the body is defined where `document.body === null`, this ensures
// `document.body` is only accessed after componentDidMount.
getDOMNode: function getDOMNode() {
return document.body;
}
}
};
},
componentWillUnmount: function () {
this._unrenderOverlay();
if (this._overlayTarget) {
this.getContainerDOMNode()
.removeChild(this._overlayTarget);
this._overlayTarget = null;
}
},
componentDidUpdate: function () {
this._renderOverlay();
},
componentDidMount: function () {
this._renderOverlay();
},
_mountOverlayTarget: function () {
this._overlayTarget = document.createElement('div');
this.getContainerDOMNode()
.appendChild(this._overlayTarget);
},
_renderOverlay: function () {
if (!this._overlayTarget) {
this._mountOverlayTarget();
}
var overlay = this.renderOverlay();
// Save reference to help testing
if (overlay !== null) {
this._overlayInstance = React.render(overlay, this._overlayTarget);
} else {
// Unrender if the component is null for transitions to null
this._unrenderOverlay();
}
},
_unrenderOverlay: function () {
React.unmountComponentAtNode(this._overlayTarget);
this._overlayInstance = null;
},
getOverlayDOMNode: function () {
if (!this.isMounted()) {
throw new Error('getOverlayDOMNode(): A component must be mounted to have a DOM node.');
}
if (this._overlayInstance) {
return this._overlayInstance.getDOMNode();
}
return null;
},
getContainerDOMNode: function () {
return this.props.container.getDOMNode ?
this.props.container.getDOMNode() : this.props.container;
}
};