Skip to content

Commit

Permalink
refactor and bump up version
Browse files Browse the repository at this point in the history
  • Loading branch information
changhuixu committed Jan 6, 2020
1 parent 868dc25 commit 87d5058
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion projects/uiowa/digit-only/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@uiowa/digit-only",
"version": "1.2.1",
"version": "1.2.2",
"author": "Changhui Xu <[email protected]>",
"description": "An Angular directive only allows [0-9] in the input box when typing, pasting or drag/dropping. This directive handles both Windows keyboard and Mac keyboard.",
"keywords": [
Expand Down
25 changes: 12 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 @@ -60,27 +60,25 @@ export class DigitOnlyDirective {

@HostListener('paste', ['$event'])
onPaste(event: ClipboardEvent) {
const pastedInput: string = event.clipboardData.getData('text/plain');
this.pasteData(pastedInput);
event.preventDefault();
let pastedInput: string = event.clipboardData.getData('text/plain');
pastedInput = this.sanatizeInput(pastedInput);
const pasted = document.execCommand('insertText', false, pastedInput);
if (!pasted) {
const { selectionStart: start, selectionEnd: end } = this.inputElement;
this.inputElement.setRangeText(pastedInput, start, end, 'end');
}
}

@HostListener('drop', ['$event'])
onDrop(event: DragEvent) {
event.preventDefault();
let textData = event.dataTransfer.getData('text');
const textData = event.dataTransfer.getData('text');
this.inputElement.focus();
this.pasteData(textData);
event.preventDefault();
}

textData = this.sanatizeInput(textData);
const pasted = document.execCommand('insertText', false, textData);
private pasteData(pastedContent: string): void {
const sanitizedContent = this.sanatizeInput(pastedContent);
const pasted = document.execCommand('insertText', false, sanitizedContent);
if (!pasted) {
const { selectionStart: start, selectionEnd: end } = this.inputElement;
this.inputElement.setRangeText(textData, start, end, 'end');
this.inputElement.setRangeText(sanitizedContent, start, end, 'end');
}
}

Expand All @@ -91,7 +89,8 @@ export class DigitOnlyDirective {
return input.replace(/[^0-9]/g, '');
}
}
isValidDecimal(string: string): boolean {

private isValidDecimal(string: string): boolean {
return string.split('.').length <= 2;
}
}

0 comments on commit 87d5058

Please sign in to comment.