Looking to transform the Phone Mask solution below into an Angular component. Does anyone have a method to accomplish this? * Any solution that results in a similar component for a Phone textbox will suffice.
Mask for an Input to allow phone numbers?
https://stackblitz.com/edit/angular6-phone-mask
I attempted to copy the code into the component below, but I'm encountering errors:
The phonebox allows text to exceed 10 characters.
During debugging, when deleting all characters, a character value remains.
The initial answer uses a directive and only functions with form control. The objective is to create a custom company textbox component with its own styling, inputs, etc.
At the end, we make reference to the StackBlitz code.
Typescript:
export class CustomFieldErrorMatcher implements ErrorStateMatcher {
constructor(private customControl: FormControl,private errors:any) { }
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
return this.customControl && this.customControl.touched &&(this.customControl.invalid || this.errors);
}
}
@Component({
selector: 'app-input-phone',
templateUrl: './input-phone.component.html',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => InputPhoneComponent),
multi: true
}
]})
export class InputPhoneComponent implements OnInit, ControlValueAccessor {
@Input() MaxLength: string;
@Input() ReadOnly: boolean;
@Input() Value: string;
@Input() type: string;
@Input() Label: string;
@Input() PlaceHolder: string;
@Output() saveValue = new EventEmitter();
@Output() onStateChange = new EventEmitter();
@Input() errors: any = null;
disabled: boolean;
...
</code></pre>
<p><strong>HTML:</strong></p>
<pre><code><div class="input-wrap">
<mat-form-field>
<mat-label>{{Label}}</mat-label>
<input
matInput
[attr.maxlength] = "MaxLength"
[value]="Value ? Value : ''"
[placeholder]="PlaceHolder ? PlaceHolder : ''"
[readonly]="ReadOnly"
[type]="type ? type: 'text'"
[ngModel]="Value"
[errorStateMatcher]="errorMatcher()"
(input)="onChange($event.target.value)"
(blur)="onTouched()"
(change)="saveValueAction($event)"
(ngModelChange)="Value=$event"
>
</mat-form-field>
</div>