-
Notifications
You must be signed in to change notification settings - Fork 651
/
Copy pathChildMapping.ts
168 lines (150 loc) · 5.37 KB
/
ChildMapping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Children, cloneElement, isValidElement } from 'react';
import type { ReactChild, ReactElement, ReactNode } from 'react';
/**
* Given `this.props.children`, return an object mapping key to child.
*
* @param {*} children `this.props.children`
* @return {object} Mapping of key to child
*/
export function getChildMapping(
children: ReactNode[] | ReactNode,
mapFn?: (child: ReactElement) => ReactElement
) {
let mapper = (child: ReactNode) =>
mapFn && isValidElement(child) ? mapFn(child) : child;
let result = Object.create(null);
if (children)
// @ts-expect-error FIXME: Object is possibly 'null' or 'undefined'.ts(2533)
Children.map(children, (c) => c).forEach((child) => {
// run the map function here instead so that the key is the computed one
// @ts-expect-error FIXME: Property 'key' does not exist on type 'string'.ts(2339)
result[child.key] = mapper(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.
*/
export function mergeChildMappings(
prev_: Record<string, ReactChild> | undefined,
next_: Record<string, ReactChild> | undefined
) {
const prev = prev_ || {};
const next = next_ || {};
function getValueForKey(key: string) {
return key in next ? next[key] : prev[key];
}
// For each key of `next`, the list of keys to insert before that key in
// the combined list
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: Record<string, ReactChild> = {};
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);
}
// 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;
}
function getProp(
child: ReactElement,
prop: string,
props: Record<string, unknown>
) {
return props[prop] != null ? props[prop] : child.props[prop];
}
export function getInitialChildMapping(
props: { children: ReactNode[] },
onExited: (
child: ReactElement<{ onExited: (node: HTMLElement) => void }>,
node: HTMLElement
) => void
) {
return getChildMapping(props.children, (child: ReactElement) => {
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: { children: ReactNode[] },
prevChildMapping: Record<string, ReactChild>,
onExited: (
child: ReactElement<{ onExited: (node: HTMLElement) => void }>,
node: HTMLElement
) => void
) {
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;
// item is new (entering)
if (hasNext && (!hasPrev || isLeaving)) {
// console.log('entering', key)
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) {
// item is old (exiting)
// console.log('leaving', key)
children[key] = cloneElement(child, { in: false });
} else if (hasNext && hasPrev && isValidElement(prevChild)) {
// item hasn't changed transition states
// copy over the last transition props;
// console.log('unchanged', key)
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;
}