Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TouchableHighlight won't accept press events while keyboard is open

I have a component which contains a TextInput and a TouchableHighlight side by side. You tap on the textbox, type what you want, then tap the add button to save it. Now the problem is the keyboard open from typing, you need to dismiss it, otherwise the button won't respond. If i tap the button first, the keyboard dismissed, then a second tap works. I feel like i should able to do both in one. Here's my render component:

class FormInput extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: null
    };
  }
  componentDidMount() {
    this.refs.textInput.focus();
  }

  _changeTextEvent(event) {
    this.setState({
      text: event.nativeEvent.text
    });
  }

  render() {
    var style = [styles.textBox];
    if (this.props.errors.length > 0) {
      style.push(styles.errorTextBox);
    }
    var errors = null;
    if (this.props.errors.length > 0) {
      errors = this.props.errors.map((msg) => {
        return (<Text style={styles.errorLabel}>{msg}</Text>);
      });
    }
    return (
      <View>
        <View style={styles.container}>
          <TextInput
            ref='textInput'
            style={style}
            onChange={this._changeTextEvent.bind(this)}
            autoFocus={true}
          />
          <TouchableHighlight underlayColor="#96DBFF" style={styles.addButton} onPress={() => { this.props.pressEvent(this.state.text) }}>
            <Text style={styles.addButtonText}>Add</Text>
          </TouchableHighlight>
        </View>
        <View>{errors}</View>
      </View>
    );
  }
}
like image 510
agmcleod Avatar asked Jun 12 '15 19:06

agmcleod


1 Answers

Please see this discussion on calling blur on a textfield to dismiss it:

https://github.com/facebook/react-native/issues/113

Also, I just tested this on the simulator and the TouchableHighlight definitely does respond even when the TextInput has focus and the keyboard is up. By adding code like:

pressEvent() {
    this.refs.textInput.blur();
}

I am able to dismiss the keyboard from the TouchableHighlight.

like image 182
Colin Ramsay Avatar answered Nov 15 '22 15:11

Colin Ramsay