Skip to content
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

Number mask ignores radix mapping, when value been set directly #863

Closed
aktanoff opened this issue May 24, 2023 · 10 comments
Closed

Number mask ignores radix mapping, when value been set directly #863

aktanoff opened this issue May 24, 2023 · 10 comments

Comments

@aktanoff
Copy link

aktanoff commented May 24, 2023

Describe the bug
When using default Number mask if i put value with dot radix and numbers after radix, imask ignores the dot, and formats into invalid value

For example
imask.value = '1.23' -> '123'

But if i put in value number that already with comma, imask don't ignore it
imask.value = '1,23' -> '1,23'

Also this bug reproducable when using react-imask library

I've did some research, and i think bug appears because of those changes
672a369#diff-1137652693300b32d70aa6e393b2783596395655a3cd6fb7d70b60c737fab848R118

Lines where code ignores radix mapping

flags.input && flags.raw ||

So when we manually set value, we getting here with only one flag enabled input, and raw is falsy (undefined)

To Reproduce
Here is react example, where you can check that bug exist in hook's setValue, and in direct way by maskRef.current.value = '1.23'
https://codesandbox.io/s/react-imask-input-box-masks-forked-3x3diy?file=/src/index.js

Expected behavior
Radix mapping when we set value directly

Environment:

  • OS: MacOs Monterey
  • Browser Chrome
  • IMask version 6.6.1
  • Framework/plugin version React 18
@uNmAnNeR
Copy link
Owner

uNmAnNeR commented May 24, 2023

@aktanoff i still believe that current behaviour is correct as described in comment there:

/*
  radix should be mapped when
  1) input is done from keyboard = flags.input && flags.raw
  2) unmasked value is set = !flags.input && !flags.raw
  and should not be mapped when
  1) value is set = flags.input && !flags.raw
  2) raw value is set = !flags.input && flags.raw
*/

Mapping is done only on input. Consider the next example:

const m = new MaskedNumber({
  thousandsSeparator: '.',
  radix: ",",
  mapToRadix: ['.'], // note thousandsSeparator is here
  scale: 2,
});
m.value = '123.123,23'; // should be resolved as 123123.23 not 123.12

So in value radix should be already mapped.

@pumano
Copy link

pumano commented May 24, 2023

we experiences same problem today with angular-imask and resolution radix as ','. It's works correctly only for dot '.' radix. problem exist in 6.4.3

More details: when we add 123.00 then visually we can get 12300, but unmask value is correct

@aktanoff
Copy link
Author

@uNmAnNeR, okay i understood, is there any way to write universal function that sets masked value from 3 forms of sources?:

  1. From already fully masked value
  2. From partially masked value
  3. From unmasked value

if i use unmaskedValue to set this, then there is a problem in phone mask like this
{ mask: '+{7}(000)000-00-00' }

onAccept gives me unmaksed value like '79998886655', and if i put it into unmasked value, i'm getting '+7(799)988-86-65' value, which is wrong

In previous versions, like 6.4.2, it's been working with all 3 sources by just putting values in mask.value, but in latest version i'm getting problems in setting through mask.value only with Number mask.

@aktanoff
Copy link
Author

Checked again, my previous message was incorrect, sorry for that
I mean, sometime i need to put already formatted phone number into mask, and if i put it in phone number mask like
{ mask: '+{7}(000)000-00-00' }
It parses incorrectly if i pass it into unmaskedValue
mask.unmaskedValue = '+7 (999) 888-77-66' // +7 (799) 988-87-76
But if i put it into value property, it parses correctly

Example
https://codesandbox.io/s/react-imask-input-box-masks-forked-3x3diy?file=/src/index.js

@uNmAnNeR
Copy link
Owner

@aktanoff
i don't think it's possible to have a universal way to process values.
Right now there are 3 ways available:

  • masked - it's not just what the user types. The masked value is preprocessed by the mask. This seems logical as long as mask can add / replace new symbols along the way.
  • unmasked
  • typed

what you are asking for is probably about setting the value as if the user is typed it. I confirm that's not possible right now.
what i can do here is add method like resolve (value, flags). It's actually already there but without flags parameter. Does it work for you?

@aktanoff
Copy link
Author

@uNmAnNeR, yeah, i think it will be awesome, thank you!

@uNmAnNeR
Copy link
Owner

@aktanoff PR is merged. thank you. but keep in mind that resolve will not return a value anymore in 7.0.0

@aktanoff
Copy link
Author

@uNmAnNeR, how is it going to work then?
and when you will make next release?

@uNmAnNeR
Copy link
Owner

uNmAnNeR commented Jun 2, 2023

@aktanoff pretty the same but no return

resolve (value: string, flags: AppendFlags={ input: true }): void {

@uNmAnNeR
Copy link
Owner

uNmAnNeR commented Jun 2, 2023

@aktanoff released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants