Skip to content

Instantly share code, notes, and snippets.

@mmazzarolo
Last active January 20, 2023 10:21
Show Gist options
  • Save mmazzarolo/1966b9333ed5c6b4fd0be3ec6bcdb1df to your computer and use it in GitHub Desktop.
Save mmazzarolo/1966b9333ed5c6b4fd0be3ec6bcdb1df to your computer and use it in GitHub Desktop.
A simple wrapper on the React Native TextInput that fixes its ugly default Android style
import * as React from "react";
import {
NativeSyntheticEvent,
StyleSheet,
TextInput,
TextInputFocusEventData,
TextInputProps
} from "react-native";
interface State {
isFocused: boolean;
}
const BLUE = "#428AF8";
const LIGHT_GRAY = "#D3D3D3";
class MyTextInput extends React.Component<TextInputProps, State> {
state = {
isFocused: false
};
handleFocus = (e: NativeSyntheticEvent<TextInputFocusEventData>) => {
this.setState({ isFocused: true });
if (this.props.onFocus) {
this.props.onFocus(e);
}
};
handleBlur = (e: NativeSyntheticEvent<TextInputFocusEventData>) => {
this.setState({ isFocused: false });
if (this.props.onBlur) {
this.props.onBlur(e);
}
};
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