Identify Return Key action in React Native

后端 未结 3 1185
渐次进展
渐次进展 2021-02-06 20:59

I have a TextInput which I have enabled multiline as true. Thing is the Keyboard won\'t hide after Return is pressed. It goes to a new line. So I was h

相关标签:
3条回答
  • 2021-02-06 21:21

    What I used is onSubmitEditing props. e.g.

    <TextInput style={[styles.textInput]}
      placeholder='搜索'
      placeholderTextColor='#bbb'
      onChange={(event) => {
        this.searchChange(event.nativeEvent.text)
      }}
      returnKeyType='search'
      autoFocus={true}
      value={ this.props.searchName }
      selectionColor={colors.orangeColor}
      onSubmitEditing={this.searchSubmit}
      clearButtonMode="while-editing"
    />
    
    0 讨论(0)
  • 2021-02-06 21:37

    In case you are using with multiline={true}, the return key would also add the newline in the text before calling onSubmitEditing. Also, the keyboard won't get dismissed automatically making you import { Keyboard } from 'react-native' and calling Keyboard.dismiss() in onSubmitEditing.

    An easier solution would be to use the blurOnSubmit={true} to automatically dismiss the keyboard and prevent return key from registering as newline.

    0 讨论(0)
  • 2021-02-06 21:46

    Okay, found the solution.

    <TextInput
        style={styles.additionalTextInput}
        multiline={true}
        autoCapitalize="sentences"
        autoCorrect={true}
        onChangeText={(orderInstructions) => this.setState({orderInstructions})}
        keyboardType="default"
        returnKeyType="done"
        onKeyPress={this.handleKeyDown}
        placeholder="Enter text here..."
    />
    
    handleKeyDown: function(e) {
        if(e.nativeEvent.key == "Enter"){
            dismissKeyboard();
        }
    },
    

    The method dismissKeyboard is from react-native-dismiss-keyboard.

    This works perfectly for me.

    0 讨论(0)
提交回复
热议问题