Skip to content

Commit

Permalink
Merge pull request #2 from jevakallio/animated-components
Browse files Browse the repository at this point in the history
Add Animated helper components
  • Loading branch information
ohanhi committed Apr 24, 2016
2 parents 7bbfa6c + 2e53c81 commit 04789af
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Animated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react-native';
const {
Animated,
PropTypes
} = React;

function createAnimationDongle(className) {
return React.createClass({
displayName: 'Cycle' + className,

propTypes: {
},

getInitialState() {
const currentValue = new Animated.Value(
this.props.initialValue || 0
);

return {
currentValue
}
},

componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.state.currentValue._value) {
this.runAnimation(nextProps);
}
},

runAnimation({animation, options = {}, value}) {
animation(this.state.currentValue, {...options,
toValue: value
}).start();
},

render() {
const AnimatedClass = Animated[className];
const { animate } = this.props;

const animatedStyle = Object.keys(animate).reduce((acc, key) => {
return {...acc,
[key]: this.state.currentValue.interpolate(animate[key])
}
}, {});

const style = {...(this.props.style || {}), ...animatedStyle}

const extraProps = {
source: this.props.source
};

return (
<AnimatedClass style={style} {...extraProps}>
{this.props.children}
</AnimatedClass>
);
}
});
};


export default {
View: createAnimationDongle('View'),
Text: createAnimationDongle('Text'),
Image: createAnimationDongle('Image')
};

0 comments on commit 04789af

Please sign in to comment.