From 3f61a69f46590c642954378e9c6586316cf02b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Mendon=C3=A7a?= Date: Mon, 3 Apr 2017 23:54:08 -0300 Subject: [PATCH] Fixed the issue #15. --- src/input.handler.ts | 21 +++++++++++++++------ src/input.service.ts | 6 +++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/input.handler.ts b/src/input.handler.ts index d015ab5..fbdbf32 100644 --- a/src/input.handler.ts +++ b/src/input.handler.ts @@ -58,13 +58,19 @@ export class InputHandler { handleKeydown(event: any): void { let keyCode = event.which || event.charCode || event.keyCode; - if (keyCode === 8 || keyCode === 46 || keyCode === 63272) { + if (keyCode == 8 || keyCode == 46 || keyCode == 63272) { event.preventDefault(); + let selectionRangeLength = Math.abs(this.inputService.inputSelection.selectionEnd - this.inputService.inputSelection.selectionStart); - if (this.inputService.inputSelection.selectionEnd === this.inputService.inputSelection.selectionStart) { + if (selectionRangeLength == 0) { this.inputService.removeNumber(keyCode); this.onModelChange(this.inputService.value); } + + if (selectionRangeLength == this.inputService.rawValue.length) { + this.setValue(0); + this.onModelChange(this.inputService.value); + } } } @@ -81,11 +87,14 @@ export class InputHandler { this.inputService.changeToNegative(); break; default: - if (!this.inputService.canInputMoreNumbers) { - return; - } + let selectionRangeLength = Math.abs(this.inputService.inputSelection.selectionEnd - this.inputService.inputSelection.selectionStart); + + if (this.inputService.canInputMoreNumbers && (selectionRangeLength == 0 || selectionRangeLength == this.inputService.rawValue.length)) { + if (selectionRangeLength == this.inputService.rawValue.length) { + this.setValue(0); + } + - if (this.inputService.inputSelection.selectionEnd === this.inputService.inputSelection.selectionStart) { this.inputService.addNumber(keyCode); } } diff --git a/src/input.service.ts b/src/input.service.ts index 267b0f9..cad4ec0 100644 --- a/src/input.service.ts +++ b/src/input.service.ts @@ -21,7 +21,7 @@ export class InputService { } applyMask(isNumber: boolean, rawValue: string): string { - let {allowNegative, decimal, precision, prefix, suffix, thousands} = this.options; + let { allowNegative, decimal, precision, prefix, suffix, thousands } = this.options; rawValue = isNumber ? new Number(rawValue).toFixed(precision) : rawValue; let onlyNumbers = rawValue.replace(/[^0-9]/g, ""); @@ -80,8 +80,8 @@ export class InputService { selectionStart = this.rawValue.length - this.options.suffix.length; } - selectionEnd = keyCode === 46 || keyCode === 63272 ? selectionEnd + 1 : selectionEnd; - selectionStart = keyCode === 8 ? selectionStart - 1 : selectionStart; + selectionEnd = keyCode == 46 || keyCode == 63272 ? selectionEnd + 1 : selectionEnd; + selectionStart = keyCode == 8 ? selectionStart - 1 : selectionStart; this.rawValue = this.rawValue.substring(0, selectionStart) + this.rawValue.substring(selectionEnd, this.rawValue.length); this.updateFieldValue(selectionStart); }