diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d81edaad3705a3dca0942e1cbc01bff2624..96d024ca0a65a31786ca1cdbfbcf95b08f527d41 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,6 +1,30 @@ function mapChildren(children, func, context) { //TODO实现此mapChildren方法 - return children; + children = Array.isArray(children) ? children: [children] + let newArr = []; + let index = 0; + function flatMap(kids, key){ + kids.forEach((kid, i) => { + if (Array.isArray(kid)) { + key += `${i}:` + flatMap(kid, key); + }else{ + const domkey = kid.key ? `$${kid.key}`: `${i}`; + let res = func(kid, index); + res = Array.isArray(res)? res: [res]; + res.forEach(child => { + child.key = `${key}${domkey}/.$${child.key? child.key: i}`; + newArr.push(child); + }); + index++; + } + }); + } + flatMap(children, '.'); + + // children = children.flat(Infinity).map(func).flat(); + + return newArr; } export {