jc28
jc28

Reputation: 1882

How to make TextInput prefill in React Native app?

I have a TextInput component in my React Native app. And i need to make the field is pre-populated with a mask of 408xx810xxx, 1-3 and 6-8 digits in the field, their change is prohibited to put for users. Can somebody recommend the best way how can i do it?

          <TextInput
            style={[SS.input, styles.input]}
            placeholder={props.placeholder} placeholderTextColor={theme.inputPlaceholder}
            underlineColorAndroid='transparent' editable={!props.disabled}
            keyboardType={props.keyboardType || 'default'} autoCapitalize={capitalize}
            keyboardAppearance={props.keyboardAppearance}
            autoCorrect={autoCorrect} selection={state.position}
            secureTextEntry={this.state.guarded}
            value={this.state.value}
            onChangeText={this._onChangeText}
            onFocus={this._onFocus} onBlur={this._onBlur}
            autoFocus={props.autoFocus}
            multiline={props.multiline}
            maxLength={props.maxLength}
            onContentSizeChange={onContentSizeChange}
          />

Upvotes: 4

Views: 6148

Answers (3)

jc28
jc28

Reputation: 1882

The best decision that i have found to use react-native-masked-text library:

import { TextInputMask } from 'react-native-masked-text';
            <TextInputMask
              type='custom' 
              options={{mask: accountMask}} 
              maxLength={20}
              customTextInput={InputText} 
              customTextInputProps={this._loginMaskProps}
              value={vm.checkingAccount} keyboardType={'number-pad'}
              placeholder={accountPlaceholder} 
              onChangeText={vm.changeCheckingAccount}
            />

You need just put property type to 'custom', and create a mask according https://github.com/benhurott/react-native-masked-text library, in my case it was like this:

 const accountMask = '40899810999999999999',

Upvotes: 0

Tim
Tim

Reputation: 10709

I have created a minimal example which exactly recreates your use case, without using any third party lib.

Code

changeText:

changeText(text){
// we do not allow the deletion of any character
if (text.length >= 11){
  var tmp = text.split("")
  // check if there are still is a X value in string 
  const currentIndex = text.indexOf('X');
  if (currentIndex) {
    //if a X was found, replace it with the newest character
    tmp[currentIndex] = tmp[11];
    //remove latest character again
    tmp.pop();
  }
  this.setState({value: tmp.join("")})
  }
}

render:

  <View style={styles.container}>
     <TextInput
      value={this.state.value}
      onChangeText={(text) => this.changeText(text)}
     />
  </View>

Working Demo

https://snack.expo.io/Sym-2W8RH

Upvotes: 3

Danish Sarwar
Danish Sarwar

Reputation: 403

For pre-population, you can assign hardcoded masked value to the state this.state.value in your constructor.

And for masking I recommend you using this library:

https://github.com/react-native-community/react-native-text-input-mask

using this library masking work something like this

<TextInputMask
  refInput={ref => { this.input = ref }}
  onChangeText={(formatted, extracted) => {
  console.log(formatted) // +1 (123) 456-78-90
  console.log(extracted) // 1234567890
 }}
  mask={"+1 ([000]) [000] [00] [00]"}

/>

Upvotes: 1

Related Questions