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

Updating the mask in Angular breaks the form unmask="typed" integration #500

Closed
dstj opened this issue Jun 24, 2021 · 6 comments
Closed

Comments

@dstj
Copy link

dstj commented Jun 24, 2021

Hi,

I'm updating the [imask] from a radio button to switch the nested mask from "percentage" to "dollar". I'm using unmask="typed" with a twist to support nullable nested Number as in #432 with an Angular Form.

It's originally bound to "Fixed" and works no problem (i.e. input shows "$ 123" and the form value is 123 as expected) .

However, after updating the [imask], the value in the Angular form is no longer unmasked (i.e. "$ 123" is the form value instead of 123)

My input is created like so:

<input type="text" formControlName="amount" [imask]="iMaskCfg" unmask="typed">

...
  iMaskCfg = this.createCurrencyMask();

  private createCurrencyMask() {
    return new NullableMaskedNumberBlock({
      mask: `$ d`,
      lazy: false,
      blocks: {
        d: {
          mask: Number,
          thousandsSeparator: '',
          scale: 2,
          signed: true,
          radix: '.'
        }
      }
    });
  }

...

class NullableMaskedNumberBlock extends IMask.MaskedPattern {
  // @ts-ignore
  get typedValue() {
    const numericBlock = this.maskedBlock('d');
    return numericBlock.unmaskedValue !== ''
      ? // @ts-ignore
        numericBlock.typedValue
      : null;
  }
  set typedValue(num) {
    const numericBlock = this.maskedBlock('d');
    // @ts-ignore
    numericBlock.typedValue = num;
  }
}

I tried updating the mask using both:

but same problem with both methods.

See repro on Stackblitz:

Possibly related to #359, I'm not sure.

Using imask v6.0.7 with both Angular 9 and 11

@uNmAnNeR
Copy link
Owner

actually imask works, but you have to pass { mask: YourMask } instead of just YourMask to updateOptions like this https://stackblitz.com/edit/angular-ivy-fmrkzv

@uNmAnNeR uNmAnNeR unpinned this issue Jul 21, 2021
@dstj
Copy link
Author

dstj commented Jul 21, 2021

@uNmAnNeR

Thanks. I'll do that!

Should Angular rebinding work in the same way as well?

Right now, this does not work:

<input #tb type="text" formControlName="amount" [imask]="iMaskCfg" unmask="typed">
...

iMaskCfg = this.createCurrencyMask();  // initial setup
...

if (type === 'Percentage') {
      this.iMaskCfg = this.createPctMask();
    }
    else {
      this.iMaskCfg = this.createCurrencyMask();
    }   

@uNmAnNeR
Copy link
Owner

uNmAnNeR commented Jul 22, 2021

@dstj same for cfg: this.iMaskCfg = { mask: this.createPctMask() };

@dstj
Copy link
Author

dstj commented Jul 22, 2021

@uNmAnNeR Thanks for letting me know.

I'm confused as to why that initial iMaskCfg = this.createCurrencyMask(); worked in the first place though... it should have been iMaskCfg = { mask: this.createCurrencyMask() }; I guess.

Thanks again.

@uNmAnNeR
Copy link
Owner

yeah, really confusing. I am thinking to get it work with masked instance directly.

@DywiTom
Copy link

DywiTom commented Oct 5, 2022

How can i use NullableMaskedNumberBlock without creating an object manually for each input?

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

No branches or pull requests

3 participants