Skip to content

Instantly share code, notes, and snippets.

@isholaomotayo
Created November 20, 2017 18:03
Show Gist options
  • Save isholaomotayo/ed530e6121aba606f2b9cce523aabe55 to your computer and use it in GitHub Desktop.
Save isholaomotayo/ed530e6121aba606f2b9cce523aabe55 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import {
View,
StatusBar,
TextInput,
Animated,
} from 'react-native';
class FloatingLabelInput extends Component {
state = {
isFocused: false,
};
componentWillMount() {
this._animatedIsFocused = new Animated.Value(this.props.value === '' ? 0 : 1);
}
handleFocus = () => this.setState({ isFocused: true });
handleBlur = () => this.setState({ isFocused: false });
componentDidUpdate() {
Animated.timing(this._animatedIsFocused, {
toValue: (this.state.isFocused || this.props.value !== '') ? 1 : 0,
duration: 200,
}).start();
}
render() {
const { label, ...props } = this.props;
const labelStyle = {
position: 'absolute',
left: 0,
top: this._animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: [18, 0],
}),
fontSize: this._animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: [20, 14],
}),
color: this._animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: ['#aaa', '#000'],
}),
};
return (
<View style={{ paddingTop: 18 }}>
<Animated.Text style={labelStyle}>
{label}
</Animated.Text>
<TextInput
{...props}
style={{ height: 26, fontSize: 20, color: '#000', borderBottomWidth: 1, borderBottomColor: '#555' }}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
blurOnSubmit
/>
</View>
);
}
}
export default class App extends Component {
state = {
value: '',
};
handleTextChange = (newText) => this.setState({ value: newText });
render() {
return (
<View style={{ flex: 1, padding: 30, backgroundColor: '#f5fcff' }}>
<StatusBar hidden />
<FloatingLabelInput
label="Email"
value={this.state.value}
onChangeText={this.handleTextChange}
/>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment