Skip to content

Commit a373fef

Browse files
committed
improve code
1 parent 3033a72 commit a373fef

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

src/QueueAnim.jsx

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class QueueAnim extends React.Component {
2525
this.keysToEnter = [];
2626
this.keysToLeave = [];
2727
this.keysAnimating = [];
28-
29-
this.placeholder = {};
28+
this.placeholderTimeoutIds = {};
3029

3130
// 第一次进入,默认进场
3231
const children = toArrayChildren(getChildrenFromProps(this.props));
@@ -114,6 +113,9 @@ class QueueAnim extends React.Component {
114113
velocity(findDOMNode(this.refs[child.key]), 'stop');
115114
}
116115
});
116+
Object.keys(this.placeholderTimeoutIds).forEach(key => {
117+
clearTimeout(this.placeholderTimeoutIds[key]);
118+
});
117119
}
118120
}
119121

@@ -150,21 +152,12 @@ class QueueAnim extends React.Component {
150152
}
151153

152154
performEnter(key, i) {
153-
/*
154-
const placeholderNode = this.placeholder[key] || document.createElement('div');//findDOMNode(this.refs[placeholderKeyPrefix + key]);
155-
this.placeholder[key] = placeholderNode*/
156-
157155
const interval = transformArguments(this.props.interval, key, i)[0];
158156
const delay = transformArguments(this.props.delay, key, i)[0];
159-
/*
160-
placeholderNode.style.visibility = 'hidden';
161-
velocity(placeholderNode, 'stop');
162-
velocity(placeholderNode, {opacity: [0, 0]}, {
163-
delay: interval * i + delay,
164-
duration: 0,
165-
begin: this.performEnterBegin.bind(this, key, i),
166-
});*/
167-
this.placeholder[key] = setTimeout(this.performEnterBegin.bind(this, key, i), interval * i + delay);
157+
this.placeholderTimeoutIds[key] = setTimeout(
158+
this.performEnterBegin.bind(this, key, i),
159+
interval * i + delay
160+
);
168161
if (this.keysToEnter.indexOf(key) >= 0) {
169162
this.keysToEnter.splice(this.keysToEnter.indexOf(key), 1);
170163
}
@@ -173,9 +166,7 @@ class QueueAnim extends React.Component {
173166
performEnterBegin(key, i) {
174167
const childrenShow = this.state.childrenShow;
175168
childrenShow[key] = true;
176-
if (this._reactInternalInstance) {
177-
this.setState({childrenShow}, this.realPerformEnter.bind(this, key, i));
178-
}
169+
this.setState({childrenShow}, this.realPerformEnter.bind(this, key, i));
179170
}
180171

181172
realPerformEnter(key, i) {
@@ -186,7 +177,6 @@ class QueueAnim extends React.Component {
186177
const duration = transformArguments(this.props.duration, key, i)[0];
187178
node.style.visibility = 'hidden';
188179
velocity(node, 'stop');
189-
console.log('enter', key);
190180
velocity(node, this.getVelocityEnterConfig(key, i), {
191181
duration: duration,
192182
easing: this.getVelocityEasing(key, i)[0],
@@ -197,12 +187,9 @@ class QueueAnim extends React.Component {
197187
}
198188

199189
performLeave(key, i) {
190+
clearTimeout(this.placeholderTimeoutIds[key]);
191+
delete this.placeholderTimeoutIds[key];
200192
const node = findDOMNode(this.refs[key]);
201-
// console.log('stop', key);
202-
/*
203-
velocity(this.placeholder[key], 'stop');
204-
*/
205-
clearTimeout(this.placeholder[key]);
206193
if (!node) {
207194
return;
208195
}
@@ -211,7 +198,6 @@ class QueueAnim extends React.Component {
211198
const duration = transformArguments(this.props.duration, key, i)[1];
212199
const order = this.props.leaveReverse ? (this.keysToLeave.length - i - 1) : i;
213200
velocity(node, 'stop');
214-
// console.log('leave', key);
215201
velocity(node, this.getVelocityLeaveConfig(key, i), {
216202
delay: interval * order + delay,
217203
duration: duration,
@@ -252,13 +238,8 @@ class QueueAnim extends React.Component {
252238
if (this.keysToLeave.indexOf(key) >= 0) {
253239
this.keysToLeave.splice(this.keysToLeave.indexOf(key), 1);
254240
}
255-
let leaveEnd;
256-
this.keysToLeave.forEach((c)=> {
257-
if (childrenShow[c]) {
258-
leaveEnd = true;
259-
}
260-
});
261-
if (!leaveEnd) {
241+
const needLeave = this.keysToLeave.some(c => childrenShow[c]);
242+
if (!needLeave) {
262243
const currentChildren = toArrayChildren(getChildrenFromProps(this.props));
263244
this.setState({
264245
children: currentChildren,

0 commit comments

Comments
 (0)