Skip to content

Commit

Permalink
HoC props fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jozsi committed Jun 9, 2018
1 parent b1f12b0 commit fc03475
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
39 changes: 33 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,50 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var HOCPropsProxy = function HOCPropsProxy(props) {
var nextProxy = props.nextProxy,
fixture = props.fixture,
onComponentRef = props.onComponentRef,
onFixtureUpdate = props.onFixtureUpdate,
rest = _objectWithoutProperties(props, ["nextProxy", "fixture", "onComponentRef", "onFixtureUpdate"]);

var NextProxy = nextProxy.value,
next = nextProxy.next;

fixture.props = Object.assign({}, fixture.props, rest);

return _react2.default.createElement(NextProxy, {
fixture: fixture,
onComponentRef: onComponentRef,
onFixtureUpdate: onFixtureUpdate,
nextProxy: next()
});
};

exports.default = function (_ref) {
var Component = _ref.component,
props = _ref.props,
fixtureKey = _ref.fixtureKey,
hoc = _ref.hoc;
return function WrapperProxy(_ref2) {
var _ref2$nextProxy = _ref2.nextProxy,
NextProxy = _ref2$nextProxy.value,
next = _ref2$nextProxy.next,
var nextProxy = _ref2.nextProxy,
nextProps = _objectWithoutProperties(_ref2, ["nextProxy"]);

var fixtureProps = nextProps.fixture[fixtureKey];
var fixtureEnabled = !!fixtureProps;
var NextComponent = fixtureEnabled && hoc ? fixtureProps[Symbol.iterator] ? Component.apply(undefined, _toConsumableArray(fixtureProps))(NextProxy) : Component(NextProxy) : NextProxy;
var nextProxyEl = _react2.default.createElement(NextComponent, Object.assign({}, nextProps, { nextProxy: next() }));

return fixtureEnabled && !hoc ? _react2.default.createElement(
if (fixtureEnabled && hoc) {
var HOComponent = fixtureProps[Symbol.iterator] ? Component.apply(undefined, _toConsumableArray(fixtureProps))(HOCPropsProxy) : Component(HOCPropsProxy);

return _react2.default.createElement(HOComponent, Object.assign({}, nextProps, { nextProxy: nextProxy }));
}

var NextProxy = nextProxy.value,
next = nextProxy.next;

var nextProxyEl = _react2.default.createElement(NextProxy, Object.assign({}, nextProps, { nextProxy: next() }));

return fixtureEnabled ? _react2.default.createElement(
Component,
Object.assign({}, props, fixtureProps),
nextProxyEl
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-cosmos-wrapper-proxy",
"version": "1.2.1",
"version": "1.3.0",
"description": "Easily wrap components using react-cosmos",
"main": "lib/index.js",
"author": "Józsi <[email protected]>",
Expand Down
16 changes: 9 additions & 7 deletions src/__tests__/hoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ const NextProxy = props => {
return <P {...props} nextProxy={next()} />;
};

const LastProxy = ({
fixture: { component: C, props },
onComponentRef,
onFixtureUpdate,
nextProxy,
...rest
}) => <C {...rest} {...props} />;
const LastProxy = ({ fixture: { component: C, props } }) => <C {...props} />;

const HOComponent = WrappedComponent =>
function WrapperComponent(props) {
Expand Down Expand Up @@ -112,6 +106,9 @@ describe("wrapped", () => {
renderProxy(fixture);
});

fixture.props = {
fromHoc: true
};
commonTests(fixture);

it("should wrap", () => {
Expand All @@ -134,6 +131,11 @@ describe("wrapped with parameters", () => {
});
});

fixture.props = {
fromHocTwo: true,
firstPassedProp: "hello",
secondPassedProp: "world"
};
commonTests(fixture);

it("should wrap", () => {
Expand Down
49 changes: 37 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
import React from "react";

const HOCPropsProxy = props => {
const {
nextProxy,
fixture,
onComponentRef,
onFixtureUpdate,
...rest
} = props;
const { value: NextProxy, next } = nextProxy;
fixture.props = {
...fixture.props,
...rest
};

return (
<NextProxy
fixture={fixture}
onComponentRef={onComponentRef}
onFixtureUpdate={onFixtureUpdate}
nextProxy={next()}
/>
);
};

export default ({ component: Component, props, fixtureKey, hoc }) =>
function WrapperProxy({
nextProxy: { value: NextProxy, next },
...nextProps
}) {
function WrapperProxy({ nextProxy, ...nextProps }) {
const fixtureProps = nextProps.fixture[fixtureKey];
const fixtureEnabled = !!fixtureProps;
const NextComponent =
fixtureEnabled && hoc
? fixtureProps[Symbol.iterator]
? Component(...fixtureProps)(NextProxy)
: Component(NextProxy)
: NextProxy;
const nextProxyEl = <NextComponent {...nextProps} nextProxy={next()} />;

return fixtureEnabled && !hoc ? (
if (fixtureEnabled && hoc) {
const HOComponent = fixtureProps[Symbol.iterator]
? Component(...fixtureProps)(HOCPropsProxy)
: Component(HOCPropsProxy);

return <HOComponent {...nextProps} nextProxy={nextProxy} />;
}

const { value: NextProxy, next } = nextProxy;
const nextProxyEl = <NextProxy {...nextProps} nextProxy={next()} />;

return fixtureEnabled ? (
<Component {...props} {...fixtureProps}>
{nextProxyEl}
</Component>
Expand Down

0 comments on commit fc03475

Please sign in to comment.