11import React from 'react'
2+ import ReactDOM from 'react-dom'
23import PropTypes from 'prop-types'
34import Animated from './animated/targets/react-dom'
45import SpringAnimation from './animated/SpringAnimation'
@@ -40,34 +41,46 @@ export default class Spring extends React.PureComponent {
4041 impl : SpringAnimation ,
4142 }
4243
43- constructor ( props ) {
44- super ( )
45- this . _defaultAnimation = new Animated . Value ( 0 )
46- this . _animations = { }
47- this . _updateProps ( props , false )
44+ state = { props : undefined }
45+ defaultAnimation = new Animated . Value ( 0 )
46+ animations = { }
47+
48+ componentWillUnmount ( ) {
49+ this . stop ( )
4850 }
4951
50- _updateProps ( { impl, from, to, config, attach, immediate, reset, onFrame, onRest } , start = false ) {
52+ componentWillMount ( ) {
53+ this . updateProps ( this . props )
54+ }
55+
56+ componentWillReceiveProps ( props ) {
57+ this . updateProps ( props )
58+ }
59+
60+ async updateProps ( { inject, ...props } ) {
61+ const { impl, from, to, config, attach, immediate, reset, onFrame, onRest } = inject ? await inject ( this , props ) : props
62+
5163 const allProps = Object . entries ( { ...from , ...to } )
52- const defaultAnimationValue = this . _defaultAnimation . _value
64+ const defaultAnimationValue = this . defaultAnimation . _value
65+
66+ this . defaultAnimation . setValue ( 0 )
67+ this . interpolators = { }
68+ this . animations = allProps . reduce ( ( acc , [ name , value ] , i ) => {
69+ const entry = ( reset === false && this . animations [ name ] ) || ( this . animations [ name ] = { } )
5370
54- this . _interpolators = { }
55- this . _defaultAnimation . setValue ( 0 )
56- this . _animations = allProps . reduce ( ( acc , [ name , value ] , i ) => {
57- const entry = ( reset === false && this . _animations [ name ] ) || ( this . _animations [ name ] = { } )
5871 let isNumber = typeof value === 'number'
5972 let isArray = ! isNumber && Array . isArray ( value )
6073 let fromValue = from [ name ] !== undefined ? from [ name ] : value
61- let toValue = isNumber || isArray ? value : 1
74+ let toValue = isNumber || isArray ? value : 1
6275
6376 if ( isNumber && attach ) {
6477 // Attach value to target animation
6578 const target = attach ( this )
66- const targetAnimation = target && target . _animations [ name ]
79+ const targetAnimation = target && target . animations [ name ]
6780 if ( targetAnimation ) toValue = targetAnimation . animation
6881 }
6982
70- if ( isNumber ) {
83+ if ( isNumber || toValue === 'auto' ) {
7184 // Create animated value
7285 entry . animation = entry . interpolation = entry . animation || new Animated . Value ( fromValue )
7386 } else if ( isArray ) {
@@ -76,10 +89,10 @@ export default class Spring extends React.PureComponent {
7689 } else {
7790 // Deal with interpolations
7891 const previous = entry . interpolation && entry . interpolation . _interpolation ( defaultAnimationValue )
79- entry . animation = this . _defaultAnimation
80- entry . interpolation = this . _defaultAnimation . interpolate ( {
81- inputRange : [ 0 , 1 ] ,
82- outputRange : [ previous !== undefined ? previous : fromValue , value ] ,
92+ entry . animation = this . defaultAnimation
93+ entry . interpolation = this . defaultAnimation . interpolate ( {
94+ range : [ 0 , 1 ] ,
95+ output : [ previous !== undefined ? previous : fromValue , value ] ,
8396 } )
8497 }
8598
@@ -89,8 +102,8 @@ export default class Spring extends React.PureComponent {
89102 entry . start = cb => {
90103 Animated . controller ( entry . animation , { toValue, ...config } , impl ) . start ( props => {
91104 if ( props . finished ) {
92- this . _animations [ name ] . stopped = true
93- if ( Object . values ( this . _animations ) . every ( animation => animation . stopped ) ) {
105+ this . animations [ name ] . stopped = true
106+ if ( Object . values ( this . animations ) . every ( animation => animation . stopped ) ) {
94107 const current = { ...this . props . from , ...this . props . to }
95108 onRest && onRest ( current )
96109 cb && cb ( current )
@@ -103,54 +116,49 @@ export default class Spring extends React.PureComponent {
103116 entry . animation . stopAnimation ( )
104117 }
105118
106- this . _interpolators [ name ] = entry . interpolation
119+ this . interpolators [ name ] = entry . interpolation
107120 return { ...acc , [ name ] : entry }
108121 } , { } )
109122
110- if ( start ) this . start ( )
123+ const oldAnimatedProps = this . animatedProps
124+ this . animatedProps = new Animated . AnimatedProps ( this . interpolators , this . callback )
125+ oldAnimatedProps && oldAnimatedProps . __detach ( )
111126
112- var oldPropsAnimated = this . _propsAnimated
113- this . _propsAnimated = new Animated . AnimatedProps ( this . _interpolators , this . callback )
114- oldPropsAnimated && oldPropsAnimated . __detach ( )
127+ this . forceUpdate ( )
128+ this . start ( )
115129 }
116130
117- start ( props = this . props ) {
118- return new Promise ( res => Object . values ( this . _animations ) . forEach ( animation => animation . start ( res ) ) )
131+ start ( ) {
132+ return new Promise ( res => this . getAnimations ( ) . forEach ( animation => animation . start ( res ) ) )
119133 }
120134
121135 stop ( ) {
122- Object . values ( this . _animations ) . forEach ( animation => animation . stop ( ) )
123- }
124-
125- update ( props ) {
126- this . _updateProps ( { ...this . props , ...props } , true )
136+ this . getAnimations ( ) . forEach ( animation => animation . stop ( ) )
127137 }
128138
129139 callback = ( ) => {
130- if ( this . props . onFrame ) this . props . onFrame ( this . _propsAnimated . __getValue ( ) )
140+ if ( this . props . onFrame ) this . props . onFrame ( this . animatedProps . __getValue ( ) )
131141 ! this . props . native && this . forceUpdate ( )
132142 }
133143
134- componentWillReceiveProps ( props ) {
135- this . _updateProps ( props , true )
136- }
137-
138- componentDidMount ( ) {
139- this . start ( )
144+ getAnimations ( ) {
145+ return Object . values ( this . animations )
140146 }
141147
142- componentWillUnmount ( ) {
143- this . stop ( )
148+ getValues ( ) {
149+ return this . animatedProps ? this . animatedProps . __getValue ( ) : { }
144150 }
145151
146- getValues ( ) {
147- return this . _propsAnimated . __getValue ( )
152+ getAnimatedValues ( ) {
153+ return this . props . native ? this . interpolators : this . getValues ( )
148154 }
149155
150156 render ( ) {
151- const { children, render, from, to, config, native, ...extra } = this . props
152- let animatedProps = { ...( native ? this . _interpolators : this . _propsAnimated . __getValue ( ) ) , ...extra }
153- if ( render ) return render ( { ...animatedProps , children } )
154- else return Array . isArray ( children ) ? children . map ( child => child ( animatedProps ) ) : children ( animatedProps )
157+ const { children, render, from, to, config, native, inject, ...extra } = this . props
158+ const values = this . getAnimatedValues ( )
159+ if ( Object . keys ( values ) . length ) {
160+ const animatedProps = { ...this . getAnimatedValues ( ) , ...extra }
161+ return render ? render ( { ...animatedProps , children } ) : children ( animatedProps )
162+ } else return null
155163 }
156164}
0 commit comments