Last active
June 15, 2021 11:57
-
-
Save mmazzarolo/77407406eea9a574c060662ab1bcac1f to your computer and use it in GitHub Desktop.
A simple wrapper on the React Native TextInput that fixes its ugly default Android style
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 * as React from "react"; | |
import { StyleSheet, TextInput } from "react-native"; | |
const BLUE = "#428AF8"; | |
const LIGHT_GRAY = "#D3D3D3"; | |
class MyTextInput extends React.Component { | |
state = { | |
isFocused: false | |
}; | |
handleFocus = event => { | |
this.setState({ isFocused: true }); | |
if (this.props.onFocus) { | |
this.props.onFocus(event); | |
} | |
}; | |
handleBlur = event => { | |
this.setState({ isFocused: false }); | |
if (this.props.onBlur) { | |
this.props.onBlur(event); | |
} | |
}; | |
render() { | |
const { isFocused } = this.state; | |
const { onFocus, onBlur, ...otherProps } = this.props; | |
return ( | |
<TextInput | |
selectionColor={BLUE} | |
underlineColorAndroid={ | |
isFocused ? BLUE : LIGHT_GRAY | |
} | |
onFocus={this.handleFocus} | |
onBlur={this.handleBlur} | |
style={styles.textInput} | |
{...otherProps} | |
/> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
textInput: { | |
height: 40, | |
paddingLeft: 6 | |
} | |
}); | |
export default MyTextInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment