UNPKG

1.16 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5exports.__esModule = true;
6exports.map = map;
7exports.forEach = forEach;
8
9var _react = _interopRequireDefault(require("react"));
10
11/**
12 * Iterates through children that are typically specified as `props.children`,
13 * but only maps over children that are "valid elements".
14 *
15 * The mapFunction provided index will be normalised to the components mapped,
16 * so an invalid component would not increase the index.
17 *
18 */
19function map(children, func) {
20 var index = 0;
21 return _react.default.Children.map(children, function (child) {
22 return /*#__PURE__*/_react.default.isValidElement(child) ? func(child, index++) : child;
23 });
24}
25/**
26 * Iterates through children that are "valid elements".
27 *
28 * The provided forEachFunc(child, index) will be called for each
29 * leaf child with the index reflecting the position relative to "valid components".
30 */
31
32
33function forEach(children, func) {
34 var index = 0;
35
36 _react.default.Children.forEach(children, function (child) {
37 if ( /*#__PURE__*/_react.default.isValidElement(child)) func(child, index++);
38 });
39}
\No newline at end of file