bhrott / react-native-masked-text

A pure javascript masked text and input text component for React-Native.
MIT License
1.6k stars 251 forks source link

Can't remove custom mask #203

Open JonathanRufino opened 4 years ago

JonathanRufino commented 4 years ago

I'm facing issues when trying to remove custom masks.

Example:

const phone = "61912345678"

const masked = MaskService.toMask('custom', phone, { mask: '(99) 9 9999-9999' })
// result: (61) 9 1234-5678

const unmasked = MaskService.toRawValue('custom', masked, { mask: '(99) 9 9999-9999' })
// result: (61) 9 1234-5678
// expected: 61912345678

If I try to use a default mask, like cpf, it works

const cpf = "12345678900"

const masked = MaskService.toMask('cpf', cpf)
// result: 123.456.789-99

const unmasked = MaskService.toRawValue('cpf', masked)
// result: 12345678999

Version

"react-native-masked-text": "1.13.0"
ssscassio commented 4 years ago

This function is not auto-generated. I think you should provide a getRawValue function to your settings parameter

Check Custom section on documentation:

     /**
     * getRawValue: (Function | optional | defaults return current masked value)
     * use this function to parse and return values to use what you want.
     * for example, if you want to create a phone number mask (999) 999-99-99 but want to get only
     * the numbers for value, use this method for this parse step.
    */
    getRawValue: function(value, settings) {
      return 'my converted value';
    },

on your case you can use something like:

const unmasked = MaskService.toRawValue('custom', masked, {
  mask: '(99) 9 9999-9999' 
  getRawValue: (value, settings) {
      return value.replace(/\D/g,'');
  }
})