Skip to content

Instantly share code, notes, and snippets.

@larkintuckerllc
Created September 30, 2018 16:28
Show Gist options
  • Save larkintuckerllc/b0b02afcd3e16eff55bd31b9e272a2a5 to your computer and use it in GitHub Desktop.
Save larkintuckerllc/b0b02afcd3e16eff55bd31b9e272a2a5 to your computer and use it in GitHub Desktop.
hrnk 2
import React, { Component } from 'react';
import { Animated, Dimensions, Keyboard, StyleSheet, TextInput, UIManager } from 'react-native';
const { State: TextInputState } = TextInput;
export default class App 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 { shift } = this.state;
return (
<Animated.View style={[styles.container, { transform: [{translateY: shift}] }]}>
<TextInput
placeholder="A"
style={styles.textInput}
/>
<TextInput
placeholder="B"
style={styles.textInput}
/>
<TextInput
placeholder="C"
style={styles.textInput}
/>
<TextInput
placeholder="D"
style={styles.textInput}
/>
<TextInput
placeholder="E"
style={styles.textInput}
/>
</Animated.View>
);
}
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: 1000,
useNativeDriver: true,
}
).start();
});
}
handleKeyboardDidHide = () => {
Animated.timing(
this.state.shift,
{
toValue: 0,
duration: 1000,
useNativeDriver: true,
}
).start();
}
}
const styles = StyleSheet.create({
container: {
backgroundColor: 'gray',
flex: 1,
height: '100%',
justifyContent: 'space-around',
left: 0,
position: 'absolute',
top: 0,
width: '100%'
},
textInput: {
backgroundColor: 'white',
height: 40,
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment