From 3be152c68c29aff76711083bc092f349f0a6e5a4 Mon Sep 17 00:00:00 2001 From: Juliana <408934141@qq.com> Date: Tue, 7 Apr 2020 13:57:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E4=BA=A4mapChildren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..a5a2bd6 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,36 @@ +let result = []; +let count = 0; + function mapChildren(children, func, context) { - //TODO实现此mapChildren方法 - return children; + //TODO实现此mapChildren方法 + children.map((child, i) => { + invoke(child, count, func); + count++; + }); + return result; } -export { - mapChildren as map, -}; \ No newline at end of file +function invoke(child, count, func) { + if (!child) { + return null; + } + if (Array.isArray(child)) { + child.map((subChild, i) => { + subChild.count = i; + invoke(subChild, count, func); + }); + } else { + let tempRes = []; + if (child.count) { + tempRes = func(child, count + child.count); + } else { + tempRes = func(child, count); + } + if (tempRes && tempRes.length) { + tempRes.map((subRes) => { + result.push(subRes); + }); + } + } +} +export { mapChildren as map }; -- Gitee