1
1
import React , { Component } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import ReactDom from 'react-dom' ;
4
+ import { polyfill } from 'react-lifecycles-compat' ;
4
5
5
6
import { objectEqual } from './util' ;
6
7
import Tween from './Tween' ;
@@ -39,6 +40,83 @@ class TweenOne extends Component {
39
40
attr : 'style' ,
40
41
onChange : noop ,
41
42
} ;
43
+
44
+ static getDerivedStateFromProps ( props , { prevProps, $self } ) {
45
+ const nextState = {
46
+ prevProps : props ,
47
+ } ;
48
+ if ( prevProps ) {
49
+ if ( ! $self . tween && ! $self . dom ) {
50
+ $self . updateAnim = true ;
51
+ return ;
52
+ }
53
+
54
+ // 动画处理
55
+ const newAnimation = props . animation ;
56
+ const currentAnimation = prevProps . animation ;
57
+ const equal = objectEqual ( currentAnimation , newAnimation ) ;
58
+ if ( ! equal ) {
59
+ if ( props . resetStyle && $self . tween ) {
60
+ $self . tween . resetDefaultStyle ( ) ;
61
+ }
62
+ $self . updateAnim = true ;
63
+ }
64
+
65
+ // 跳帧事件 moment;
66
+ const nextMoment = props . moment ;
67
+ if ( typeof nextMoment === 'number' && nextMoment !== prevProps . moment ) {
68
+
69
+ if ( $self . tween && ! $self . updateAnim ) {
70
+ $self . startMoment = nextMoment ;
71
+ $self . startTime = ticker . time ;
72
+ if ( props . paused ) {
73
+ $self . raf ( ) ;
74
+ }
75
+ if ( $self . tween . progressTime >= $self . tween . totalTime ) {
76
+ $self . play ( ) ;
77
+ }
78
+ } else {
79
+ $self . updateAnim = true ;
80
+ }
81
+ }
82
+
83
+ // 暂停倒放
84
+ if ( $self . paused !== props . paused || $self . reverse !== props . reverse ) {
85
+ $self . paused = props . paused ;
86
+ $self . reverse = props . reverse ;
87
+ if ( $self . paused ) {
88
+ $self . cancelRequestAnimationFrame ( ) ;
89
+ } else if ( $self . reverse && props . reverseDelay ) {
90
+ $self . cancelRequestAnimationFrame ( ) ;
91
+ ticker . timeout ( $self . restart , props . reverseDelay ) ;
92
+ } else {
93
+ // 在 form 状态下,暂停时拉 moment 时,start 有值恢复播放,在 delay 的时间没有处理。。
94
+ if ( $self . tween ) {
95
+ $self . tween . resetAnimData ( ) ;
96
+ $self . tween . resetDefaultStyle ( ) ;
97
+ }
98
+ if ( ! $self . updateAnim ) {
99
+ $self . restart ( ) ;
100
+ }
101
+ }
102
+ }
103
+
104
+ const styleEqual = objectEqual ( prevProps . style , props . style ) ;
105
+ if ( ! styleEqual ) {
106
+ // 在动画时更改了 style, 作为更改开始数值。
107
+ if ( $self . tween ) {
108
+ $self . tween . reStart ( props . style , prevProps . style ,
109
+ $self . tween . progressTime < $self . tween . totalTime ) ;
110
+ if ( $self . paused ) {
111
+ $self . raf ( ) ;
112
+ }
113
+ }
114
+ }
115
+ $self . setForcedJudg ( props ) ;
116
+ }
117
+ return nextState ; // eslint-disable-line
118
+ }
119
+
42
120
constructor ( props ) {
43
121
super ( props ) ;
44
122
this . rafID = - 1 ;
@@ -49,6 +127,9 @@ class TweenOne extends Component {
49
127
this . forced = { } ;
50
128
this . currentRef = null ;
51
129
this . setForcedJudg ( props ) ;
130
+ this . state = {
131
+ $self : this ,
132
+ } ;
52
133
}
53
134
54
135
componentDidMount ( ) {
@@ -58,76 +139,6 @@ class TweenOne extends Component {
58
139
}
59
140
}
60
141
61
- componentWillReceiveProps ( nextProps ) {
62
- if ( ! this . tween && ! this . dom ) {
63
- this . updateAnim = true ;
64
- return ;
65
- }
66
-
67
- // 动画处理
68
- const newAnimation = nextProps . animation ;
69
- const currentAnimation = this . props . animation ;
70
- const equal = objectEqual ( currentAnimation , newAnimation ) ;
71
- if ( ! equal ) {
72
- if ( nextProps . resetStyle && this . tween ) {
73
- this . tween . resetDefaultStyle ( ) ;
74
- }
75
- this . updateAnim = true ;
76
- }
77
-
78
- // 跳帧事件 moment;
79
- const nextMoment = nextProps . moment ;
80
- if ( typeof nextMoment === 'number' && nextMoment !== this . props . moment ) {
81
- if ( this . tween && ! this . updateAnim ) {
82
- this . startMoment = nextMoment ;
83
- this . startTime = ticker . time ;
84
- if ( nextProps . paused ) {
85
- this . raf ( ) ;
86
- }
87
- if ( this . tween . progressTime >= this . tween . totalTime ) {
88
- this . play ( ) ;
89
- }
90
- } else {
91
-
92
- this . updateAnim = true ;
93
- }
94
- }
95
-
96
- // 暂停倒放
97
- if ( this . paused !== nextProps . paused || this . reverse !== nextProps . reverse ) {
98
- this . paused = nextProps . paused ;
99
- this . reverse = nextProps . reverse ;
100
- if ( this . paused ) {
101
- this . cancelRequestAnimationFrame ( ) ;
102
- } else if ( this . reverse && nextProps . reverseDelay ) {
103
- this . cancelRequestAnimationFrame ( ) ;
104
- ticker . timeout ( this . restart , nextProps . reverseDelay ) ;
105
- } else {
106
- // 在 form 状态下,暂停时拉 moment 时,start 有值恢复播放,在 delay 的时间没有处理。。
107
- if ( this . tween ) {
108
- this . tween . resetAnimData ( ) ;
109
- this . tween . resetDefaultStyle ( ) ;
110
- }
111
- if ( ! this . updateAnim ) {
112
- this . restart ( ) ;
113
- }
114
- }
115
- }
116
-
117
- const styleEqual = objectEqual ( this . props . style , nextProps . style ) ;
118
- if ( ! styleEqual ) {
119
- // 在动画时更改了 style, 作为更改开始数值。
120
- if ( this . tween ) {
121
- this . tween . reStart ( nextProps . style , this . props . style ,
122
- this . tween . progressTime < this . tween . totalTime ) ;
123
- if ( this . paused ) {
124
- this . raf ( ) ;
125
- }
126
- }
127
- }
128
- this . setForcedJudg ( nextProps ) ;
129
- }
130
-
131
142
componentDidUpdate ( ) {
132
143
if ( ! this . dom ) {
133
144
this . dom = ReactDom . findDOMNode ( this ) ;
@@ -348,4 +359,4 @@ class TweenOne extends Component {
348
359
}
349
360
}
350
361
TweenOne . isTweenOne = true ;
351
- export default TweenOne ;
362
+ export default polyfill ( TweenOne ) ;
0 commit comments