91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
exports.__esModule = true;
|
|
exports.getChildMapping = getChildMapping;
|
|
exports.mergeChildMappings = mergeChildMappings;
|
|
|
|
var _react = require('react');
|
|
|
|
/**
|
|
* Given `this.props.children`, return an object mapping key to child.
|
|
*
|
|
* @param {*} children `this.props.children`
|
|
* @return {object} Mapping of key to child
|
|
*/
|
|
function getChildMapping(children) {
|
|
if (!children) {
|
|
return children;
|
|
}
|
|
var result = {};
|
|
_react.Children.map(children, function (child) {
|
|
return child;
|
|
}).forEach(function (child) {
|
|
result[child.key] = child;
|
|
});
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* When you're adding or removing children some may be added or removed in the
|
|
* same render pass. We want to show *both* since we want to simultaneously
|
|
* animate elements in and out. This function takes a previous set of keys
|
|
* and a new set of keys and merges them with its best guess of the correct
|
|
* ordering. In the future we may expose some of the utilities in
|
|
* ReactMultiChild to make this easy, but for now React itself does not
|
|
* directly have this concept of the union of prevChildren and nextChildren
|
|
* so we implement it here.
|
|
*
|
|
* @param {object} prev prev children as returned from
|
|
* `ReactTransitionChildMapping.getChildMapping()`.
|
|
* @param {object} next next children as returned from
|
|
* `ReactTransitionChildMapping.getChildMapping()`.
|
|
* @return {object} a key set that contains all keys in `prev` and all keys
|
|
* in `next` in a reasonable order.
|
|
*/
|
|
function mergeChildMappings(prev, next) {
|
|
prev = prev || {};
|
|
next = next || {};
|
|
|
|
function getValueForKey(key) {
|
|
if (next.hasOwnProperty(key)) {
|
|
return next[key];
|
|
}
|
|
|
|
return prev[key];
|
|
}
|
|
|
|
// For each key of `next`, the list of keys to insert before that key in
|
|
// the combined list
|
|
var nextKeysPending = {};
|
|
|
|
var pendingKeys = [];
|
|
for (var prevKey in prev) {
|
|
if (next.hasOwnProperty(prevKey)) {
|
|
if (pendingKeys.length) {
|
|
nextKeysPending[prevKey] = pendingKeys;
|
|
pendingKeys = [];
|
|
}
|
|
} else {
|
|
pendingKeys.push(prevKey);
|
|
}
|
|
}
|
|
|
|
var i = void 0;
|
|
var childMapping = {};
|
|
for (var nextKey in next) {
|
|
if (nextKeysPending.hasOwnProperty(nextKey)) {
|
|
for (i = 0; i < nextKeysPending[nextKey].length; i++) {
|
|
var pendingNextKey = nextKeysPending[nextKey][i];
|
|
childMapping[nextKeysPending[nextKey][i]] = getValueForKey(pendingNextKey);
|
|
}
|
|
}
|
|
childMapping[nextKey] = getValueForKey(nextKey);
|
|
}
|
|
|
|
// Finally, add the keys which didn't appear before any key in `next`
|
|
for (i = 0; i < pendingKeys.length; i++) {
|
|
childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]);
|
|
}
|
|
|
|
return childMapping;
|
|
} |