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

Using controlled component causes strange behavior #62

Open
sergeyzwezdin opened this issue Oct 17, 2023 · 3 comments
Open

Using controlled component causes strange behavior #62

sergeyzwezdin opened this issue Oct 17, 2023 · 3 comments

Comments

@sergeyzwezdin
Copy link

After connecting use-mask-input, using controlled input (passing value/onChange controlled outside), the caret puts on the first character.

Tried to use useMemo to avoid re-rendering of Input component and set its state via ref, but nothing changed.

Here is how it behaves:
2023-10-17 at 11 12 38

Repro project could be found here - https://stackblitz.com/edit/stackblitz-starters-hak8pb?file=app%2Finput.tsx

I think it could be considered as a bug.

@eduardoborges
Copy link
Owner

Hey @sergeyzwezdin, yes its a bug... im working on a solution. Thanks for reporting.

@eduardoborges
Copy link
Owner

@sergeyzwezdin I detected when using controlled component the mask alias not works as espected.

For now, you can pass full mask declaration:

const ref = useInputMask({
  mask: '9{1,}[.9{2}]',
  options: {
    placeholder: '',
    rightAlign: true,
  },
});

This example represents a decimal with optional numbers after radix point.

@RHarryH
Copy link

RHarryH commented Nov 22, 2023

@sergeyzwezdin, @eduardoborges My workaround for that issue is to add onChange event with below content:

const caret = event.target.selectionStart;
const element = event.target;
 window.requestAnimationFrame(() => {
    element.selectionStart = caret;
    element.selectionEnd = caret;
})

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