react-native-mask-text
react-native-mask-text copied to clipboard
NaN value with curreny type
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" />
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.
This issue appears to be occurring with prefix as-well.
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
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
The solution of @ErikHasp Works fine, please @hoangnguyen8895 Merge that in the code