You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ui-lib/src/util/children-with-props.js

21 lines
621 B
JavaScript

"use strict";
// NOTE: Only use this where absolutely needed! Normally, the context API should be preferred where possible.
const React = require("react");
module.exports = function childrenWithProps(children, mapper) {
return React.Children.map(children, (child, i) => {
let extraProps = (typeof mapper === "function")
? mapper(child, i)
: mapper;
if (extraProps != null) {
// FIXME: Do we need to check React.isValidElement here, to deal with eg. text nodes? Or is that handled internally by React.cloneElement?
return React.cloneElement(child, extraProps);
} else {
return child;
}
});
};