Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to focus the next field of input, in react-native component

I have one component called ImageTextField:

import React from 'react';
import {TextInput, View } from 'react-native';

export default class ImageTextField extends React.Component {
  render() {
    const {
      placeHolderValue, value, editable
    } = this.props;
    return (
      <View>
        <TextInput
          placeholder={placeHolderValue}
          placeholderTextColor="#b1b1b1"
          value={value}
          editable={editable}
        />
      </View>
    );
  }
}

My screen:

import React from 'react';
import { StyleSheet, View, Text, TouchableOpacity} from 'react-native';
import ImageTextField from '../../custom-elements/ImageTextField';

export default class App extends React.Component {
  render() {
    return (
      <ImageTextField 
        onChangeText={text => this.onNameChange(text)}
        color={Colors.textFieldBlack}
        placeHolderValue="Name"
        value={this.state.name}
        autoCapitalize="words"
      />
      <ImageTextField
        onChangeText={text => this.onEmailChange(text)}
        color={Colors.textFieldBlack}
        placeHolderValue="E-mail"
        value={this.state.email}
        keyboardType='email-address'
      />
      <ImageTextField
        onChangeText={text => this.onPhoneChange(text)}
        color={Colors.textFieldBlack}
        placeHolderValue="Phone"
        value={this.state.phone}
        keyboardType="phone-pad"
      />
    );
  }
}

In above code, i have ImageTextField Component which i am using in my screen, i want to focus the next field, if user press next key in keyboard its need to be jump on second field.

like image 325
Mohamed Sameer Avatar asked Apr 09 '18 10:04

Mohamed Sameer


People also ask

How do you focus on next input?

If we want to focus on the text input field when the current input field reaches its max character capacity, we have to find the next input HTML element and make it focus. Each time the user types in the current text field, we have to check whether the field has a max character that is specified.

How do you change the focus from one text input to another in react native?

You have to focus the TextInput you want the cursor to go to. To do that, You can set maxLength to 1 , and call onChangeText to change focus. You may also want to capture the value and store it in state.


2 Answers

Add a prop which will retrieve the ref of the input to your ImageTextField component. So your render function should look like:

render() {
const {
  placeHolderValue, value, editable, inputRef
} = this.props;
return (
  <View>
    <TextInput
      placeholder={placeHolderValue}
      placeholderTextColor="#b1b1b1"
      value={value}
      editable={editable}
      ref={inputRef}
    />
  </View>
);
}

Then your App component will look like:

render() {
return (
  <ImageTextField 
    onChangeText={text => this.onNameChange(text)}
    color={Colors.textFieldBlack}
    placeHolderValue="Name"
    value={this.state.name}
    autoCapitalize="words"
    onSubmitEditing={() => this.emailAddress.focus()}
  />
  <ImageTextField
    onChangeText={text => this.onEmailChange(text)}
    color={Colors.textFieldBlack}
    placeHolderValue="E-mail"
    value={this.state.email}
    keyboardType='email-address'
    inputRef={ref => this.emailAddress = ref}
    onSubmitEditing={() => this.phoneNumber.focus()}
  />
  <ImageTextField
    onChangeText={text => this.onPhoneChange(text)}
    color={Colors.textFieldBlack}
    placeHolderValue="Phone"
    value={this.state.phone}
    keyboardType="phone-pad"
    inputRef={ref => this.phoneNumber = ref}
  />
);
}
like image 91
Foyarash Avatar answered Oct 05 '22 16:10

Foyarash


Component Name= MyTextInput

render() {
return (
<View>
<TextInput
  onChangeText={this.props.onChangeText}
  placeholder="some text"
  value={this.state.value}
  ref={this.props.inputRef}
  onSubmitEditing={this.props.onSubmitEditing}
/>
  </View>
);
}

Then your App component will look like:

<MyTextInput 
    onChangeText={text => this.onNameChange(text)}
    color={Colors.textFieldBlack}
    placeHolderValue="Name"
    value={this.state.name}
    autoCapitalize="words"
    onSubmitEditing={() => this.email.focus()}
  />

  <MyTextInput 
    onChangeText={text => this.onNameChange(text)}
    color={Colors.textFieldBlack}
    placeHolderValue="email"
    value={this.state.name}
    autoCapitalize="words"
    inputRef={ref => this.email = ref}
    onSubmitEditing={() => alert('submitted')}
  />
like image 39
Muhammad Numan Avatar answered Oct 05 '22 14:10

Muhammad Numan