如何在下次按下时关注下一个输入(如果有)

时间:2017-11-01 19:04:19

标签: reactjs react-native

我实际上知道如何做到这一点,但我需要一种不同的方法 所以我有文字输入,如:

<TextInput returnKeyType='next' 
           ref={ref => this.someName = ref}
           onSubmitEditing={(event) => { this.nextFieldName.focus() }}/>

诀窍是,我不想在下一个字段中使用特定名称,但只需将焦点放在下一个焦点即可。
这可能吗?

2 个答案:

答案 0 :(得分:2)

你的问题有点宽泛。如果您提供更多详细信息,我可能会提供帮助,但现在让我举个例子。

假设您有一组数据正在映射并创建表单。

const data = [
  {
    type: 'text',
    value: 'name'
  },
  {
    type: 'text',
    value: 'surname'
  },
  {
    type: 'number',
    value: 'age'
  }
];

现在让我们遍历数据

data.map((field, index) => {
  return (
    <TextInput
      type={field.type}
      value={this.state[field.value]}
      ref={(ref) => this.refs[index] = ref}
      onSubmitEditing={() => this.onSubmitEditing(index)}
    />
  );
});

现在因为我们知道我们在哪个字段上,如果存在其他字段,我们可以关注或提交表单。

onSubmitEditing = (index) => {
  if (data.length <= (index + 1)) {
    this.refs[index + 1].focus();
  } else {
    // no input to move 
    // submit form
  }
}

答案 1 :(得分:0)

假设您有2个textInputs,那么如果按next,这就是下一个输入的重点:

<TextInput returnKeyType='next' 
       onSubmitEditing={(event) => { this.someName.focus() }}
/>

<TextInput returnKeyType='next' 
       ref={ref => this.someName = ref}
/>
相关问题