/
TransitionGroup.js
245 lines (214 loc) · 7.26 KB
/
TransitionGroup.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import PropTypes from 'prop-types';
import React, { cloneElement, isValidElement } from 'react';
import { getChildMapping, mergeChildMappings } from './utils/ChildMapping';
const values = Object.values || (obj => Object.keys(obj).map(k => obj[k]));
const propTypes = {
/**
* `<TransitionGroup>` renders a `<div>` by default. You can change this
* behavior by providing a `component` prop.
*/
component: PropTypes.any,
/**
* A set of `<Transition>` components, that are toggled `in` and out as they
* leave. the `<TransitionGroup>` will inject specific transition props, so
* remember to spread them throguh if you are wrapping the `<Transition>` as
* with our `<Fade>` example.
*/
children: PropTypes.node,
/**
* A convenience prop that enables or disabled appear animations
* for all children. Note that specifiying this will override any defaults set
* on individual children Transitions.
*/
appear: PropTypes.bool,
/**
* A convenience prop that enables or disabled enter animations
* for all children. Note that specifiying this will override any defaults set
* on individual children Transitions.
*/
enter: PropTypes.bool,
/**
* A convenience prop that enables or disabled exit animations
* for all children. Note that specifiying this will override any defaults set
* on individual children Transitions.
*/
exit: PropTypes.bool,
/**
* You may need to apply reactive updates to a child as it is exiting.
* This is generally done by using `cloneElement` however in the case of an exiting
* child the element has already been removed and not accessible to the consumer.
*
* If you do need to update a child as it leaves you can provide a `childFactory`
* to wrap every child, even the ones that are leaving.
*
* @type Function(child: ReactElement) -> ReactElement
*/
childFactory: PropTypes.func,
};
const defaultProps = {
component: 'div',
childFactory: child => child,
};
/**
* The `<TransitionGroup>` component manages a set of `<Transition>` components
* in a list. Like with the `<Transition>` component, `<TransitionGroup>`, is a
* state machine for managing the mounting and unmounting of components over
* time.
*
* Consider the example below using the `Fade` CSS transition from before.
* As items are removed or added to the TodoList the `in` prop is toggled
* automatically by the `<TransitionGroup>`. You can use _any_ `<Transition>`
* component in a `<TransitionGroup>`, not just css.
*
* ```jsx
* import TransitionGroup from 'react-transition-group/TransitionGroup';
*
* class TodoList extends React.Component {
* constructor(props) {
* super(props)
* this.state = {items: ['hello', 'world', 'click', 'me']}
* }
* handleAdd() {
* const newItems = this.state.items.concat([
* prompt('Enter some text')
* ]);
* this.setState({ items: newItems });
* }
* handleRemove(i) {
* let newItems = this.state.items.slice();
* newItems.splice(i, 1);
* this.setState({items: newItems});
* }
* render() {
* return (
* <div>
* <button onClick={() => this.handleAdd()}>Add Item</button>
* <TransitionGroup>
* {this.state.items.map((item, i) => (
* <FadeTransition key={item}>
* <div>
* {item}{' '}
* <button onClick={() => this.handleRemove(i)}>
* remove
* </button>
* </div>
* </FadeTransition>
* ))}
* </TransitionGroup>
* </div>
* );
* }
* }
* ```
*
* Note that `<TransitionGroup>` does not define any animation behavior!
* Exactly _how_ a list item animates is up to the individual `<Transition>`
* components. This means you can mix and match animations across different
* list items.
*/
class TransitionGroup extends React.Component {
static childContextTypes = {
transitionGroup: PropTypes.object.isRequired,
};
constructor(props, context) {
super(props, context);
// Initial children should all be entering, dependent on appear
this.state = {
children: getChildMapping(props.children, child => {
const onExited = (node) => {
this.handleExited(child.key, node, child.props.onExited);
}
return cloneElement(child, {
onExited,
in: true,
appear: this.getProp(child, 'appear'),
enter: this.getProp(child, 'enter'),
exit: this.getProp(child, 'exit'),
})
}),
};
}
getChildContext() {
return {
transitionGroup: { isMounting: !this.appeared }
}
}
// use child config unless explictly set by the Group
getProp(child, prop, props = this.props) {
return props[prop] != null ?
props[prop] :
child.props[prop];
}
componentDidMount() {
this.appeared = true;
}
componentWillReceiveProps(nextProps) {
let prevChildMapping = this.state.children;
let nextChildMapping = getChildMapping(nextProps.children);
let children = mergeChildMappings(prevChildMapping, nextChildMapping);
Object.keys(children).forEach((key) => {
let child = children[key]
if (!isValidElement(child)) return;
const onExited = (node) => {
this.handleExited(child.key, node, child.props.onExited);
}
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,
in: true,
exit: this.getProp(child, 'exit', nextProps),
enter: this.getProp(child, 'enter', nextProps),
});
}
// item is old (exiting)
else if (!hasNext && hasPrev && !isLeaving) {
// console.log('leaving', key)
children[key] = cloneElement(child, { in: false });
}
// item hasn't changed transition states
// copy over the last transition props;
else if (hasNext && hasPrev && isValidElement(prevChild)) {
// console.log('unchanged', key)
children[key] = cloneElement(child, {
onExited,
in: prevChild.props.in,
exit: this.getProp(child, 'exit', nextProps),
enter: this.getProp(child, 'enter', nextProps),
});
}
})
this.setState({ children });
}
handleExited = (key, node, originalHandler) => {
let currentChildMapping = getChildMapping(this.props.children);
if (key in currentChildMapping) return
if (originalHandler)
originalHandler(node)
this.setState((state) => {
let children = { ...state.children };
delete children[key];
return { children };
});
};
render() {
const { component: Component, childFactory, ...props } = this.props;
const { children } = this.state;
delete props.appear;
delete props.enter;
delete props.exit;
return (
<Component {...props}>
{values(children).map(childFactory)}
</Component>
);
}
}
TransitionGroup.propTypes = propTypes;
TransitionGroup.defaultProps = defaultProps;
export default TransitionGroup;