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

When working with useIMask, it does not allow you to enter fractional numbers with currency #1118

Open
MaxGuraevskiy opened this issue Mar 24, 2025 · 0 comments

Comments

@MaxGuraevskiy
Copy link

MaxGuraevskiy commented Mar 24, 2025

Describe the bug

            blocks: {
              d: {
                mask: Number,
                min: 0,
                radix: ',',
                mapToRadix: [',', '.'],
                scale: 2
              }
            },
            mask: `d ${RUB}`,
            lazy: false

Such a mask should be able to enter numbers like 100.35 ₽. But this is not happening.

This starts working if you write setValue(value) in onAccept, but the cursor position becomes incorrect (it shifts to the last position). When you enter a new digit or click in the input area, the cursor position shifts to the correct position.

If you do setUnmaskedValue(mask.unmaskedValue) in onAccept, then the cursor is positioned correctly relative to the additional character, but you will not be able to enter the fractional part. onAccept is called a second time, but with a value without . or ,

To Reproduce
#990
You can run an example from here, it's almost mine. The same problem is observed there.

Expected behavior
updateValue and alignCursor in onAccept don't help.

This can only be fixed in a separate useEffect, where you can bind to the input value in the mask.

  useEffect(() => {
    maskRef.current?.alignCursor()
  }, [maskRef.current?.value])

Environment:

  • OS: MacOS
  • Browser: All
  • IMask version: 7.5.0, 7.6.1
  • Framework/plugin: React 19

P.S. - There is also a bug with the fact that setUnmaskedValue does not change the value, but only unmaskedValue, so you have to use it like this

  useEffect(() => {
    if (unmaskedValue !== value) {
      setUnmaskedValue(value)
      // костыль setUnmaskedValue не меняет почему-то value, а updateValue не работает
      setValue(value)
    }
    // setTimeout(() => {
    //   maskRef.current?.updateValue()
    // }, 0)
  }, [value])

And updateValue doesn't help. So that it doesn't interfere when entering numbers with a fractional part, you have to wrap it in setTimeout. But when the state is changed from the outside, it does not work.

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

1 participant