Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import React, { PropTypes } from 'react';
  2. import { View, DeviceEventEmitter, Animated } from 'react-native';
  3.  
  4. const KeyboardAvoid = React.createClass({
  5. propTypes: {
  6. duration: PropTypes.number.isRequired,
  7. additionalHeight: PropTypes.number.isRequired,
  8. },
  9.  
  10. getInitialState() {
  11. return {
  12. height: new Animated.Value(0),
  13. };
  14. },
  15.  
  16. getDefaultProps() {
  17. return {
  18. duration: 400,
  19. additionalHeight: 50,
  20. };
  21. },
  22.  
  23. componentDidMount() {
  24. this.keyboardDidShowListener = DeviceEventEmitter.addListener('keyboardDidShow', this.keyboardDidShow);
  25. this.keyboardDidHideListener = DeviceEventEmitter.addListener('keyboardDidHide', this.keyboardDidHide);
  26. },
  27.  
  28. componentWillUnmount() {
  29. this.keyboardDidShowListener.remove();
  30. this.keyboardDidHideListener.remove();
  31. },
  32.  
  33. animate(toValue) {
  34. Animated.timing(this.state.height, { toValue, duration: this.props.duration }).start();
  35. },
  36.  
  37. keyboardDidShow(e) {
  38. this.animate(e.endCoordinates.height - this.props.additionalHeight);
  39. },
  40.  
  41. keyboardDidHide(e) {
  42. this.animate(0);
  43. },
  44.  
  45. render() {
  46. return (
  47. <Animated.View style={[this.props.style, { flex: 1, paddingBottom: this.state.height }]}>
  48. {this.props.children}
  49. </Animated.View>
  50. );
  51. },
  52. });
  53.  
  54. export default KeyboardAvoid;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement