Created
July 15, 2016 16:48
-
-
Save chirag04/d7a7d58f4afc9520a51f511ce7f67788 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { | |
Keyboard, | |
TextInput, | |
ScrollView, | |
findNodeHandle, | |
} = ReactNative; | |
// tab height + listitem bottom padding + textinput height | |
const ADDITIONAL_OFFSET = 35 + 50; | |
class KeyboardAvoidingScrollView extends React.Component { | |
componentWillMount() { | |
this.subscriptions = [ | |
Keyboard.addListener('keyboardDidShow', this.keyboardDidShow), | |
]; | |
} | |
componentWillUnmount() { | |
this.subscriptions.forEach((sub) => sub.remove()); | |
} | |
keyboardDidShow = () => { | |
const currentlyFocusedField = TextInput.State.currentlyFocusedField(); | |
const scrollResponder = this.refs.keyboardAvoidingScrollView.getScrollResponder(); | |
scrollResponder.scrollResponderScrollNativeHandleToKeyboard( | |
findNodeHandle(currentlyFocusedField), | |
ADDITIONAL_OFFSET, | |
true | |
); | |
}; | |
render() { | |
const { children, ...props } = this.props; | |
return ( | |
<ScrollView | |
{...props} | |
ref="keyboardAvoidingScrollView" | |
keyboardDismissMode="on-drag" | |
> | |
{children} | |
</ScrollView> | |
); | |
} | |
} | |
KeyboardAvoidingScrollView.propTypes = { | |
...ScrollView.propTypes, | |
}; | |
export default KeyboardAvoidingScrollView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@chirag04 I tried using this component in my app. It seems to work fine most of the time, except a few time when the component leaves some whitespace at the bottom, even when keyboard is not visible.