-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
help with currency mask and rifm props #111
Comments
would it be a stupid suggestion if I said to divide the field entrance by 100? so, if the user types "1", in the text field it would appear "0.01" |
I don't know if it's the better solution, but: The tricky is that |
it is looking great, tks the only problem is the is there a way to fix it? |
I think you should use |
what is the best way to add a failing test case for this behavior? |
what about an |
this line is breaking the backspace behavior https://github.com/realadvisor/rifm/blob/master/src/Rifm.js#L211
|
working version without that line const numberFormat = (str: string) => {
if (str === '') str = '0';
const value = (parseInt(str.replace(/[^\d]+/gi, ''), 10) / 100).toFixed(2);
const getCurrencyFormat = () => {
if (!value) {
return '';
}
return new Intl.NumberFormat('pt-br', {
style: 'currency',
currency: 'BRL',
})
.format(value)
.replace('R$ ', '');
};
return getCurrencyFormat();
};
const rifm = useMask({
value,
onChange: (value) => {
const e = { target: { value } };
onChange(e);
},
format: numberFormat,
}); |
do you see a bug where the second inserted number after an erase goes to the decimals? ssr-2022-03-24_18.44.29.mp4added a new if to the numberFormat above to fix it: const numberFormat = (str: string) => {
if (str === '') str = '0';
if (str.includes('R$') && !isNaN(parseInt(str.charAt(0)))) {
str = str.replace('R$', '')
str = str.slice(1, str.length) + str.charAt(0)
}
const value = (parseInt(str.replace(/[^\d]+/gi, ''), 10) / 100).toFixed(2);
const getCurrencyFormat = () => {
if (!value) {
return '';
}
return new Intl.NumberFormat('pt-br', {
style: 'currency',
currency: 'BRL',
})
.format(value)
.replace('R$ ', '');
};
return getCurrencyFormat();
};
|
I'm trying to apply this mask
0.000.000,00
for Brazilian currencyI'd like to have the following behavior:
1 cents -> 0,01 (when user types 1, it gets 0,01)
10 cents -> 0,10
100 cents -> 1,00
100000 cents -> 1.000,00
I'm not sure if I need to use
format
,replace
,mask true or false
, orappend
to achieve this behaviorany thoughts on this?
The text was updated successfully, but these errors were encountered: