Skip to content

Commit

Permalink
Merge pull request #12452 from primefaces/issue-12426
Browse files Browse the repository at this point in the history
Fixed #12426 - InputMask | numbers entered in the middle of the field…
  • Loading branch information
cetincakiroglu authored Jan 17, 2023
2 parents c5acb47 + d56b104 commit 43f021d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/app/components/inputmask/inputmask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export class InputMask implements OnInit, ControlValueAccessor {

@Input() autocomplete: string;

@Input() keepBuffer: boolean = false;

@ViewChild('input', { static: true }) inputViewChild: ElementRef;

@Output() onComplete: EventEmitter<any> = new EventEmitter();
Expand Down Expand Up @@ -404,7 +406,9 @@ export class InputMask implements OnInit, ControlValueAccessor {
onInputBlur(e) {
this.focused = false;
this.onModelTouched();
this.checkVal();
if (!this.keepBuffer) {
this.checkVal();
}
this.updateFilledState();
this.onBlur.emit(e);

Expand Down Expand Up @@ -442,7 +446,11 @@ export class InputMask implements OnInit, ControlValueAccessor {
}

this.clearBuffer(begin, end);
this.shiftL(begin, end - 1);
if (this.keepBuffer) {
this.shiftL(begin, end - 2);
} else {
this.shiftL(begin, end - 1);
}
this.updateModel(e);
this.onInput.emit(e);

Expand Down Expand Up @@ -523,10 +531,12 @@ export class InputMask implements OnInit, ControlValueAccessor {
}

clearBuffer(start, end) {
let i;
for (i = start; i < end && i < this.len; i++) {
if (this.tests[i]) {
this.buffer[i] = this.getPlaceholder(i);
if (!this.keepBuffer) {
let i;
for (i = start; i < end && i < this.len; i++) {
if (this.tests[i]) {
this.buffer[i] = this.getPlaceholder(i);
}
}
}
}
Expand All @@ -549,7 +559,9 @@ export class InputMask implements OnInit, ControlValueAccessor {
while (pos++ < test.length) {
c = test.charAt(pos - 1);
if (this.tests[i].test(c)) {
this.buffer[i] = c;
if (!this.keepBuffer) {
this.buffer[i] = c;
}
lastMatch = i;
break;
}
Expand Down Expand Up @@ -599,7 +611,7 @@ export class InputMask implements OnInit, ControlValueAccessor {

this.focusText = this.inputViewChild.nativeElement.value;

pos = this.checkVal();
pos = this.keepBuffer ? this.inputViewChild.nativeElement.value.length : this.checkVal();

this.caretTimeoutId = setTimeout(() => {
if (this.inputViewChild.nativeElement !== this.inputViewChild.nativeElement.ownerDocument.activeElement) {
Expand Down
6 changes: 6 additions & 0 deletions src/app/showcase/components/inputmask/inputmaskdemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ <h5>Properties</h5>
<td>true</td>
<td>Clears the incomplete value on blur.</td>
</tr>
<tr>
<td>keepBuffer</td>
<td>boolean</td>
<td>false</td>
<td>When present, it specifies that whether to clean buffer value from model.</td>
</tr>
<tr>
<td>unmask</td>
<td>boolean</td>
Expand Down

0 comments on commit 43f021d

Please sign in to comment.