Skip to content
Open
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
17 changes: 15 additions & 2 deletions src/mf-mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges {
@Input()
public mfMask: string = '(999) 999-9999';

private inputValue: string = '';

public onChange = (_: any) => { };
public onTouch = () => { };

public placeholderChar = '_';

private inputValue: string = '';

constructor(
@Inject(MFMASK_CONFIG) private config: IConfig,
private elementRef: ElementRef
Expand All @@ -43,6 +45,10 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges {
}
}

public ngOnInit(): void {
this.applyMask();
}

@HostListener('input', ['$event'])
public onInput(input: InputEvent) {
const el = input.target as HTMLInputElement;
Expand Down Expand Up @@ -134,6 +140,13 @@ export class MfMaskDirective implements ControlValueAccessor, OnChanges {
}
}

for (let i = newValue.length; i < this.mfMask.length; i++) {
if (this.mfMask[i] === 'A' || this.mfMask[i] === '9') {
newValue = newValue + this.placeholderChar;
} else {
newValue = newValue + this.mfMask[i];
}
}
setTimeout(() => this.onChange(newValue));
this.elementRef.nativeElement.value = newValue;
}
Expand Down