Skip to content

Commit

Permalink
fluxible-addons-react: add plugins param to provideContext
Browse files Browse the repository at this point in the history
In this way we can still inject custom data from plugins into the
state.
  • Loading branch information
pablopalacios committed Nov 11, 2020
1 parent fc169a5 commit 39af154
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
16 changes: 14 additions & 2 deletions packages/fluxible-addons-react/src/FluxibleContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, createContext, createElement } from 'react';
import { node, object } from 'prop-types';
import { arrayOf, node, object, string } from 'prop-types';

export const FluxibleContext = createContext({
executeAction: () => {},
Expand All @@ -9,10 +9,17 @@ export const FluxibleContext = createContext({
export class FluxibleProvider extends Component {
constructor(props) {
super(props);
this.state = {

const state = {
executeAction: this.props.context.executeAction,
getStore: this.props.context.getStore,
};

this.props.plugins.forEach(plugin => {
state[plugin] = this.props.context[plugin];
});

this.state = state;
}

render() {
Expand All @@ -24,4 +31,9 @@ export class FluxibleProvider extends Component {
FluxibleProvider.propTypes = {
children: node.isRequired,
context: object.isRequired,
plugins: arrayOf(string)
};

FluxibleProvider.defaultProps = {
plugins: []
};
5 changes: 3 additions & 2 deletions packages/fluxible-addons-react/src/provideContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import { FluxibleProvider } from './FluxibleContext';
*
* @method provideContext
* @param {React.Component} [Component] component to wrap
* @param {array} [plugins] list of plugins names to inject into the context
* @returns {React.Component}
*/
function provideContext(Component) {
function provideContext(Component, plugins) {
class ContextProvider extends ReactComponent {
constructor(props) {
super(props);
Expand All @@ -33,7 +34,7 @@ function provideContext(Component) {

const { context } = this.props;
const children = createElement(Component, {...this.props, ...props});
return createElement(FluxibleProvider, { context }, children);
return createElement(FluxibleProvider, { context, plugins }, children);
}
}

Expand Down
26 changes: 25 additions & 1 deletion packages/fluxible-addons-react/tests/unit/lib/provideContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { renderToString } from 'react-dom/server';
import PropTypes from 'prop-types';
import { JSDOM } from 'jsdom';

import { provideContext } from '../../../';
import { provideContext, FluxibleContext } from '../../../';

describe('fluxible-addons-react', () => {
describe('provideContext', () => {
Expand Down Expand Up @@ -52,6 +52,30 @@ describe('fluxible-addons-react', () => {
);
});

it('should provide the context with custom types to children', () => {
const plugins = ['foo'];
const context = {
foo: 'bar',
executeAction: function() {},
getStore: function() {}
};

class Component extends React.Component {
static contextType = FluxibleContext;

render() {
expect(this.context.executeAction).to.equal(context.executeAction);
expect(this.context.getStore).to.equal(context.getStore);
expect(this.context.foo).to.equal(context.foo);
return null;
}
}

const WrappedComponent = provideContext(Component, plugins);

renderToString(<WrappedComponent context={context} />);
});

it('should hoist non-react statics to higher order component', () => {
const context = {
foo: 'bar',
Expand Down

0 comments on commit 39af154

Please sign in to comment.