Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put two masks in 1 input #265

Open
MatheusFC2 opened this issue Jan 26, 2022 · 1 comment
Open

How to put two masks in 1 input #265

MatheusFC2 opened this issue Jan 26, 2022 · 1 comment

Comments

@MatheusFC2
Copy link

In my case, I want to put a CPF and CNPJ mask in 1 input

image

When the person adds a number more than 11 digits, the mask turns into cpnj

image

I tried to add 2 mask types

`
export default function App() {

    const [cpf, setCpf] = useState('');
  
    return (
      <View style={styles.container}>
        <TextInputMask 
          style={styles.input}
          
          type={'cpf', 'cpnj'}
          
  
          value={cpf}
          onChangeText={text => setCpf(text)}
        />
  
      </View>
    );
}

`

I wanted help on how I could create this code

Sorry my english i'm from brazil

@ederhmaia
Copy link

ederhmaia commented Jan 27, 2022

First of all you need hook the useState when you want component updates. In this case you wanna update the state of the attribute type (type="cpf" // type="cnpj") according to the character length. You can pass 'cpf' as the initial value, because has less characters and will be the first option to check.

const [inputMask, setInputMask] = useState('cpf')

Then, in the component when onChangeText event is fired, the setInputMask function is called passing cpf or cnpj (depending on text length) as an argument, which will be the value of inputMask

<TextInputMask
         placeholder="CPF/CNPJ"
         type={inputMask}
         onChangeText={(text) => {
               setInputMask(text?.length >= 14 ? 'cnpj' : 'cpf')
          }}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants