Last active
October 1, 2019 07:38
-
-
Save OkancanCosar/b60e6b6dcb1f55e8e73e3d22947ece7d to your computer and use it in GitHub Desktop.
dismis keyboard when click somewhere and scrool when keyboard open
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
export default function App() { | |
return ( | |
<ArunaKeyboard> | |
<Login /> | |
</ArunaKeyboard> | |
); | |
} | |
https://stackoverflow.com/questions/29685421/hide-keyboard-in-react-native |
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 { PropTypes } from "prop-types"; | |
import React, { Component } from "react"; | |
import { | |
Animated, | |
Dimensions, | |
Keyboard, | |
StyleSheet, | |
TextInput, | |
UIManager, | |
TouchableWithoutFeedback | |
} from "react-native"; | |
const { State: TextInputState } = TextInput; | |
class ArunaKeyboard extends Component { | |
state = { | |
shift: new Animated.Value(0) | |
}; | |
componentWillMount() { | |
this.keyboardDidShowSub = Keyboard.addListener( | |
"keyboardDidShow", | |
this.handleKeyboardDidShow | |
); | |
this.keyboardDidHideSub = Keyboard.addListener( | |
"keyboardDidHide", | |
this.handleKeyboardDidHide | |
); | |
} | |
componentWillUnmount() { | |
this.keyboardDidShowSub.remove(); | |
this.keyboardDidHideSub.remove(); | |
} | |
render() { | |
const { children: renderProp } = this.props; | |
const { shift } = this.state; | |
return ( | |
<TouchableWithoutFeedback onPress={() => Keyboard.dismiss()}> | |
<Animated.View | |
style={[styles.container, { transform: [{ translateY: shift }] }]} | |
> | |
{this.props.children} | |
</Animated.View> | |
</TouchableWithoutFeedback> | |
); | |
} | |
handleKeyboardDidShow = event => { | |
const { height: windowHeight } = Dimensions.get("window"); | |
const keyboardHeight = event.endCoordinates.height; | |
const currentlyFocusedField = TextInputState.currentlyFocusedField(); | |
UIManager.measure( | |
currentlyFocusedField, | |
(originX, originY, width, height, pageX, pageY) => { | |
const fieldHeight = height; | |
const fieldTop = pageY; | |
const gap = windowHeight - keyboardHeight - (fieldTop + fieldHeight); | |
if (gap >= 0) { | |
return; | |
} | |
Animated.timing(this.state.shift, { | |
toValue: gap, | |
duration: 400, | |
useNativeDriver: true | |
}).start(); | |
} | |
); | |
}; | |
handleKeyboardDidHide = () => { | |
Animated.timing(this.state.shift, { | |
toValue: 0, | |
duration: 400, | |
useNativeDriver: true | |
}).start(); | |
}; | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
height: "100%", | |
left: 0, | |
position: "absolute", | |
top: 0, | |
width: "100%" | |
} | |
}); | |
ArunaKeyboard.propTypes = { | |
children: PropTypes.any.isRequired | |
}; | |
export { ArunaKeyboard }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment