File size: 2,255 Bytes
6bcb42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e792c7
6bcb42f
 
 
5e792c7
6bcb42f
 
 
 
 
 
 
5e792c7
 
 
 
 
 
 
6bcb42f
 
 
 
5e792c7
6bcb42f
 
 
 
 
5e792c7
6bcb42f
 
 
5e792c7
6bcb42f
 
 
 
 
 
 
 
 
 
 
5e792c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import omit from 'lodash.omit';
import PropTypes from 'prop-types';
import React from 'react';
import Style from 'to-style';
import stylePropType from 'react-style-proptype';

/*
 * DOMElementRenderer wraps a DOM element, allowing it to be
 * rendered by React. It's up to the containing component
 * to retain a reference to the element prop, or else it
 * will be garbage collected after unmounting.
 *
 * Props passed to the DOMElementRenderer will be set on the
 * DOM element like it's a normal component.
 */
class DOMElementRenderer extends React.Component {
    constructor (props) {
        super(props);
        this.setContainer = this.setContainer.bind(this);
    }
    componentDidMount () {
        if (!this.props.domElement) return;
        this.container.appendChild(this.props.domElement);
    }
    componentWillUnmount () {
        if (!this.props.domElement) return;
        if (this.props.domElement.parentNode !== this.container) return;
        this.container.removeChild(this.props.domElement);
    }
    setContainer (c) {
        this.container = c;
    }
    render () {
        let element = this.props.domElement;
        // if we where never passed an element, ensure Object.assign doesnt error out about
        if (!element) {
            element = document.createElement('span');
            element.innerText = 'ERR: No element provided';
            console.warn('No element provided to the DOMElementRenderer');
        }
        // Apply props to the DOM element, so its attributes
        // are updated as if it were a normal component.
        // Look at me, I'm the React now!
        Object.assign(
            element,
            omit(this.props, ['domElement', 'children', 'style'])
        );

        // Convert react style prop to dom element styling.
        if (this.props.style) {
            element.style.cssText = Style.string(this.props.style);
        }
        if (this.container) {
            this.container.innerHTML = '';
            this.container.appendChild(element);
        }

        return <div ref={this.setContainer} />;
    }
}

DOMElementRenderer.propTypes = {
    domElement: PropTypes.instanceOf(Element).isRequired,
    style: stylePropType
};

export default DOMElementRenderer;