Skip to content

Commit f343dcb

Browse files
committed
fix component is func bug
1 parent 867d680 commit f343dcb

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-tween-one",
3-
"version": "2.7.0",
3+
"version": "2.7.1",
44
"description": "tween-one anim component for react",
55
"keywords": [
66
"react",
@@ -71,7 +71,6 @@
7171
"core-js": "^2.5.7",
7272
"expect.js": "0.3.x",
7373
"precommit-hook": "^3.0.0",
74-
"rc-queue-anim": "^1.6.1",
7574
"rc-scroll-anim": "2.x",
7675
"rc-test": "6.x",
7776
"rc-tools": "8.x",

src/TweenOne.jsx

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { objectEqual } from './util';
77
import Tween from './Tween';
88
import ticker from './ticker';
99

10-
function noop() {
11-
}
10+
function noop() {}
1211

1312
const perFrame = Math.round(1000 / 60);
1413
const objectOrArray = PropTypes.oneOfType([PropTypes.object, PropTypes.array]);
@@ -65,7 +64,6 @@ class TweenOne extends Component {
6564
// 跳帧事件 moment;
6665
const nextMoment = props.moment;
6766
if (typeof nextMoment === 'number' && nextMoment !== prevProps.moment) {
68-
6967
if ($self.tween && !$self.updateAnim) {
7068
$self.startMoment = nextMoment;
7169
$self.startTime = ticker.time;
@@ -105,16 +103,19 @@ class TweenOne extends Component {
105103
if (!styleEqual) {
106104
// 在动画时更改了 style, 作为更改开始数值。
107105
if ($self.tween) {
108-
$self.tween.reStart(props.style, prevProps.style,
109-
$self.tween.progressTime < $self.tween.totalTime);
106+
$self.tween.reStart(
107+
props.style,
108+
prevProps.style,
109+
$self.tween.progressTime < $self.tween.totalTime,
110+
);
110111
if ($self.paused) {
111112
$self.raf();
112113
}
113114
}
114115
}
115116
$self.setForcedJudg(props);
116117
}
117-
return nextState;// eslint-disable-line
118+
return nextState; // eslint-disable-line
118119
}
119120

120121
constructor(props) {
@@ -132,12 +133,14 @@ class TweenOne extends Component {
132133
}
133134

134135
componentDidMount() {
136+
this.setDom()
135137
if (this.dom && this.dom.nodeName !== '#text') {
136138
this.start();
137139
}
138140
}
139141

140142
componentDidUpdate() {
143+
this.setDom();
141144
// 样式更新了后再执行动画;
142145
if (this.updateAnim && this.dom && this.dom.nodeName !== '#text') {
143146
if (this.tween) {
@@ -166,7 +169,7 @@ class TweenOne extends Component {
166169
* 暂时方案: 在组件里添加判断用的值。
167170
*/
168171

169-
setForcedJudg = (props) => {
172+
setForcedJudg = props => {
170173
Object.keys(this.forced).forEach(key => {
171174
delete this[key];
172175
delete this.forced[key];
@@ -179,12 +182,16 @@ class TweenOne extends Component {
179182
}
180183
});
181184
}
182-
}
185+
};
183186

184-
setDefault = (props) => {
187+
setDefault = props => {
185188
this.moment = props.moment || 0;
186189
this.startMoment = props.moment || 0;
187190
this.startTime = ticker.time;
191+
};
192+
193+
setDom = () => {
194+
this.dom = this.dom || ReactDom.findDOMNode(this);
188195
}
189196

190197
restart = () => {
@@ -197,15 +204,14 @@ class TweenOne extends Component {
197204
this.tween.reverseStartTime = this.startMoment;
198205
this.raf();
199206
this.play();
200-
}
207+
};
201208

202209
start = () => {
203210
this.updateAnim = false;
204211
const props = this.props;
205212
if (props.animation && Object.keys(props.animation).length) {
206213
this.setDefault(props);
207-
this.tween = new Tween(this.dom, props.animation,
208-
props.attr);
214+
this.tween = new Tween(this.dom, props.animation, props.attr);
209215
this.tween.reverse = this.reverse;
210216
// 预先注册 raf, 初始动画数值。
211217
this.raf();
@@ -214,15 +220,15 @@ class TweenOne extends Component {
214220
} else {
215221
this.tween = null;
216222
}
217-
}
223+
};
218224

219225
play = () => {
220226
this.cancelRequestAnimationFrame();
221227
if (this.paused) {
222228
return;
223229
}
224230
this.rafID = ticker.add(this.raf);
225-
}
231+
};
226232

227233
frame = () => {
228234
const { yoyo } = this.props;
@@ -238,14 +244,15 @@ class TweenOne extends Component {
238244
let repeatNum = Math.floor(moment / this.tween.totalTime) || 0;
239245
repeatNum = repeatNum > repeat ? repeat : repeatNum;
240246
let tweenMoment = moment - this.tween.totalTime * repeatNum;
241-
tweenMoment = tweenMoment < perFrame && !this.reverse &&
242-
totalTime >= perFrame ? 0 : tweenMoment;
247+
tweenMoment =
248+
tweenMoment < perFrame && !this.reverse && totalTime >= perFrame ? 0 : tweenMoment;
243249
if (repeat && moment && moment - this.tween.totalTime * repeatNum < perFrame) {
244250
// 在重置样式之前补 complete;
245251
this.tween.frame(this.tween.totalTime * repeatNum);
246252
}
247-
if (moment < this.moment && !this.reverse ||
248-
repeat !== 0 && repeatNum && repeatNum !== this.repeatNum
253+
if (
254+
(moment < this.moment && !this.reverse) ||
255+
(repeat !== 0 && repeatNum && repeatNum !== this.repeatNum)
249256
) {
250257
// 在 form 状态下,暂停时拉 moment 时,start 有值,,往返方向播放时,在 delay 的时间没有处理。。
251258
// 与上面的处理一样,删除 start ,重新走一遍 start。。
@@ -256,22 +263,18 @@ class TweenOne extends Component {
256263
if (yoyoReverse) {
257264
tweenMoment = this.tween.totalTime - tweenMoment;
258265
}
259-
this.tween.onChange = (e) => {
266+
this.tween.onChange = e => {
260267
const cb = {
261268
...e,
262269
timelineMode: '',
263270
};
264271

265272
if (
266-
(this.moment === this.startMoment &&
267-
(!this.reverse && !e.index && e.mode === 'onStart') ||
268-
this.reverse)
273+
(this.moment === this.startMoment && !this.reverse && !e.index && e.mode === 'onStart') ||
274+
this.reverse
269275
) {
270276
cb.timelineMode = 'onTimelineStart';
271-
} else if (
272-
moment >= totalTime && !this.reverse ||
273-
!moment && this.reverse
274-
) {
277+
} else if ((moment >= totalTime && !this.reverse) || (!moment && this.reverse)) {
275278
cb.timelineMode = 'onTimelineComplete';
276279
} else if (repeatNum !== this.timelineRepeatNum) {
277280
cb.timelineMode = 'onTimelineRepeat';
@@ -284,7 +287,7 @@ class TweenOne extends Component {
284287
this.moment = moment;
285288
this.repeatNum = repeatNum;
286289
this.tween.frame(tweenMoment);
287-
}
290+
};
288291

289292
raf = () => {
290293
const tween = this.tween;
@@ -295,18 +298,20 @@ class TweenOne extends Component {
295298
}
296299
const { repeat } = this.props;
297300
const totalTime = repeat === -1 ? Number.MAX_VALUE : this.tween.totalTime * (repeat + 1);
298-
if ((this.moment >= totalTime && !this.reverse)
299-
|| this.paused || (this.reverse && this.moment === 0)
301+
if (
302+
(this.moment >= totalTime && !this.reverse) ||
303+
this.paused ||
304+
(this.reverse && this.moment === 0)
300305
) {
301306
return this.cancelRequestAnimationFrame();
302307
}
303308
return null;
304-
}
309+
};
305310

306311
cancelRequestAnimationFrame = () => {
307312
ticker.clear(this.rafID);
308313
this.rafID = -1;
309-
}
314+
};
310315

311316
render() {
312317
const {
@@ -333,8 +338,8 @@ class TweenOne extends Component {
333338
});
334339
// component 为空时调用子级的。。
335340
const { className, children } = props;
336-
const ref = (c) => {
337-
this.dom = c && c.props ? ReactDom.findDOMNode(c) : c;
341+
const ref = c => {
342+
this.dom = c;
338343
};
339344
if (!component && typeof children !== 'string') {
340345
if (!children) {
@@ -345,16 +350,16 @@ class TweenOne extends Component {
345350
// 合并 style 与 className。
346351
const newStyle = { ...childStyle, ...props.style };
347352
const newClassName = className ? `${className} ${childClass}` : childClass;
348-
return React.cloneElement(children, {
353+
return React.cloneElement(children, {
349354
style: newStyle,
350355
ref,
351356
className: newClassName,
352357
});
353358
}
354359
return React.createElement(component, {
355-
ref,
360+
ref: typeof component === 'string' ? ref : undefined,
356361
...props,
357-
...componentProps
362+
...componentProps,
358363
});
359364
}
360365
}

0 commit comments

Comments
 (0)