How to autofocus next TextInput on react-native

前端 未结 6 1628
迷失自我
迷失自我 2020-12-15 00:23

I\'m trying to create a passcode protected screen. The screen will uses 4 numeric input as the passcode.

The way I\'m doing this is create a TextInput Component and

相关标签:
6条回答
  • 2020-12-15 00:59

    we handled this style of screen with a different approach.

    Rather than manage 4 individual TextInputs and handle the navigation of focus across each one (and then back again when the user deletes a character), we have a single TextInput on screen but is invisible (ie. 0px x 0px) wide which has the focus, maxLength and keyboard configuration, etc.

    This TextInput takes input from the user but can't actually been seen, as each character is typed in we render the entered text as a series simple View/Text elements, styled much similar to your screen above.

    This approach worked well for us with no need to manage what the 'next' or 'previous' TextInput to focus next to.

    0 讨论(0)
  • 2020-12-15 01:01

    You can use focus method onChangeText as Jason stated, in addition to that adding maxLength={1} can make you jump to the next input immediately without checking what's added. (just noticed its deprecated, but still this is how I solved my problem, and should do fine until v0.36, and this link explains how you should update the deprecated function).

      <TextInput
       ref="first"
       style={styles.inputMini}
       maxLength={1}
       keyboardType="numeric"
       returnKeyType='next'
       blurOnSubmit={false}
       placeholderTextColor="gray"
       onChangeText={(val) => {
          this.refs['second'].focus()
       }}
      />
      <TextInput
       ref="second"
       style={styles.inputMini}
       maxLength={1}
       keyboardType="numeric"
       returnKeyType='next'
       blurOnSubmit={false}
       placeholderTextColor="gray"
       onChangeText={(val) => {
         this.refs['third'].focus()
       }}
      />
      ...
    

    Please notice that my use of refs are deprecated too, but I've just copied the code since I can guarantee you that was working back then (hopefully works now too).

    Finally, the main issue with this type of implementation is, once you try to remove a number with backspace your focus will jump to next one, causing serious UX issues. However, you can listen for backspace key entry and perform something different instead of focusing to next input. So I'll leave a link here for you to further investigate if you choose to use this type of implementation.

    Hacky Solution to Previously Described Issue: If you check what's entered in onChangeText prop before doing anything, you can jump to next input if the value is a number, else (that's a backspace), jump back. (Just came up with this idea, I haven't tried it.)

    0 讨论(0)
  • 2020-12-15 01:02

    You cannot forward the ref to <TextInput> using that way because ref is one of the special props. Thus, calling this.refs.passcode2 will return you <PasscodeTextInput> instead.

    Try change to the following to get the ref from <TextInput>.

    PasscodeTextInput.js

    const PasscodeTextInput = ({ inputRef, ... }) => {
    
      ...
    
      return (
        <View>
          <TextInput
            ref={(r) => { inputRef && inputRef(r) }}
            ...
          />
        </View>
        ...
      );
    }
    

    Then, assign the inputRef from <PasscodeTextInput> to a variable and use focus() to switch focus (it is not deprecated as of RN 0.41.2).

    index.ios.js

    return (
      <PasscodeTextInput
        autoFocus={true}
        onChangeText={(event) => { event && this.passcode2.focus() }} />
    
      <PasscodeTextInput
        inputRef={(r) => { this.passcode2 = r }}
        onChangeText={(event) => { event && this.passcode3.focus() }} />
    
      <PasscodeTextInput
        inputRef={(r) => { this.passcode3 = r }}
        onChangeText={(event) => { event && this.passcode4.focus() }} />
    
      <PasscodeTextInput
        inputRef={(r) => { this.passcode4 = r }} />
    );
    

    P.S: event && this.passcode2.focus() prevents focus is switched when trying to clear the old passcode and enter a new one.

    0 讨论(0)
  • 2020-12-15 01:03

    There is a defaultProp for TextInput where one can focus after component mounted.

    autoFocus

    If true, focuses the input on componentDidMount, the default value is false. for more information please read the related Docs.

    UPDATE

    After componentDidUpdate it won't work properly. In that case, one can use ref to focus programmatically.

    0 讨论(0)
  • 2020-12-15 01:11

    I think the issue is that onSubmitEditing is when you hit the "return" or "enter" key on the regular keyboard... there is not one of those buttons on the keypad.

    Assuming you want each input to only have one character, you could look at the onChangeText and then check if text has length 1 and call focus if the length is indeed 1.

    0 讨论(0)
  • 2020-12-15 01:15
        <TextInput 
        ref={input => {
              this.nameOrId = input;
            }}
        />
    
    
    
        <TouchableOpacity
          onPress={()=>{
            this.nameOrId.focus()
          }}
         >
          <Text>Click</Text>
        </TouchableOpacity>
    
    0 讨论(0)
提交回复
热议问题