Refactoring

This commit is contained in:
fe-dev 2022-05-30 14:54:41 +03:00
parent baa681634b
commit 19c313dbed
4 changed files with 44 additions and 11 deletions

View File

@ -27,7 +27,7 @@
</mat-option> </mat-option>
</mat-select> </mat-select>
</div> </div>
<mat-form-field class="phone-input"> <mat-form-field class="phone-input" [floatLabel]="showLabel">
<mat-label translate>phone-input.phone-input-label</mat-label> <mat-label translate>phone-input.phone-input-label</mat-label>
<input <input
formControlName="phoneNumber" formControlName="phoneNumber"
@ -35,6 +35,7 @@
matInput matInput
[placeholder]="phonePlaceholder" [placeholder]="phonePlaceholder"
[pattern]="phoneNumberPattern" [pattern]="phoneNumberPattern"
(focus)="focus()"
autocomplete="off" autocomplete="off"
[required]="required"> [required]="required">
<mat-error *ngIf="phoneFormGroup.get('phoneNumber').hasError('required')"> <mat-error *ngIf="phoneFormGroup.get('phoneNumber').hasError('required')">

View File

@ -59,6 +59,15 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
@Input() enableFlagsSelect = true; @Input() enableFlagsSelect = true;
@Input() required = true; @Input() required = true;
private floatLabel = 'always';
get showLabel(): string {
return this.floatLabel;
}
@Input()
set showLabel(value) {
this.floatLabel = value ? 'always' : 'never';
}
allCountries: Array<Country> = []; allCountries: Array<Country> = [];
phonePlaceholder: string; phonePlaceholder: string;
countryCallingCode: string; countryCallingCode: string;
@ -82,11 +91,9 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
} }
this.phoneFormGroup = this.fb.group({ this.phoneFormGroup = this.fb.group({
country: [this.defaultCountry, []], country: [this.defaultCountry, []],
phoneNumber: [ phoneNumber: [null, this.required ? [Validators.required] : []]
this.countryCallingCode,
this.required ? [Validators.required, Validators.pattern(phoneNumberPattern), this.validatePhoneNumber()] : []
]
}); });
this.phoneFormGroup.get('phoneNumber').setValidators([Validators.pattern(phoneNumberPattern), this.validatePhoneNumber()]);
this.valueChange$ = this.phoneFormGroup.get('phoneNumber').valueChanges.subscribe(() => { this.valueChange$ = this.phoneFormGroup.get('phoneNumber').valueChanges.subscribe(() => {
this.updateModel(); this.updateModel();
@ -97,12 +104,14 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
const code = this.countryCallingCode; const code = this.countryCallingCode;
this.getFlagAndPhoneNumberData(value); this.getFlagAndPhoneNumberData(value);
let phoneNumber = this.phoneFormGroup.get('phoneNumber').value; let phoneNumber = this.phoneFormGroup.get('phoneNumber').value;
if (code !== this.countryCallingCode && phoneNumber) { if (phoneNumber) {
phoneNumber = phoneNumber.replace(code, this.countryCallingCode); if (code !== this.countryCallingCode && phoneNumber.includes(code)) {
} else { phoneNumber = phoneNumber.replace(code, this.countryCallingCode);
phoneNumber = this.countryCallingCode; } else {
phoneNumber = this.countryCallingCode;
}
this.phoneFormGroup.get('phoneNumber').patchValue(phoneNumber);
} }
this.phoneFormGroup.get('phoneNumber').patchValue(phoneNumber);
} }
}); });
@ -123,6 +132,13 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
} }
} }
focus() {
const phoneNumber = this.phoneFormGroup.get('phoneNumber');
if (!phoneNumber.value) {
phoneNumber.patchValue(this.countryCallingCode);
}
}
getFlagAndPhoneNumberData(country) { getFlagAndPhoneNumberData(country) {
if (this.enableFlagsSelect) { if (this.enableFlagsSelect) {
this.flagIcon = this.getFlagIcon(country); this.flagIcon = this.getFlagIcon(country);

View File

@ -1,3 +1,19 @@
///
/// Copyright © 2016-2022 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
export interface Country { export interface Country {

View File

@ -3355,7 +3355,7 @@
"phone-input-label": "Phone number", "phone-input-label": "Phone number",
"phone-input-required": "Phone number is required", "phone-input-required": "Phone number is required",
"phone-input-validation": "Phone number is invalid or not possible", "phone-input-validation": "Phone number is invalid or not possible",
"phone-input-pattern": "Invalid phone number. Should be in E.164 format, ex. +19995550123." "phone-input-pattern": "Invalid phone number. Should be in E.164 format, ex. {{phoneNumber}}"
}, },
"custom": { "custom": {
"widget-action": { "widget-action": {