diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-08-29 09:54:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-29 09:54:02 -0400 |
commit | 257edc9ea3b25328aa44098e963815c3c3d25312 (patch) | |
tree | ed72b2f646ea9287fdccb5076b99b01bc8585a1d /webapp/plugins/pluggable | |
parent | 82a8bd99cc5fe59fe4577c9b0d2c06a82c89e628 (diff) | |
download | chat-257edc9ea3b25328aa44098e963815c3c3d25312.tar.gz chat-257edc9ea3b25328aa44098e963815c3c3d25312.tar.bz2 chat-257edc9ea3b25328aa44098e963815c3c3d25312.zip |
Experimental implementation for webapp plugins (#7185)
* Start of experimental implementation for webapp plugins
* Updates to webapp plugin architecture
* Update pluggable test
* Remove debug code
Diffstat (limited to 'webapp/plugins/pluggable')
-rw-r--r-- | webapp/plugins/pluggable/index.js | 17 | ||||
-rw-r--r-- | webapp/plugins/pluggable/pluggable.jsx | 55 |
2 files changed, 72 insertions, 0 deletions
diff --git a/webapp/plugins/pluggable/index.js b/webapp/plugins/pluggable/index.js new file mode 100644 index 000000000..d00f18a5d --- /dev/null +++ b/webapp/plugins/pluggable/index.js @@ -0,0 +1,17 @@ +// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import {connect} from 'react-redux'; +import {getTheme} from 'mattermost-redux/selectors/entities/preferences'; + +import Pluggable from './pluggable.jsx'; + +function mapStateToProps(state, ownProps) { + return { + ...ownProps, + components: state.plugins.components, + theme: getTheme(state) + }; +} + +export default connect(mapStateToProps)(Pluggable); diff --git a/webapp/plugins/pluggable/pluggable.jsx b/webapp/plugins/pluggable/pluggable.jsx new file mode 100644 index 000000000..566e024e5 --- /dev/null +++ b/webapp/plugins/pluggable/pluggable.jsx @@ -0,0 +1,55 @@ +// 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; + } + + // 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(child.name)) { + const PluginComponent = components[child.name]; + return ( + <PluginComponent + {...props} + theme={this.props.theme} + /> + ); + } + + return React.cloneElement(this.props.children, {...props}); + } +} |