Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
fix(compose): Propers should pass their output to next proper
Browse files Browse the repository at this point in the history
This is kinda big deal. Might break old behavior, but I'm fairly sure that this is the way to go.
Since we did the optimization for nesting composed components, we need this to attain expected
behavior

Subsequent props will now be passed to propers
  • Loading branch information
danielwerthen committed Jun 13, 2016
1 parent a2cfd88 commit 17d698f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
20 changes: 20 additions & 0 deletions lib/__tests__/compose-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,24 @@ describe('mapProp', function () {
var p = shallow(React.createElement(Comped, { x: 5 })).node;
expect(p.props.x).toEqual(10);
});
});

describe('composing', function () {
it('chains properly', function () {
var f0 = {
a: 7
};
function f1() {
return { b: 5 };
}
function f2(_ref8) {
var a = _ref8.a;
var b = _ref8.b;

return { c: a + b };
}
var Comped = compose(f0, f1, f2)('p');
var p = shallow(React.createElement(Comped, null)).node;
expect(p.props.c).toEqual(13);
});
});
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ var compose = exports.compose = function compose() {
function doCompose(Component, ps) {
var ComposedComponent = function ComposedComponent(props, context) {
var base = _extends({}, props, ps.constant);
var finalProps = mergeObjArr([base].concat(_toConsumableArray(applyFunctor(ps.dynamic, base, context))));
var finalProps = ps.dynamic.reduce(function (obj, fn) {
return Object.assign({}, obj, applyFunctor(fn, obj, context));
}, base);
return (0, _config.composeComponent)(Component, finalProps);
};
ComposedComponent.contextTypes = (0, _config.exposeContextTypes)();
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/compose-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,20 @@ describe('mapProp', () => {
expect(p.props.x).toEqual(10);
});
});

describe('composing', () => {
it('chains properly', () => {
const f0 = {
a: 7,
};
function f1() {
return { b: 5 };
}
function f2({ a, b }) {
return { c: a + b };
}
const Comped = compose(f0, f1, f2)('p');
const p = shallow(<Comped />).node;
expect(p.props.c).toEqual(12);
});
});
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export const compose = (...propers) => {
function doCompose(Component, ps) {
const ComposedComponent = (props, context) => {
const base = { ...props, ...ps.constant };
const finalProps = mergeObjArr([base, ...applyFunctor(ps.dynamic, base, context)]);
const finalProps = ps.dynamic.reduce((obj, fn) =>
Object.assign({}, obj, applyFunctor(fn, obj, context)),
base);
return composeComponent(Component, finalProps);
};
ComposedComponent.contextTypes = exposeContextTypes();
Expand Down

0 comments on commit 17d698f

Please sign in to comment.