Created
July 8, 2016 23:32
-
-
Save chirag04/6d20700b21c674c9b1aee3019e57468e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactNative from 'react-native'; | |
const { View, LayoutAnimation, Keyboard } = ReactNative; | |
class KeyboardSpacer extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
keyboardHeight: 0, | |
}; | |
} | |
componentWillMount() { | |
this.subscriptions = [ | |
Keyboard.addListener('keyboardDidShow', this.keyboardDidShow), | |
Keyboard.addListener('keyboardWillHide', this.keyboardWillHide), | |
]; | |
} | |
componentWillUnmount() { | |
this.subscriptions.forEach((sub) => sub.remove()); | |
} | |
setLayoutAnimation({ duration, easing }) { | |
if (duration) { | |
LayoutAnimation.configureNext({ | |
duration: duration, | |
update: { | |
duration: duration, | |
type: LayoutAnimation.Types[easing] || 'keyboard', | |
}, | |
}); | |
} | |
} | |
keyboardDidShow = (event) => { | |
this.setLayoutAnimation(event); | |
this.setState({ | |
keyboardHeight: event.endCoordinates.height - this.props.offset, | |
}); | |
}; | |
keyboardWillHide = (event) => { | |
this.setLayoutAnimation(event); | |
this.setState({ keyboardHeight: 0 }); | |
}; | |
render() { | |
const style={ height: this.state.keyboardHeight }; | |
return ( | |
<View style={style} /> | |
); | |
} | |
} | |
KeyboardSpacer.defaultProps = { | |
offset: 0, | |
}; | |
KeyboardSpacer.propTypes = { | |
offset: React.PropTypes.number.isRequired, | |
}; | |
export default KeyboardSpacer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment