Skip to content

Commit

Permalink
feat(angular): allow nullable options
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Oct 18, 2023
1 parent d48cdd6 commit 0d551b8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
10 changes: 9 additions & 1 deletion projects/angular/src/lib/maskito.cva.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskit
},
})
export class MaskitoCva {
private options?: MaskitoOptions | null;

@Input()
maskito: MaskitoOptions = MASKITO_DEFAULT_OPTIONS;
set maskito(options: MaskitoOptions | null | undefined) {
this.options = options;
}

get maskito() {

Check failure on line 30 in projects/angular/src/lib/maskito.cva.ts

View workflow job for this annotation

GitHub Actions / eslint

Missing return type on function
return this.options ?? MASKITO_DEFAULT_OPTIONS;
}

constructor(readonly accessor: DefaultValueAccessor) {
const original = accessor.writeValue.bind(accessor);
Expand Down
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
2 changes: 1 addition & 1 deletion projects/angular/src/lib/maskito.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {MaskitoOptions, maskitoTransform} from '@maskito/core';
name: 'maskito',
})
export class MaskitoPipe implements PipeTransform {
transform(value: unknown, maskitoOptions: MaskitoOptions): string {
transform(value: unknown, maskitoOptions?: MaskitoOptions | null): string {
return maskitoTransform(String(value ?? ''), maskitoOptions);
}
}
11 changes: 10 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 @@ -58,6 +58,15 @@ describe('Maskito Angular package', () => {

expect(getText()).toBe('12345,67');
expect(getValue()).toBe('12345,67');

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 {
Expand Down
6 changes: 5 additions & 1 deletion projects/core/src/lib/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import {MASKITO_DEFAULT_OPTIONS} from '../constants';
import {ElementState, MaskitoOptions} from '../types';
import {maskitoPipe} from './pipe';

export function maskitoTransform(
value: string,
maskitoOptions?: MaskitoOptions | null,
): string;
export function maskitoTransform(value: string, maskitoOptions: MaskitoOptions): string;
export function maskitoTransform(
state: ElementState,
Expand All @@ -11,7 +15,7 @@ export function maskitoTransform(

export function maskitoTransform(
valueOrState: ElementState | string,
maskitoOptions: MaskitoOptions,
maskitoOptions?: MaskitoOptions | null,
): ElementState | string {
const options: Required<MaskitoOptions> = {
...MASKITO_DEFAULT_OPTIONS,
Expand Down

0 comments on commit 0d551b8

Please sign in to comment.