Skip to content

Commit

Permalink
Update digit-only.directive.ts
Browse files Browse the repository at this point in the history
added an allowPaste input that can be set to false in markup to prevent pasting into an digitOnly input field
  • Loading branch information
billn6 authored Sep 3, 2021
1 parent 7ad10f3 commit bc4bf3d
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions projects/uiowa/digit-only/src/lib/digit-only.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class DigitOnlyDirective implements OnChanges {
@Input() decimal = false;
@Input() decimalSeparator = '.';
@Input() allowNegatives= false;
@Input() allowPaste = true;
@Input() negativeSign = '-';
@Input() min = -Infinity;
@Input() max = Infinity;
Expand Down Expand Up @@ -132,23 +133,29 @@ export class DigitOnlyDirective implements OnChanges {
e.preventDefault();
}
}

@HostListener('paste', ['$event'])
onPaste(event: any): void {
let pastedInput: string = '';
if ((window as { [key: string]: any })['clipboardData']) {
// Browser is IE
pastedInput = (window as { [key: string]: any })['clipboardData'].getData(
'text'
);
} else if (event.clipboardData && event.clipboardData.getData) {
// Other browsers
pastedInput = event.clipboardData.getData('text/plain');
}
if (this.allowPaste === true) {
let pastedInput: string = '';
if ((window as { [key: string]: any })['clipboardData']) {
// Browser is IE
pastedInput = (window as { [key: string]: any })['clipboardData'].getData(
'text'
);
} else if (event.clipboardData && event.clipboardData.getData) {
// Other browsers
pastedInput = event.clipboardData.getData('text/plain');
}

this.pasteData(pastedInput);
event.preventDefault();
this.pasteData(pastedInput);
event.preventDefault();
} else { // this prevents the paste
event.preventDefault();
event.stopPropagation();
}
}


@HostListener('drop', ['$event'])
onDrop(event: DragEvent): void {
Expand Down

0 comments on commit bc4bf3d

Please sign in to comment.