Skip to content
Snippets Groups Projects
Unverified Commit 8c3244b7 authored by Jack Reed's avatar Jack Reed Committed by GitHub
Browse files

Merge pull request #3257 from ProjectMirador/plugin-component-elements

Allow plugin components to be given as react elements
parents fae99a70 dbedbed8
Branches
Tags
No related merge requests found
...@@ -61,7 +61,7 @@ ...@@ -61,7 +61,7 @@
config: { config: {
foo: 'bar', foo: 'bar',
}, },
component: AddPluginComponentA, component: <AddPluginComponentA />,
}; };
const addPluginB = { const addPluginB = {
......
...@@ -58,8 +58,12 @@ describe('connectPluginsToStore', () => { ...@@ -58,8 +58,12 @@ describe('connectPluginsToStore', () => {
const ComponentB = props => null; const ComponentB = props => null;
const plugins = [ const plugins = [
{ component: ComponentA, mode: 'wrap', target: 'Window' }, {
{ component: ComponentB, mode: 'add', target: 'TopBar' }, component: ComponentA, mapStateToProps: {}, mode: 'wrap', target: 'Window',
},
{
component: ComponentB, mapDispatchToProps: {}, mode: 'add', target: 'TopBar',
},
]; ];
const result = connectPluginsToStore(plugins); const result = connectPluginsToStore(plugins);
...@@ -67,6 +71,20 @@ describe('connectPluginsToStore', () => { ...@@ -67,6 +71,20 @@ describe('connectPluginsToStore', () => {
expect(result[0].component.displayName).toBe('Connect(ComponentA)'); expect(result[0].component.displayName).toBe('Connect(ComponentA)');
expect(result[1].component.displayName).toBe('Connect(ComponentB)'); expect(result[1].component.displayName).toBe('Connect(ComponentB)');
}); });
it('returns plugins unchanged that do not need a connection to the store', () => {
/** */
const ComponentA = props => null;
const plugins = [
{
component: ComponentA, mode: 'wrap', target: 'Window',
},
];
const result = connectPluginsToStore(plugins);
expect(result[0].component).toEqual(ComponentA);
});
}); });
describe('addPluginsToCompanionWindowsRegistry', () => { describe('addPluginsToCompanionWindowsRegistry', () => {
......
...@@ -6,11 +6,15 @@ export const PluginHook = React.forwardRef((props, ref) => { ...@@ -6,11 +6,15 @@ export const PluginHook = React.forwardRef((props, ref) => {
const { classes, ...otherProps } = props; // eslint-disable-line react/prop-types const { classes, ...otherProps } = props; // eslint-disable-line react/prop-types
return PluginComponents ? ( return PluginComponents ? (
PluginComponents.map((PluginComponent, index) => ( // eslint-disable-line react/prop-types PluginComponents.map((PluginComponent, index) => ( // eslint-disable-line react/prop-types
React.isValidElement(PluginComponent)
? React.cloneElement(PluginComponent, { ...otherProps, ref })
: (
<PluginComponent <PluginComponent
ref={ref} ref={ref}
{...otherProps} {...otherProps}
key={index} // eslint-disable-line react/no-array-index-key key={index} // eslint-disable-line react/no-array-index-key
/> />
)
)) ))
) : null; ) : null;
}); });
...@@ -40,6 +40,8 @@ export function addPluginsToCompanionWindowsRegistry(plugins) { ...@@ -40,6 +40,8 @@ export function addPluginsToCompanionWindowsRegistry(plugins) {
/** Connect plugin component to state */ /** Connect plugin component to state */
function connectPluginComponent(plugin) { function connectPluginComponent(plugin) {
if (!plugin.mapStateToProps && !plugin.mapDispatchToProps) return plugin.component;
return connect( return connect(
plugin.mapStateToProps, plugin.mapStateToProps,
plugin.mapDispatchToProps, plugin.mapDispatchToProps,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment