Skip to content

Commit

Permalink
feat(angular): allow nullable options (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Oct 18, 2023
1 parent d48cdd6 commit 21eaa7c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
9 changes: 7 additions & 2 deletions projects/angular/src/lib/maskito.cva.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
};
}
}
7 changes: 5 additions & 2 deletions projects/angular/src/lib/maskito.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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,
);
});
}

Expand Down
9 changes: 6 additions & 3 deletions projects/angular/src/lib/maskito.pipe.ts
Original file line number Diff line number Diff line change
@@ -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,
);
}
}
13 changes: 12 additions & 1 deletion projects/angular/src/lib/maskito.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}) => {
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 21eaa7c

Please sign in to comment.