blob: be4963ef654034812cb6a101461f9619bae17f73 [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001import React from 'react';
2import ReactDOM from 'react-dom';
3import BootstrapModal from 'react-bootstrap/lib/Modal.js';
4
5let nextModalId = 0;
6
7export 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}