CaioQuirinoMedeiros / react-native-mask-input

🎭:iphone: A simple and effective Text Input with mask for ReactNative on iOS and Android. Includes obfuscation characters feature.
https://www.npmjs.com/package/react-native-mask-input
MIT License
309 stars 31 forks source link

Masks.BRL_CURRENCY for Positive and Negative values #19

Open guilhermemigliano opened 2 years ago

guilhermemigliano commented 2 years ago

Is there any way to use the Masks.BRL_CURRENCY for negative and positve values?

CaioQuirinoMedeiros commented 2 years ago

@guilhermemigliano you can try using the createNumberMask to create your own custom mask... Something like that (not tested, just an idea):

<MaskInput
  ...
  mask={(text) => {
    const numberPrefix = ['R', '$', ' '];
    const isNegative = text?.includes('-');

    if (isNegative) {
      numberPrefix.push('-');
    }

    return createNumberMask({
      prefix: numberPrefix,
      separator: ',',
      delimiter: '.',
      precision: 2,
    })(text); // typescript will complain, I need to fix that
  }}
  ...
/>

Also, you may want to see this: https://github.com/CaioQuirinoMedeiros/react-native-currency-input