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

feat(angular): allow nullable options #605

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
splincode marked this conversation as resolved.
Show resolved Hide resolved
});

function getText(): string {
return fixture.debugElement.nativeElement
.querySelector('#pipe')
Expand Down