//此组件将指定组件渲染到指定的dom节点上 import * as React from 'react' import * as ReactDom from 'react-dom'; interface MountProps { rootDom?: any; className?: string; styleStr?: string; children: any; triggerClose?: Function } export default class Mount extends React.Component { popup: any; rootDom: any componentDidMount() { let { rootDom = document.getElementById('bs-main') } = this.props; this.popup = document.createElement('div'); this.popup.setAttribute('class', this.props.className || '') this.popup.setAttribute('style', this.props.styleStr || '') this.rootDom = rootDom; this.rootDom.appendChild(this.popup); this._renderLayer(); document.addEventListener('click', this.closeMount, true) } componentDidUpdate() { this._renderLayer(); } componentWillUnmount() { this.close(); document.removeEventListener('click', this.closeMount, true) } closeMount: any = (e: React.MouseEvent) => { const el = this.popup; if (el && el.contains && !el.contains(e.target)) { const { triggerClose } = this.props; triggerClose && triggerClose() } } close = () => { this.rootDom && this.popup && this.rootDom.removeChild(this.popup); } _renderLayer() { const { children } = this.props if (children) ReactDom.render(children, this.popup); } render() { return null; } }