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
305 stars 31 forks source link

`precision: 0` breaks numerical input #10

Closed nandorojo closed 2 years ago

nandorojo commented 2 years ago
createNumberMask({
  prefix: ['$'],
  delimiter: ',',
  separator: '.',
  precision: 0
})

I'm trying to use this for a mask, so that you can only use integer dollar values like $200, but it ends up prepending a decimal:

Screen Shot 2021-11-18 at 5 42 09 PM

And if you enter 4 digits, it's totally off:

Screen Shot 2021-11-18 at 5 45 24 PM

Any suggestions on how to get around this @CaioQuirinoMedeiros?

Snack: https://snack.expo.dev/@nandorojo/humiliated-pretzel

Full code here:

import MaskInput, { createNumberMask } from 'react-native-mask-input';
import React from 'react'

const dollarMask = createNumberMask({
  prefix: ['$'],
  delimiter: ',',
  separator: '.',
  precision: 0,
})

export default function MyComponent() {
  const [value, setValue] = React.useState('');

  return (
    <MaskInput
      value={value}
      mask={dollarMask}
      onChangeText={(masked, unmasked) => {
        setValue(unmasked); // you can use the masked value as well

        // assuming you typed "123456":
        console.log(masked); // "U$ 1,234.56"
        console.log(unmasked); // "123456"
      }}
      style={{ fontSize: 40}}
      autoFocus
    />
  );
}
nandorojo commented 2 years ago

This works fine on the current input library FWIW

CaioQuirinoMedeiros commented 2 years ago

Good to know... anyway, I just got it fixed on release v1.1.0

aprilmintacpineda commented 2 years ago

@CaioQuirinoMedeiros hi!

Still doesn't work on v1.1.0 If I put in 1000, it shows ,1000 instead of 1,000

image

Can this be reopened?

import MaskInput, { createNumberMask } from 'react-native-mask-input';
import React from 'react'

const dollarMask = createNumberMask({
  delimiter: ',',
  separator: '.',
  precision: 0
})

export default function MyComponent() {
  const [value, setValue] = React.useState('');

  return (
    <MaskInput
      value={value}
      mask={dollarMask}
      onChangeText={(masked, unmasked) => {
        setValue(unmasked); // you can use the masked value as well

        // assuming you typed "123456":
        console.log(masked); // "U$ 1,234.56"
        console.log(unmasked); // "123456"
      }}
      style={{ fontSize: 40}}
      autoFocus
    />
  );
}

https://snack.expo.dev/@nandorojo/humiliated-pretzel

CaioQuirinoMedeiros commented 2 years ago

@aprilmintacpineda fixed on v1.1.1