Skip to content

Commit

Permalink
Construct actions recursively during render. Fixes #14.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmingoia committed Mar 30, 2016
1 parent c547178 commit 39a3db1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
14 changes: 11 additions & 3 deletions src/Pux.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ exports.renderToString = function (htmlSignal) {
};

exports.render = function (input, parentAction, html) {
if (typeof html === 'string') return html;

var props = html.props
var newProps = {};

Expand All @@ -52,7 +50,17 @@ exports.render = function (input, parentAction, html) {

var newChildren = React.Children.map(html.props.children, function (child) {
var childAction = child.props && child.props.puxParentAction;
return exports.render(input, childAction || parentAction, child);
var action = parentAction;
if (childAction) {
action = function (a) {
return parentAction(childAction(a));
};
}
if (typeof child === 'string') {
return child;
} else {
return exports.render(input, action, child);
}
});

return React.cloneElement(html, newProps, newChildren);
Expand Down
16 changes: 11 additions & 5 deletions src/Pux/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ exports.append = function (html1, html2) {
return html1.concat(html2);
};

exports.mapActions = function (parentAction, html) {
var childAction = html.props.puxParentAction;
return React.cloneElement(html, {
puxParentAction: childAction ? parentAction(childAction) : parentAction
});
exports.forwardTo = function (parentAction) {
return function (html) {
var childAction = html.props.puxParentAction;
var action = parentAction;
if (childAction) {
action = function (a) {
return parentAction(childAction(a));
}
}
return React.cloneElement(html, { puxParentAction: action });
};
};
5 changes: 1 addition & 4 deletions src/Pux/Html.purs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ foreign import append :: forall a. Fn2 (Html a) (Html a) (Html a)
-- | forwardTo Bottom $ Counter.view state.bottomCount
-- | button ! onClick (const Reset) # text "Reset"
-- | ```
forwardTo :: forall a b. (a -> b) -> Html a -> Html b
forwardTo a2b html = runFn2 mapActions a2b html

foreign import mapActions :: forall a b. Fn2 (a -> b) (Html a) (Html b)
foreign import forwardTo :: forall a b. (a -> b) -> Html a -> Html b

-- | Combine elements with attributes.
-- |
Expand Down

0 comments on commit 39a3db1

Please sign in to comment.