-
-
Save ZHocean123/b18a3a4ee21d9989b91e2ad04f399026 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