Created
May 28, 2018 09:14
-
-
Save watanabeyu/ccf59b069987494a03a5f24d005a9857 to your computer and use it in GitHub Desktop.
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 React from 'react'; | |
import { TextInput as Input } from 'react-native'; | |
export default class TextInput extends React.Component { | |
static defaultProps = { | |
onFocus: () => { }, | |
} | |
constructor(props) { | |
super(props); | |
this.state = { | |
value: this.props.value, | |
refresh: false, | |
}; | |
} | |
shouldComponentUpdate(nextProps, nextState) { | |
if (this.state.value !== nextState.value) { | |
return false; | |
} | |
return true; | |
} | |
componentDidUpdate(prevProps) { | |
if (prevProps.value !== this.props.value && this.props.value === '') { | |
this.setState({ value: '', refresh: true }, () => this.setState({ refresh: false })); | |
} | |
} | |
onFocus = (e) => { | |
this.input.focus(); | |
this.props.onFocus(); | |
} | |
render() { | |
if (this.state.refresh) { | |
return null; | |
} | |
return ( | |
<Input | |
{...this.props} | |
ref={(ref) => { this.input = ref; }} | |
value={this.state.value} | |
onFocus={this.onFocus} | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seem like it doesn't work with redux-form :(