-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (56 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-FileCopyrightText: 2023 Redradix - development@redradix.com
//
// SPDX-License-Identifier: MIT
let React
try {
React = require('react')
} catch (err) {
throw new Error('This package must be used in a React app')
}
/**
* childrenToArray – Convert react children to an array. Works for a single
* child. Omits children that will not be rendered (null, undefined, false).
* Flattens children inside fragments
* REVIEW we are calling Array.prototype.flat() once in every recursive call
* @param {React.Children} children - Children object as provided by react
* @param {Boolean} flatten - Whether or not to flatten children inside
* fragments. Defaults to true
* @return {Array} An array of react elements
*/
const childrenToArray = (children, flatten = true) => {
const array = React.Children.toArray(children)
if (flatten) {
return array
.map(child =>
child.type === React.Fragment
? childrenToArray(child.props.children, flatten)
: child,
)
.flat()
}
return array
}
/**
* countChildren – Count the number of children. Omits children that will not be
* rendered (null, undefined, false). Flattens children inside fragments
* @param {React.Children} children - Children object as provided by react
* @param {Boolean} flatten - Whether or not to flatten children inside
* fragments. Defaults to true
* @return {Number} The actual number of children
*/
const countChildren = (children, flatten = true) =>
React.Children.count(childrenToArray(children, flatten))
/**
* mapChildren – Map over all children, invoking a callback with any of them.
* Omits children that will not be rendered (null, undefined, false). Flattens
* children inside fragments
* @param {React.Children} children - Children object as provided by react
* @param {Function} fn - Callback that will be called with every child
* @param {Boolean} flatten - Whether or not to flatten children inside
* fragments. Defaults to true
* @return {Array} An array with the results of calling fn with every child in
* children
*/
const mapChildren = (children, fn, flatten = true) =>
React.Children.map(childrenToArray(children, flatten), fn)
module.exports = { countChildren, mapChildren }