Skip to content

Commit

Permalink
Fixed the issue #15.
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarrew committed Apr 4, 2017
1 parent 9dd06fa commit 3f61a69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
21 changes: 15 additions & 6 deletions src/input.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand All @@ -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);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 3f61a69

Please sign in to comment.