Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @typechecks static-only
10
* @providesModule ReactTransitionChildMapping
11
*/
12
13
"use strict";
14
15
var ReactChildren = require('ReactChildren');
16
17
var ReactTransitionChildMapping = {
18
/**
19
* Given `this.props.children`, return an object mapping key to child. Just
20
* simple syntactic sugar around ReactChildren.map().
21
*
22
* @param {*} children `this.props.children`
23
* @return {object} Mapping of key to child
24
*/
25
getChildMapping: function(children) {
26
return ReactChildren.map(children, function(child) {
27
return child;
28
});
29
},
30
31
/**
32
* When you're adding or removing children some may be added or removed in the
33
* same render pass. We want to show *both* since we want to simultaneously
34
* animate elements in and out. This function takes a previous set of keys
35
* and a new set of keys and merges them with its best guess of the correct
36
* ordering. In the future we may expose some of the utilities in
37
* ReactMultiChild to make this easy, but for now React itself does not
38
* directly have this concept of the union of prevChildren and nextChildren
39
* so we implement it here.
40
*
41
* @param {object} prev prev children as returned from
42
* `ReactTransitionChildMapping.getChildMapping()`.
43
* @param {object} next next children as returned from
44
* `ReactTransitionChildMapping.getChildMapping()`.
45
* @return {object} a key set that contains all keys in `prev` and all keys
46
* in `next` in a reasonable order.
47
*/
48
mergeChildMappings: function(prev, next) {
49
prev = prev || {};
50
next = next || {};
51
52
function getValueForKey(key) {
53
if (next.hasOwnProperty(key)) {
54
return next[key];
55
} else {
56
return prev[key];
57
}
58
}
59
60
// For each key of `next`, the list of keys to insert before that key in
61
// the combined list
62
var nextKeysPending = {};
63
64
var pendingKeys = [];
65
for (var prevKey in prev) {
66
if (next.hasOwnProperty(prevKey)) {
67
if (pendingKeys.length) {
68
nextKeysPending[prevKey] = pendingKeys;
69
pendingKeys = [];
70
}
71
} else {
72
pendingKeys.push(prevKey);
73
}
74
}
75
76
var i;
77
var childMapping = {};
78
for (var nextKey in next) {
79
if (nextKeysPending.hasOwnProperty(nextKey)) {
80
for (i = 0; i < nextKeysPending[nextKey].length; i++) {
81
var pendingNextKey = nextKeysPending[nextKey][i];
82
childMapping[nextKeysPending[nextKey][i]] = getValueForKey(
83
pendingNextKey
84
);
85
}
86
}
87
childMapping[nextKey] = getValueForKey(nextKey);
88
}
89
90
// Finally, add the keys which didn't appear before any key in `next`
91
for (i = 0; i < pendingKeys.length; i++) {
92
childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]);
93
}
94
95
return childMapping;
96
}
97
};
98
99
module.exports = ReactTransitionChildMapping;
100
101