|
|
import { Children, cloneElement, isValidElement } from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getChildMapping(children, mapFn) { |
|
|
let mapper = (child) => |
|
|
mapFn && isValidElement(child) ? mapFn(child) : child; |
|
|
|
|
|
let result = Object.create(null); |
|
|
if (children) |
|
|
Children.map(children, (c) => c).forEach((child) => { |
|
|
|
|
|
result[child.key] = mapper(child); |
|
|
}); |
|
|
return result; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function mergeChildMappings(prev, next) { |
|
|
prev = prev || {}; |
|
|
next = next || {}; |
|
|
|
|
|
function getValueForKey(key) { |
|
|
return key in next ? next[key] : prev[key]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let nextKeysPending = Object.create(null); |
|
|
|
|
|
let pendingKeys = []; |
|
|
for (let prevKey in prev) { |
|
|
if (prevKey in next) { |
|
|
if (pendingKeys.length) { |
|
|
nextKeysPending[prevKey] = pendingKeys; |
|
|
pendingKeys = []; |
|
|
} |
|
|
} else { |
|
|
pendingKeys.push(prevKey); |
|
|
} |
|
|
} |
|
|
|
|
|
let i; |
|
|
let childMapping = {}; |
|
|
for (let nextKey in next) { |
|
|
if (nextKeysPending[nextKey]) { |
|
|
for (i = 0; i < nextKeysPending[nextKey].length; i++) { |
|
|
let pendingNextKey = nextKeysPending[nextKey][i]; |
|
|
childMapping[nextKeysPending[nextKey][i]] = |
|
|
getValueForKey(pendingNextKey); |
|
|
} |
|
|
} |
|
|
childMapping[nextKey] = getValueForKey(nextKey); |
|
|
} |
|
|
|
|
|
|
|
|
for (i = 0; i < pendingKeys.length; i++) { |
|
|
childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]); |
|
|
} |
|
|
|
|
|
return childMapping; |
|
|
} |
|
|
|
|
|
function getProp(child, prop, props) { |
|
|
return props[prop] != null ? props[prop] : child.props[prop]; |
|
|
} |
|
|
|
|
|
export function getInitialChildMapping(props, onExited) { |
|
|
return getChildMapping(props.children, (child) => { |
|
|
return cloneElement(child, { |
|
|
onExited: onExited.bind(null, child), |
|
|
in: true, |
|
|
appear: getProp(child, 'appear', props), |
|
|
enter: getProp(child, 'enter', props), |
|
|
exit: getProp(child, 'exit', props), |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
export function getNextChildMapping(nextProps, prevChildMapping, onExited) { |
|
|
let nextChildMapping = getChildMapping(nextProps.children); |
|
|
let children = mergeChildMappings(prevChildMapping, nextChildMapping); |
|
|
|
|
|
Object.keys(children).forEach((key) => { |
|
|
let child = children[key]; |
|
|
|
|
|
if (!isValidElement(child)) return; |
|
|
|
|
|
const hasPrev = key in prevChildMapping; |
|
|
const hasNext = key in nextChildMapping; |
|
|
|
|
|
const prevChild = prevChildMapping[key]; |
|
|
const isLeaving = isValidElement(prevChild) && !prevChild.props.in; |
|
|
|
|
|
|
|
|
if (hasNext && (!hasPrev || isLeaving)) { |
|
|
|
|
|
children[key] = cloneElement(child, { |
|
|
onExited: onExited.bind(null, child), |
|
|
in: true, |
|
|
exit: getProp(child, 'exit', nextProps), |
|
|
enter: getProp(child, 'enter', nextProps), |
|
|
}); |
|
|
} else if (!hasNext && hasPrev && !isLeaving) { |
|
|
|
|
|
|
|
|
children[key] = cloneElement(child, { in: false }); |
|
|
} else if (hasNext && hasPrev && isValidElement(prevChild)) { |
|
|
|
|
|
|
|
|
|
|
|
children[key] = cloneElement(child, { |
|
|
onExited: onExited.bind(null, child), |
|
|
in: prevChild.props.in, |
|
|
exit: getProp(child, 'exit', nextProps), |
|
|
enter: getProp(child, 'enter', nextProps), |
|
|
}); |
|
|
} |
|
|
}); |
|
|
|
|
|
return children; |
|
|
} |
|
|
|