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

Cursor jumps in negative number mask #748

Closed
Res42 opened this issue Jun 3, 2020 · 3 comments
Closed

Cursor jumps in negative number mask #748

Res42 opened this issue Jun 3, 2020 · 3 comments

Comments

@Res42
Copy link

Res42 commented Jun 3, 2020

When programmatically setting the input value and then manually modifying a number input with negative number support, the cursor jumps before the negative sign.

When not setting the input programmatically, it works as expected.

Version info

Angular: 9.1.9
ngx-mask: 9.1.2

Minimal reproduction demo

https://stackblitz.com/edit/ngx-mask-negative-number-cursor-jump-bug

or

HTML:

<form [formGroup]="form">
  <input formControlName="input1" type="text" mask="separator.0" [allowNegativeNumbers]="true"/>
</form>

TypeScript:

form = this.formBuilder.group({
  input1: this.formBuilder.control(42),
})

How to reproduce

  1. Set the value of the control programmatically (for example: input1: this.formBuilder.control(42)).
  2. Clear the input by hand.
  3. Type the negative sign (-).

The cursor will jump to before the negative sign automatically:
ngx-mask-cursor-bug 43
This causes that if you write more than one character (-1), then the negative sign will be removed and the value will be 1.

A gif of the bug:
ngx-mask-cursor-bug-2
Keys pressed:

  1. Mouse left click in the input.
  2. Backspace.
  3. Backspace.
  4. Negative sign (-).
  5. 1.

Expected

I don't want the cursor to jump.

Debug

I debugged it a little, but I don't know the library enough to create a PR for this bug.

As I see it, the programmatic value change calls the MaskDirective.writeValue method which sets this._maskService.isNumberValue = true;.
When manually modifying the value, the MaskService._checkSymbols('-') method will try to parse the input as a number (return Number(separatorValue);) which will return NaN, and this somehow causes the cursor jump.

Workaround

If somebody else has this error too, set [dropSpecialCharacters]="false" and parse the string output of ngx-mask yourself.

Duplicate

This issue may be a duplicate of #736.

@vishmay-AF
Copy link

vishmay-AF commented Sep 4, 2020

Same happens for round braces, after typing when I continuously click the backspace button to erase everything, the symbol on the first location is jumped from backspace.

Regex
(000) 0000 0000

After placing backspace
from '(0|' to '|(' in single backspace.
Here, consider '|' as the cursor

@vishmay-AF
Copy link

possible duplicate of #692

@Res42
Copy link
Author

Res42 commented Feb 16, 2021

I updated my repro to the latest versions (Angular 11, ngx-mask 11.1.4) and I cannot reproduce the problem with these versions.

Closing this issue.

@Res42 Res42 closed this as completed Feb 16, 2021
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

2 participants