question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TextInput.Focus is not working if i use Textinputmask in TextInput

See original GitHub issue

Environment

“react-native”: “0.57.8”, “react”: “16.6.3”, “react-native-masked-text”: “1.11.0”, “react-native-paper”: “2.12.0”,

Description

I tried to jump onto next Custom textinput by clicking on returnKeyType=‘next’…It’s throwing an error…if it is normal textinput(moving from one textinput to nextinput by clicking onreturnKeyType=‘next’. ) focus() is working fine…but when i injected textinputmask it is breaking… please help me how to resolve…

Screen Shot 2019-03-12 at 6 34 59 PM Screen Shot 2019-03-12 at 6 41 26 PM Screen Shot 2019-03-12 at 6 35 16 PM

Reproducible Demo

This flow is working fine

import { TextInputMask } from 'react-native-masked-text'
import { TextInput } from 'react-native-paper'

 <TextInput 
mode="outlined"
                ref='firstName'
                style={{marginVertical: Metrics.baseMargin * Metrics.screenWidth * 0.004,
marginRight: Metrics.baseMargin * Metrics.screenWidth * 0.022,
marginLeft: Metrics.baseMargin * Metrics.screenWidth * 0.004,}}
                label="First Name"
                theme={{
                  colors: {
                    primary: 'purple',
                    background: 'white
                  }
                }}
                value={this.state.firstName}
                keyboardType='default'
                returnKeyType='next'
                autoCorrect={false}
                autoCapitalize='words'
                onChangeText={(value) => this.handleRegistration('firstName', value)}
                onSubmitEditing={(event) => { this.refs.lastName.focus() }}
              />

<TextInput 
mode="outlined"
                ref='lastName'
                 value={this.state.lastName}
                    style={{marginVertical: Metrics.baseMargin * Metrics.screenWidth * 0.004,
    marginRight: Metrics.baseMargin * Metrics.screenWidth * 0.022,
    marginLeft: Metrics.baseMargin * Metrics.screenWidth * 0.004,}}
                label="Last Name"
                theme={{
                  colors: {
                    primary: 'purple',
                    background: 'white
                  }
                }}
       
                keyboardType='default'
                returnKeyType='next'
                autoCapitalize='words'
                onChangeText={(value) => this.handleRegistration('lastName', value)}
                onSubmitEditing={(event) => { this.refs.dateofBirth.focus() }}
              />

This flow is throwing error*** (Regular TextInput–>Custom TextInput ---->Regular Text Input)

<TextInput 
mode="outlined"
                ref='lastName'
                 value={this.state.lastName}
                    style={{marginVertical: Metrics.baseMargin * Metrics.screenWidth * 0.004,
    marginRight: Metrics.baseMargin * Metrics.screenWidth * 0.022,
    marginLeft: Metrics.baseMargin * Metrics.screenWidth * 0.004,}}
                label="Last Name"
                theme={{
                  colors: {
                    primary: 'purple',
                    background: 'white
                  }
                }}
       
                keyboardType='default'
                returnKeyType='next'
                autoCapitalize='words'
                onChangeText={(value) => this.handleRegistration('lastName', value)}
                onSubmitEditing={(event) => { this.refs.dateofBirth.focus() }}
              />

                  <TextInput 
mode="outlined"
                    ref='dateofBirth'
                    render={props =>
                      <TextInputMask
                        {...props}
                        type={'custom'}
                        options={{
                          mask: "99/99/9999"
                        }}

                      />
                    }
                    value={this.state.dateOfBirthValue}
                    style={{marginVertical: Metrics.baseMargin * Metrics.screenWidth * 0.004,
    marginRight: Metrics.baseMargin * Metrics.screenWidth * 0.022,
    marginLeft: Metrics.baseMargin * Metrics.screenWidth * 0.004,}}
                    maxLength={10}
                    textInputStyle={{
                      flex: 1,
                      color: Colors.facebook,
                      fontSize: Fonts.size.input * Metrics.screenWidth * 0.0020
                    }}

                    label="Date of Birth"
                    theme={{
                      colors: {
                       primary: 'purple',
                    background: 'white
                      }
                    }}
                    keyboardType='phone-pad'
                    returnKeyType='done'
                    onChangeText={(value) => this.handleRegistration('dateOfBirth', value)}
                    onSubmitEditing={(event) => { this.refs.zipCode.focus() }}
                  />

 <TextInput mode="outlined"
                ref='zipCode'
                label="Zip Code"
                maxLength={5}
                theme={{
                  colors: {
                      primary: 'purple',
                    background: 'white
                  }
                }}
                 value={this.state.zipCode}
                    style={{marginVertical: Metrics.baseMargin * Metrics.screenWidth * 0.004,
    marginRight: Metrics.baseMargin * Metrics.screenWidth * 0.022,
    marginLeft: Metrics.baseMargin * Metrics.screenWidth * 0.004,}}
                onChangeText={(value) => this.handleRegistration('zipCode', value)}
                keyboardType='numeric'
                returnKeyType='done'
              />

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
brunocrpontescommented, Mar 29, 2019

Hi, @anveshreddy87 😃

1 - you’re using a deprecated method to get refs, try use React.createRef instead.

2 - when you render a custom TextInput inside the TextInput from react-native-paper the refs aren’t passed to his children. So you need to get the ref directly from TextInputMask, like this:

dateOfBirt = React.createRef();
[...]
<TextInput
...props
render={props => <TextInputMask {...props} ref={this.dateOfBirt} />}
/>

3 - If you read the documentation of TextInputMask, he says: “getElement(): return the instance of Text component.”

So, to get the instance ref you need do: onSubmitForm = () => this.dateOfBirt.current.getElement().focus()

I forked your snack reproduction and made the changes cited above to help you. https://snack.expo.io/@brunocardosor/paper-textinput-refs

2reactions
anveshreddy87commented, Apr 4, 2019

@brunocrpontes Thanks for the solution. @Trancever It’s working now as expected…thanks and we can close the issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error on putting focus on InputTextMask through Icon on React ...
I am trying to implement shadow effect on button using react native but did not work can any one help me? Hot Network...
Read more >
Using input masks in React Native - LogRocket Blog
Implement input masks in React Native to develop user-friendly input constraints such as entering data according to a pre-defined format.
Read more >
TextInput - React Native
A foundational component for inputting text into the app via a keyboard. Props provide configurability for several features, ...
Read more >
TextInput · React Native Paper
A component to allow users to input text. ... Callback that is called when the text input is focused. ... testID to be...
Read more >
react-native-mask-input - npm
Start using react-native-mask-input in your project by running `npm ... No fancy stuff, it's basically a JavaScript function that allow you ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found