UNPKG

braid-design-system

Version:
30 lines (29 loc) 848 B
"use strict"; const react = require("react"); const lib_utils_isFragment_cjs = require("./isFragment.cjs"); function flattenChildren(children, depth = 0, keys = []) { return react.Children.toArray(children).reduce( (acc, node, nodeIndex) => { if (lib_utils_isFragment_cjs.isFragment(node)) { acc.push( ...flattenChildren( node.props.children, depth + 1, keys.concat(node.key || nodeIndex) ) ); } else if (react.isValidElement(node)) { acc.push( react.cloneElement(node, { key: keys.concat(String(node.key)).join(".") }) ); } else if (typeof node === "string" || typeof node === "number") { acc.push(node); } return acc; }, [] ); } exports.flattenChildren = flattenChildren;