diff --git a/src/utils/react-dfs.js b/src/utils/react-dfs.js index ffefa1a4..daf05062 100644 --- a/src/utils/react-dfs.js +++ b/src/utils/react-dfs.js @@ -1,4 +1,5 @@ import React from 'react/addons'; +import _ from 'lodash'; var reactDFS = function (children, visitFunction) { var stack = []; @@ -12,8 +13,31 @@ var reactDFS = function (children, visitFunction) { React.Children.forEach(element.props.children, child => tempChilds.push(child)); visitFunction(element); - stack.concat(tempChilds.reverse()); + stack = stack.concat(tempChilds.reverse()); } }; -export default reactDFS; \ No newline at end of file +var renderChildrenWithProps = function(children, mapFunction) { + if (typeof children !== 'object' || children === null) { + return children; + } + + return React.Children.map(children, function (child) { + var props = mapFunction(child); + + if (typeof child !== 'object' || child === null) { + return child; + } + + if (!_.isEmpty(props)) { + return React.cloneElement(child, props, child.props && child.props.children); + } else { + return React.cloneElement(child, {}, renderChildrenWithProps(child.props && child.props.children, mapFunction)); + } + }.bind(this)); +}; + +export default { + renderChildrenWithProps, + reactDFS +}; \ No newline at end of file