diff --git a/projects/angular/src/lib/maskito.cva.ts b/projects/angular/src/lib/maskito.cva.ts index a38772b34..43137ef5d 100644 --- a/projects/angular/src/lib/maskito.cva.ts +++ b/projects/angular/src/lib/maskito.cva.ts @@ -21,13 +21,18 @@ import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskit }) export class MaskitoCva { @Input() - maskito: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; + maskito: MaskitoOptions | null = MASKITO_DEFAULT_OPTIONS; constructor(readonly accessor: DefaultValueAccessor) { const original = accessor.writeValue.bind(accessor); accessor.writeValue = (value: unknown) => { - original(maskitoTransform(String(value ?? ''), this.maskito)); + original( + maskitoTransform( + String(value ?? ''), + this.maskito ?? MASKITO_DEFAULT_OPTIONS, + ), + ); }; } } diff --git a/projects/angular/src/lib/maskito.directive.ts b/projects/angular/src/lib/maskito.directive.ts index 0c5af25a5..4a399d131 100644 --- a/projects/angular/src/lib/maskito.directive.ts +++ b/projects/angular/src/lib/maskito.directive.ts @@ -23,7 +23,7 @@ export class MaskitoDirective implements OnDestroy, OnChanges { private maskedElement: Maskito | null = null; @Input() - maskito: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; + maskito: MaskitoOptions | null = MASKITO_DEFAULT_OPTIONS; @Input() maskitoElement: MaskitoElementPredicate | MaskitoElementPredicateAsync = @@ -47,7 +47,10 @@ export class MaskitoDirective implements OnDestroy, OnChanges { } this.ngZone.runOutsideAngular(() => { - this.maskedElement = new Maskito(predicateResult, this.maskito); + this.maskedElement = new Maskito( + predicateResult, + this.maskito ?? MASKITO_DEFAULT_OPTIONS, + ); }); } diff --git a/projects/angular/src/lib/maskito.pipe.ts b/projects/angular/src/lib/maskito.pipe.ts index 1781be337..b5d53254e 100644 --- a/projects/angular/src/lib/maskito.pipe.ts +++ b/projects/angular/src/lib/maskito.pipe.ts @@ -1,11 +1,14 @@ import {Pipe, PipeTransform} from '@angular/core'; -import {MaskitoOptions, maskitoTransform} from '@maskito/core'; +import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskito/core'; @Pipe({ name: 'maskito', }) export class MaskitoPipe implements PipeTransform { - transform(value: unknown, maskitoOptions: MaskitoOptions): string { - return maskitoTransform(String(value ?? ''), maskitoOptions); + transform(value: unknown, maskitoOptions: MaskitoOptions | null): string { + return maskitoTransform( + String(value ?? ''), + maskitoOptions ?? MASKITO_DEFAULT_OPTIONS, + ); } } diff --git a/projects/angular/src/lib/maskito.spec.ts b/projects/angular/src/lib/maskito.spec.ts index e2fa631c1..91efe68fc 100644 --- a/projects/angular/src/lib/maskito.spec.ts +++ b/projects/angular/src/lib/maskito.spec.ts @@ -17,7 +17,7 @@ describe('Maskito Angular package', () => { }) class TestComponent { readonly control = new FormControl(); - readonly options: MaskitoOptions = { + options: MaskitoOptions | null = { mask: /^\d+(,\d{0,2})?$/, preprocessors: [ ({elementState, data}) => { @@ -60,6 +60,17 @@ describe('Maskito Angular package', () => { expect(getValue()).toBe('12345,67'); }); + it('disable mask formatting if options is null', () => { + fixture.componentInstance.options = null; + fixture.detectChanges(); + + fixture.componentInstance.control.setValue(123456.9999); + fixture.detectChanges(); + + expect(getText()).toBe('123456.9999'); + expect(getValue()).toBe('123456.9999'); + }); + function getText(): string { return fixture.debugElement.nativeElement .querySelector('#pipe')