summaryrefslogtreecommitdiffstats
path: root/webapp/plugins/pluggable/pluggable.jsx
blob: c81d8df5ee6d5cd394bdfb4b9d6e79316f2a8fdb (plain)
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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

// EXPERIMENTAL - SUBJECT TO CHANGE

import React from 'react';
import PropTypes from 'prop-types';

export default class Pluggable extends React.PureComponent {
    static propTypes = {

        /*
         * Should be a single overridable React component
         */
        children: PropTypes.element.isRequired,

        /*
         * Components for overriding provided by plugins
         */
        components: PropTypes.object.isRequired,

        /*
         * Logged in user's theme
         */
        theme: PropTypes.object.isRequired
    }

    render() {
        const child = React.Children.only(this.props.children).type;
        const components = this.props.components;

        if (child == null) {
            return null;
        }

        const childName = child.getComponentName();

        // Include any props passed to this component or to the child component
        let props = {...this.props};
        Reflect.deleteProperty(props, 'children');
        Reflect.deleteProperty(props, 'components');
        props = {...props, ...this.props.children.props};

        // Override the default component with any registered plugin's component
        if (components.hasOwnProperty(childName)) {
            const PluginComponent = components[childName];
            return (
                <PluginComponent
                    {...props}
                    theme={this.props.theme}
                />
            );
        }

        return React.cloneElement(this.props.children, {...props});
    }
}