1 | import React from 'react';
|
2 | /**
|
3 | * Iterates through children that are typically specified as `props.children`,
|
4 | * but only maps over children that are "valid elements".
|
5 | *
|
6 | * The mapFunction provided index will be normalised to the components mapped,
|
7 | * so an invalid component would not increase the index.
|
8 | *
|
9 | */
|
10 |
|
11 | function map(children, func) {
|
12 | var index = 0;
|
13 | return React.Children.map(children, function (child) {
|
14 | return /*#__PURE__*/React.isValidElement(child) ? func(child, index++) : child;
|
15 | });
|
16 | }
|
17 | /**
|
18 | * Iterates through children that are "valid elements".
|
19 | *
|
20 | * The provided forEachFunc(child, index) will be called for each
|
21 | * leaf child with the index reflecting the position relative to "valid components".
|
22 | */
|
23 |
|
24 |
|
25 | function forEach(children, func) {
|
26 | var index = 0;
|
27 | React.Children.forEach(children, function (child) {
|
28 | if ( /*#__PURE__*/React.isValidElement(child)) func(child, index++);
|
29 | });
|
30 | }
|
31 |
|
32 | export { map, forEach }; |
\ | No newline at end of file |