react-native-mask-text icon indicating copy to clipboard operation
react-native-mask-text copied to clipboard

NaN value with curreny type

Open hoangnguyen8895 opened this issue 2 years ago • 4 comments

When I deleted all the text, instead of 0 the input shows NaN. it happens when precision: 0 this is my code

<MaskedTextInput type="currency" options={{ suffix: ' $', decimalSeparator: '.', groupSeparator: ',', precision: 0 }} onChangeText={(text, rawText) => { console.log(text); console.log(rawText); }} style={styles.input} keyboardType="numeric" />

hoangnguyen8895 avatar Jul 19 '21 03:07 hoangnguyen8895

Hey @hoangnguyen8895, thanks to open this issue. This bug is not related to precision 0, is related to suffix. Suffix seems doesn't work fine.

Also, precision has 0 as defaultValue, you don't need to indicate precision when is 0.

A temporary fix is to use prefix instead of suffix until the correction is released.

Feel free to open a PR.

akinncar avatar Jul 19 '21 12:07 akinncar

This issue appears to be occurring with prefix as-well.

dan-trewin avatar Aug 31 '21 21:08 dan-trewin

Should be fixed by adding this line in mask.ts:

function unMask(value: string, type: 'custom' | 'currency' = 'custom') {
  if (type === 'currency') {
    if (!value) return '0'

    const unMaskedValue = value.replace(/\D/g, '')
    if (!unMaskedValue) return '0'   // <--------------------- add this line
    const number = parseInt(unMaskedValue.trimStart())

    return number.toString()
  }

  return value.replace(/\W/g, '')
}

but didn't give it full thought yet

ErikHasp avatar Dec 23 '21 14:12 ErikHasp

Should be fixed by adding this line in mask.ts:

function unMask(value: string, type: 'custom' | 'currency' = 'custom') {
  if (type === 'currency') {
    if (!value) return '0'

    const unMaskedValue = value.replace(/\D/g, '')
    if (!unMaskedValue) return '0'   // <--------------------- add this line
    const number = parseInt(unMaskedValue.trimStart())

    return number.toString()
  }

  return value.replace(/\W/g, '')
}

Thanks @ErikHasp for share it

Brenont avatar Apr 29 '22 09:04 Brenont

The solution of @ErikHasp Works fine, please @hoangnguyen8895 Merge that in the code

DarlonHenrique avatar Oct 02 '23 15:10 DarlonHenrique