0

let say I have 3 views with TextInput inside it.

<View style={styles.row}>
    <TextInput
        ref={'cell_'+i}
        maxLength={1}
        spellCheck={false}
        style={styles.chewy}
        onKeyPress={this._onInputFilled} />
</View>
<View style={styles.row}>
    <TextInput
        ref={'cell_'+i}
        maxLength={1}
        spellCheck={false}
        style={styles.chewy}
        onKeyPress={this._onInputFilled} />
</View>
<View style={styles.row}>
    <TextInput
        ref={'cell_'+i}
        maxLength={1}
        spellCheck={false}
        style={styles.chewy}
        onKeyPress={this._onInputFilled} />
</View>

notice ref={'cell_'+i} where i equal to 0..2. now when I type in a char inside cell_0 how to focus (move the cursor) to the next TextInput ie.cell_1?

Dariel Pratama
  • 1,557
  • 2
  • 16
  • 43

1 Answers1

0

Try this code :

<View style={styles.row}>
    <TextInput
        ref={'cell_'+i}
        maxLength={1}
        spellCheck={false}
        style={styles.chewy}
        onKeyPress={this._onInputFilled}
        onSubmitEditing={() => this.focusNextField('cell_' + (i+1))}
        />
</View>
<View style={styles.row}>
    <TextInput
        ref={'cell_'+i}
        maxLength={1}
        spellCheck={false}
        style={styles.chewy}
        onKeyPress={this._onInputFilled}
        onSubmitEditing={() => this.focusNextField('cell_' + (i+1))}
        />
</View>
<View style={styles.row}>
    <TextInput
        ref={'cell_'+i}
        maxLength={1}
        spellCheck={false}
        style={styles.chewy}
        onKeyPress={this._onInputFilled}
        />
</View>

onSubmitEditing:

focusNextField = nextField => {
   this.refs[nextField].focus();
};
Vikram Biwal
  • 2,358
  • 1
  • 23
  • 35