@@ -21,20 +21,9 @@ class TweenOneGroup extends Component {
2121 this . isTween = { } ;
2222 // 第一进入,appear 为 true 时默认用 enter 或 tween-one 上的效果
2323 const children = toArrayChildren ( getChildrenFromProps ( this . props ) ) ;
24- children . forEach ( child => {
25- if ( ! child || ! child . key ) {
26- return ;
27- }
28- this . keysToEnter . push ( child . key ) ;
29- } ) ;
3024 this . state = {
3125 children,
3226 } ;
33- [
34- 'getChildrenToRender' ,
35- 'getCoverAnimation' ,
36- 'onChange' ,
37- ] . forEach ( ( method ) => this [ method ] = this [ method ] . bind ( this ) ) ;
3827 }
3928
4029 componentDidMount ( ) {
@@ -43,7 +32,7 @@ class TweenOneGroup extends Component {
4332
4433 componentWillReceiveProps ( nextProps ) {
4534 const nextChildren = toArrayChildren ( nextProps . children ) ;
46- const currentChildren = this . state . children ;
35+ const currentChildren = toArrayChildren ( this . state . children ) ;
4736 const newChildren = mergeChildren ( currentChildren , nextChildren ) ;
4837
4938 this . keysToEnter = [ ] ;
@@ -74,27 +63,27 @@ class TweenOneGroup extends Component {
7463 } ) ;
7564 }
7665
77- onChange ( animation , key , type , obj ) {
66+ onChange = ( animation , key , type , obj ) => {
7867 const length = dataToArray ( animation ) . length ;
7968 const animatingClassName = this . props . animatingClassName ;
8069 const tag = obj . target ;
70+ const isEnter = type === 'enter' || type === 'appear' ;
8171 if ( obj . mode === 'onStart' ) {
8272 tag . className = tag . className
83- . replace ( animatingClassName [ type === 'enter' ? 1 : 0 ] , '' ) . trim ( ) ;
84- if ( tag . className . indexOf ( animatingClassName [ type === 'enter' ? 0 : 1 ] ) === - 1 ) {
85- tag . className = `${ tag . className } ${ animatingClassName [ type === 'enter' ? 0 : 1 ] } ` . trim ( ) ;
73+ . replace ( animatingClassName [ isEnter ? 1 : 0 ] , '' ) . trim ( ) ;
74+ if ( tag . className . indexOf ( animatingClassName [ isEnter ? 0 : 1 ] ) === - 1 ) {
75+ tag . className = `${ tag . className } ${ animatingClassName [ isEnter ? 0 : 1 ] } ` . trim ( ) ;
8676 }
8777 } else if ( obj . index === length - 1 && obj . mode === 'onComplete' ) {
88- let children ;
78+ let children = this . state . children ;
8979 if ( type === 'enter' ) {
90- children = this . state . children ;
9180 this . keysToEnter . splice ( this . keysToEnter . indexOf ( key ) , 1 ) ;
92- } else {
81+ } else if ( type === 'leave' ) {
9382 children = this . state . children . filter ( child => key !== child . key ) ;
9483 this . keysToLeave . splice ( this . keysToLeave . indexOf ( key ) , 1 ) ;
9584 }
9685 tag . className = tag . className
97- . replace ( animatingClassName [ type === 'enter' ? 0 : 1 ] , '' ) . trim ( ) ;
86+ . replace ( animatingClassName [ isEnter ? 0 : 1 ] , '' ) . trim ( ) ;
9887 delete this . isTween [ key ] ;
9988 this . setState ( {
10089 children,
@@ -104,38 +93,46 @@ class TweenOneGroup extends Component {
10493 }
10594 }
10695
107- getCoverAnimation ( child , i , type ) {
96+ getCoverAnimation = ( child , i , type ) => {
10897 let animation ;
10998 let onChange ;
110- const appear = transformArguments ( this . props . appear , child . key , i ) ;
111- if ( appear || this . onEnterBool ) {
112- animation = type === 'leave' ? this . props . leave : this . props . enter ;
113- onChange = this . onChange . bind ( this , animation , child . key , type ) ;
99+ animation = type === 'leave' ? this . props . leave : this . props . enter ;
100+ if ( type === 'appear' ) {
101+ const appear = transformArguments ( this . props . appear , child . key , i ) ;
102+ animation = appear && this . props . enter || null ;
114103 }
104+ onChange = this . onChange . bind ( this , animation , child . key , type ) ;
115105 const children = ( < TweenOne
116106 { ...child . props }
117107 key = { child . key }
118108 component = { child . type }
119109 animation = { transformArguments ( animation , child . key , i ) }
120110 onChange = { onChange }
121- resetStyleBool = { child . key in this . isTween }
111+ resetStyleBool = { this . props . resetStyleBool }
122112 /> ) ;
123- if ( this . keysToEnter . concat ( this . keysToLeave ) . indexOf ( child . key ) >= 0 ) {
124- this . isTween [ child . key ] = true ;
113+ if ( this . keysToEnter . concat ( this . keysToLeave ) . indexOf ( child . key ) >= 0
114+ || ! this . onEnterBool && animation ) {
115+ this . isTween [ child . key ] = type ;
125116 }
126117 return children ;
127118 }
128119
129- getChildrenToRender ( children ) {
120+ getChildrenToRender = children => {
130121 return children . map ( ( child , i ) => {
131122 if ( ! child || ! child . key ) {
132123 return child ;
133124 }
134125 const key = child . key ;
135126 if ( this . keysToLeave . indexOf ( key ) >= 0 ) {
136127 return this . getCoverAnimation ( child , i , 'leave' ) ;
128+ } else if ( this . keysToEnter . indexOf ( key ) >= 0 ) {
129+ return this . getCoverAnimation ( child , i , 'enter' ) ;
130+ } else if ( ! this . onEnterBool ) {
131+ return this . getCoverAnimation ( child , i , 'appear' ) ;
137132 }
138- return this . getCoverAnimation ( child , i , 'enter' ) ;
133+ return this . isTween [ child . key ] &&
134+ this . getCoverAnimation ( child , i , this . isTween [ child . key ] ) ||
135+ React . createElement ( TweenOne , { ...child . props , component : child . type , key : child . key } ) ;
139136 } ) ;
140137 }
141138
@@ -152,6 +149,7 @@ class TweenOneGroup extends Component {
152149 'leave' ,
153150 'animatingClassName' ,
154151 'onEnd' ,
152+ 'resetStyleBool' ,
155153 ] . forEach ( key => delete componentProps [ key ] ) ;
156154 return createElement ( this . props . component , componentProps , childrenToRender ) ;
157155 }
@@ -169,6 +167,7 @@ TweenOneGroup.propTypes = {
169167 leave : objectOrArrayOrFunc ,
170168 animatingClassName : PropTypes . array ,
171169 onEnd : PropTypes . func ,
170+ resetStyleBool : PropTypes . bool ,
172171} ;
173172
174173TweenOneGroup . defaultProps = {
@@ -178,5 +177,6 @@ TweenOneGroup.defaultProps = {
178177 enter : { x : 50 , opacity : 0 , type : 'from' } ,
179178 leave : { x : - 50 , opacity : 0 } ,
180179 onEnd : noop ,
180+ resetStyleBool : true ,
181181} ;
182182export default TweenOneGroup ;
0 commit comments