JsDaddy / ngx-mask

Angular Plugin to make masks on form fields and html elements.
https://jsdaddy.github.io/ngx-mask
MIT License
1.16k stars 300 forks source link

Mask with `shownMaskExpression` and special characters/patterns cleans the special character when it is not supposed #1254

Closed tiago-eusebio-dev closed 1 year ago

tiago-eusebio-dev commented 1 year ago

🐞 bug report

Is this a regression?

No.

Description

I have a complex duration input with the 00D : 00H : 00M : 00S mask, on which the user can only enter digits. I have defined each of the mask's non-digit characters as specialCharacters and created custom patterns for each of them so the user cannot write over them (snippet below). The bug is that when pressing the backspace key on the "H" or "D" character, the mask's "M" character is replaced with a 0. When using another character in the place of the "M" (eg.: "Y"), the issue doesn't happen.

Press backspace here: image

Result: image

// The default specialCharacters + custom
specialCharacters: [
    '-',
    '/',
    '(',
    ')',
    '.',
    ':',
    ' ',
    '+',
    ',',
    '@',
    '[',
    ']',
    '"',
    "'",
    'D', // custom
    'H', // custom
    'M', // custom
    '\\S', // custom
],
// The default patterns + custom
patterns: {
    '0': { pattern: /\d/ },
    '9': { pattern: /\d/, optional: true },
    A: { pattern: /[a-zA-Z0-9]/ },
    L: { pattern: /[a-z]/ },
    S: { pattern: /[a-zA-Z]/ },
    U: { pattern: /[A-Z]/ },
    X: { pattern: /\d/, symbol: '*' },
    d: { pattern: /\d/ },
    h: { pattern: /\d/ },
    m: { pattern: /\d/ },
    s: { pattern: /\d/ },
    D: { pattern: /D/ }, // custom: the D on the mask can only be the D character
    H: { pattern: /H/ }, // custom: the H on the mask can only be the H character
    M: { pattern: /M/ }, // custom: the M on the mask can only be the M character
    '\\S': { pattern: /\S/ }, // custom: the S on the mask can only be the S character. Escape it to prevent digits from being removed from the value
},

🔬 Minimal Reproduction

Stackblitz demo: https://stackblitz.com/edit/ngx-time?file=src%2Fmain.ts

🔥 Exception or Error

None.

🌍 Your Environment

Angular CLI: 16.2.6 Node: 16.20.2 ngx-mask: 16.3.9

Anything else relevant?

I had to escape the "S" character otherwise it wouldn't work. I think it is another bug when replacing characters.

andriikamaldinov1 commented 1 year ago

@tiago-eusebio-dev Thanks for using Ngx-Mask. Please update to latest version. It example all work as expected - https://stackblitz.com/edit/ngx-time-gwqnkt?file=src%2Fmain.ts

tiago-eusebio-dev commented 1 year ago

@andriikamaldinov1 Thank you. I confirm that it is fixed now.

On a side note, do you have any comment about having to escape the 'S' character? Shouldn't this be treated by ngx-mask itself?