Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import ReactDOM from 'react-dom'; |
| 3 | import BootstrapModal from 'react-bootstrap/lib/Modal.js'; |
| 4 | |
| 5 | let nextModalId = 0; |
| 6 | |
| 7 | export default class Modal extends React.Component { |
| 8 | |
| 9 | static Header = BootstrapModal.Header; |
| 10 | |
| 11 | static Title = BootstrapModal.Title; |
| 12 | |
| 13 | static Footer = BootstrapModal.Footer; |
| 14 | |
| 15 | static Body = class ModalBody extends React.Component { |
| 16 | |
| 17 | render() { |
| 18 | let {children, ...props} = this.props; |
| 19 | return ( |
| 20 | <BootstrapModal.Body {...props}> |
| 21 | {children} |
| 22 | </BootstrapModal.Body> |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | componentDidMount() { |
| 27 | let element = ReactDOM.findDOMNode(this); |
| 28 | element.addEventListener('click', event => { |
| 29 | if (event.target.tagName === 'A') { |
| 30 | event.preventDefault(); |
| 31 | } |
| 32 | }); |
| 33 | ['wheel', 'mousewheel', 'DOMMouseScroll'].forEach(eventType => |
| 34 | element.addEventListener(eventType, event => event.stopPropagation()) |
| 35 | ); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | componentWillMount() { |
| 40 | this.modalId = `dox-ui-modal-${nextModalId++}`; |
| 41 | } |
| 42 | |
| 43 | componentDidMount() { |
| 44 | this.ensureRootClass(); |
| 45 | } |
| 46 | |
| 47 | componentDidUpdate() { |
| 48 | this.ensureRootClass(); |
| 49 | } |
| 50 | |
| 51 | ensureRootClass() { |
| 52 | let element = document.getElementById(this.modalId); |
| 53 | while(element && !element.hasAttribute('data-reactroot')) { |
| 54 | element = element.parentElement; |
| 55 | } |
| 56 | if (element && !element.classList.contains('dox-ui')) { |
| 57 | element.classList.add('dox-ui'); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | render() { |
| 62 | let {children, ...props} = this.props; |
| 63 | return ( |
| 64 | <BootstrapModal {...props} id={this.modalId}> |
| 65 | {children} |
| 66 | </BootstrapModal> |
| 67 | ); |
| 68 | } |
| 69 | } |