Skip to content

Commit

Permalink
fix(mask-directive): replaced native KeyboardEvent with type alias (#424
Browse files Browse the repository at this point in the history
)

in order to avoid server errors we have to patch the global object with
fake implementation of the KeyboardEvent and add a type alias so that
the global object path is executed right before the event reference in the code

fixes #399
close #399
  • Loading branch information
bpopnikolov authored and NepipenkoIgor committed Apr 23, 2019
1 parent adedb10 commit 7f972ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/app/ngx-mask/custom-keyboard-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// tslint:disable: no-any typedef
declare var global: any;

(function() {
if (!global.KeyboardEvent) {
global.KeyboardEvent = function(_eventType: any, _init: any) {};
}
})();

export type CustomKeyboardEvent = KeyboardEvent;
7 changes: 4 additions & 3 deletions src/app/ngx-mask/mask.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DOCUMENT } from '@angular/common';
import { ControlValueAccessor, FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR, ValidationErrors } from '@angular/forms';
import { MaskService } from './mask.service';
import { config, IConfig, withoutValidation } from './config';
import { CustomKeyboardEvent } from './custom-keyboard-event';

@Directive({
selector: '[mask]',
Expand Down Expand Up @@ -180,7 +181,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
}

@HostListener('input', ['$event'])
public onInput(e: KeyboardEvent): void {
public onInput(e: CustomKeyboardEvent): void {
const el: HTMLInputElement = e.target as HTMLInputElement;
this._inputValue = el.value;
if (!this._maskValue) {
Expand Down Expand Up @@ -218,7 +219,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
}

@HostListener('click', ['$event'])
public onFocus(e: MouseEvent | KeyboardEvent): void {
public onFocus(e: MouseEvent | CustomKeyboardEvent): void {
const el: HTMLInputElement = e.target as HTMLInputElement;
const posStart: number = 0;
const posEnd: number = 0;
Expand Down Expand Up @@ -252,7 +253,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
}

@HostListener('keydown', ['$event'])
public a(e: KeyboardEvent): void {
public a(e: CustomKeyboardEvent): void {
this._code = e.code;
const el: HTMLInputElement = e.target as HTMLInputElement;
this._maskService.selStart = el.selectionStart;
Expand Down

0 comments on commit 7f972ca

Please sign in to comment.