Skip to content

Commit 1c9bbcc

Browse files
committed
fix clearing input by focus
1 parent 30472cd commit 1c9bbcc

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

projects/ngx-mask-lib/src/lib/mask.directive.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
225225
caretShift = shift;
226226
backspaceShift = _backspaceShift;
227227
});
228+
228229
// only set the selection if the element is active
229230
if (this.document.activeElement !== el) {
230231
return;
@@ -259,8 +260,17 @@ export class MaskDirective implements ControlValueAccessor, OnChanges {
259260
const el: HTMLInputElement = e.target as HTMLInputElement;
260261
const posStart = 0;
261262
const posEnd = 0;
263+
262264
if (this._maskService.showOnFocus) {
263265
this._maskService.showMaskTyped = true;
266+
267+
// Когда задано дефолтное значение
268+
if ( !this._maskService.valueWithMask && el.value ) {
269+
const currentValue = this._maskService.actualValue;
270+
const prefNmask = this._maskService.prefix + this._maskService.maskIsShown;
271+
this._maskService.valueWithMask = currentValue + prefNmask.slice( currentValue.length );
272+
}
273+
// this._maskService.applyValueChanges();
264274
}
265275
if (
266276
el !== null &&

projects/ngx-mask-lib/src/lib/mask.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export class MaskService extends MaskApplierService {
168168
}
169169

170170
public showMaskInInput(inputVal?: string): string {
171+
debugger
171172
if ((this.showMaskTyped || this.showOnFocus) && !!this.shownMaskExpression) {
172173
if (this.maskExpression.length !== this.shownMaskExpression.length) {
173174
throw new Error('Mask expression must match mask placeholder length');

src/app/app.component.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,45 @@ <h1 class="title">
3333
</div>
3434
</div>
3535

36+
<div class="container">
37+
<div class="row">
38+
<div class="col-12">
39+
<div class="mat-card-wr">
40+
<mat-card>
41+
<mat-card-header>
42+
<mat-card-title>Phone number</mat-card-title>
43+
<mat-card-subtitle>
44+
You can add 'placeHolderCharacter' property to changed the placeholder character used when displaying the
45+
mask.
46+
</mat-card-subtitle>
47+
</mat-card-header>
48+
<mat-card-content>
49+
<div class="flex-row">
50+
<div class="flex-cell-padding">
51+
<mat-form-field>
52+
<input
53+
matInput
54+
placeholder="Phone number"
55+
mask="(000) 000-0000"
56+
prefix="+5 "
57+
[(ngModel)]="test"
58+
[showOnFocus]="true"
59+
/>
60+
<mat-hint><b>Mask: </b>+5 (000) 000 0000</mat-hint>
61+
</mat-form-field>
62+
</div>
63+
<div class="flex-cell">
64+
<p><b>FormControl:</b> {{ form1.value ? form1.value : 'Empty' }}</p>
65+
<p><b>NgModel:</b> {{ showMaskModel ? showMaskModel : 'Empty' }}</p>
66+
</div>
67+
</div>
68+
</mat-card-content>
69+
</mat-card>
70+
</div>
71+
</div>
72+
</div>
73+
</div>
74+
3675
<div class="container">
3776
<div class="row">
3877
<div class="col-12">

src/app/app.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class AppComponent {
7373
public commaSeparatorForm!: FormControl;
7474
public commaPrecisionSeparatorForm!: FormControl;
7575
public commaZeroPrecisionSeparatorForm!: FormControl;
76+
public test = '5435 5 345';
7677

7778
public constructor() {
7879
this.form = new FormControl('');

src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
1616
import { BrowserModule } from '@angular/platform-browser';
1717
import { NgModule } from '@angular/core';
1818
import 'hammerjs';
19-
import { NgxMaskModule } from 'ngx-mask';
19+
2020

2121
import { AppComponent } from './app.component';
22+
import { NgxMaskModule } from 'ngx-mask';
2223

2324
@NgModule({
2425
declarations: [AppComponent, TestMaskComponent],

0 commit comments

Comments
 (0)