[Ivan Diaz] - Update reactDFS lib
This commit is contained in:
parent
06763a69f4
commit
5acd086dd8
|
@ -1,4 +1,5 @@
|
||||||
import React from 'react/addons';
|
import React from 'react/addons';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
var reactDFS = function (children, visitFunction) {
|
var reactDFS = function (children, visitFunction) {
|
||||||
var stack = [];
|
var stack = [];
|
||||||
|
@ -12,8 +13,31 @@ var reactDFS = function (children, visitFunction) {
|
||||||
React.Children.forEach(element.props.children, child => tempChilds.push(child));
|
React.Children.forEach(element.props.children, child => tempChilds.push(child));
|
||||||
|
|
||||||
visitFunction(element);
|
visitFunction(element);
|
||||||
stack.concat(tempChilds.reverse());
|
stack = stack.concat(tempChilds.reverse());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default reactDFS;
|
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
|
||||||
|
};
|
Loading…
Reference in New Issue