From 742144e39f3aee52bbac625180f998fd6ac9d5a3 Mon Sep 17 00:00:00 2001 From: liu <> Date: Mon, 6 Apr 2020 16:56:39 +0800 Subject: [PATCH 1/2] add #334# --- src/react/ReactChildren.js | 41 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..536297d 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,45 @@ + +let arr = []; +let index = 0; +let key = ""; function mapChildren(children, func, context) { //TODO实现此mapChildren方法 - return children; + + if (children === null || children === undefined) { + return children; + } + function fn(children) { + if (Array.isArray(children)) { + for (let i = 0; i < children.length; i++) { + let child = children[i]; + if (child.key == null) { + key = `.${index}/$` + } else { + key = `.$${child.key}/.$`; + } + if (Array.isArray(child)) { + fn(child); + return; + } + callback(child); + index++; + } + } else { + callback(children) + } + } + function callback(child) { + let ary = func(child, index); + for (let k = 0; k < ary.length; k++) { + ary[k].key = key + ary[k].key; + arr.push(ary[k]) + } + } + fn(children); + return arr } export { mapChildren as map, -}; \ No newline at end of file +}; + -- Gitee From c9748240084359db95aa2c1e7a34ba06891a496f Mon Sep 17 00:00:00 2001 From: liu <> Date: Mon, 6 Apr 2020 17:10:18 +0800 Subject: [PATCH 2/2] #334# --- src/react/ReactChildren.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index 536297d..39a2d8c 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -36,7 +36,7 @@ function mapChildren(children, func, context) { } } fn(children); - return arr + return arr; } export { -- Gitee