1 | import React from 'react';
|
2 | export function toArrayChildren(children) {
|
3 | var ret = [];
|
4 | React.Children.forEach(children, function (c) {
|
5 | ret.push(c);
|
6 | });
|
7 | return ret;
|
8 | }
|
9 | export function findChildInChildrenByKey(children, key) {
|
10 | var ret = null;
|
11 |
|
12 | if (children) {
|
13 | children.forEach(function (c) {
|
14 | if (ret || !c) {
|
15 | return;
|
16 | }
|
17 |
|
18 | if (c.key === key) {
|
19 | ret = c;
|
20 | }
|
21 | });
|
22 | }
|
23 |
|
24 | return ret;
|
25 | }
|
26 | export function mergeChildren(prev, next) {
|
27 | var ret = [];
|
28 |
|
29 |
|
30 | var nextChildrenPending = {};
|
31 | var pendingChildren = [];
|
32 | var followChildrenKey = null;
|
33 | prev.forEach(function (c) {
|
34 | if (!c) {
|
35 | return;
|
36 | }
|
37 |
|
38 | if (c.key && findChildInChildrenByKey(next, c.key)) {
|
39 | if (pendingChildren.length) {
|
40 | nextChildrenPending[c.key] = pendingChildren;
|
41 | pendingChildren = [];
|
42 | }
|
43 |
|
44 | followChildrenKey = c.key;
|
45 | } else if (c.key) {
|
46 | pendingChildren.push(c);
|
47 | }
|
48 | });
|
49 |
|
50 | if (!followChildrenKey) {
|
51 | ret = ret.concat(pendingChildren);
|
52 | }
|
53 |
|
54 | next.forEach(function (c) {
|
55 | if (!c) {
|
56 | return;
|
57 | }
|
58 |
|
59 | if (c.key && nextChildrenPending.hasOwnProperty(c.key)) {
|
60 |
|
61 | ret = ret.concat(nextChildrenPending[c.key]);
|
62 | }
|
63 |
|
64 | ret.push(c);
|
65 |
|
66 | if (c.key === followChildrenKey) {
|
67 | ret = ret.concat(pendingChildren);
|
68 | }
|
69 | });
|
70 | return ret;
|
71 | }
|
72 | export function transformArguments(arg, key, i) {
|
73 | var result;
|
74 |
|
75 | if (typeof arg === 'function') {
|
76 | result = arg({
|
77 | key: key,
|
78 | index: i
|
79 | });
|
80 | } else {
|
81 | result = arg;
|
82 | }
|
83 |
|
84 | return result;
|
85 | }
|
86 | export function getChildrenFromProps(props) {
|
87 | return props && props.children;
|
88 | } |
\ | No newline at end of file |