Merge branch 'master' of github.com:thingsboard/thingsboard

This commit is contained in:
Igor Kulikov 2022-11-03 15:44:15 +02:00
commit e94624a313

View File

@ -97,8 +97,9 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
if (this.defaultCountry) { if (this.defaultCountry) {
this.getFlagAndPhoneNumberData(this.defaultCountry); this.getFlagAndPhoneNumberData(this.defaultCountry);
} }
if (this.phoneFormGroup) { if (this.phoneFormGroup && this.phoneFormGroup.get('phoneNumber').value) {
this.defineCountryFromNumber(this.phoneFormGroup.get('phoneNumber').value); const parsedPhoneNumber = this.parsePhoneNumberFromString(this.phoneFormGroup.get('phoneNumber').value);
this.defineCountryFromNumber(parsedPhoneNumber);
} }
} }
} }
@ -110,7 +111,7 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
private countryCallingCode = '+'; private countryCallingCode = '+';
private modelValue: string; private modelValue: string;
private changeSubscriptions: Subscription[] = []; private changeSubscriptions: Subscription[] = [];
private validators: ValidatorFn[] = [(c: FormControl) => Validators.pattern(this.getPhoneNumberPattern())(c), this.validatePhoneNumber()]; private validators: ValidatorFn[] = [this.validatePhoneNumber()];
private propagateChange = (v: any) => { }; private propagateChange = (v: any) => { };
@ -133,8 +134,12 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
}); });
this.changeSubscriptions.push(this.phoneFormGroup.get('phoneNumber').valueChanges.subscribe(value => { this.changeSubscriptions.push(this.phoneFormGroup.get('phoneNumber').valueChanges.subscribe(value => {
this.updateModel(); let parsedPhoneNumber = null;
this.defineCountryFromNumber(value); if (value && this.parsePhoneNumberFromString) {
parsedPhoneNumber = this.parsePhoneNumberFromString(value);
this.defineCountryFromNumber(parsedPhoneNumber);
}
this.updateModel(parsedPhoneNumber);
})); }));
this.changeSubscriptions.push(this.phoneFormGroup.get('country').valueChanges.subscribe(value => { this.changeSubscriptions.push(this.phoneFormGroup.get('country').valueChanges.subscribe(value => {
@ -184,6 +189,10 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
return String.fromCodePoint(...countryCode.split('').map(country => this.baseCode + country.charCodeAt(0))); return String.fromCodePoint(...countryCode.split('').map(country => this.baseCode + country.charCodeAt(0)));
} }
private updateModelValueInFormat(parsedPhoneNumber: any) {
this.modelValue = parsedPhoneNumber.format('E.164');
}
validatePhoneNumber(): ValidatorFn { validatePhoneNumber(): ValidatorFn {
return (c: FormControl) => { return (c: FormControl) => {
const phoneNumber = c.value; const phoneNumber = c.value;
@ -201,19 +210,12 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
}; };
} }
private defineCountryFromNumber(phoneNumber) { private defineCountryFromNumber(parsedPhoneNumber) {
if (phoneNumber && this.parsePhoneNumberFromString) {
const parsedPhoneNumber = this.parsePhoneNumberFromString(phoneNumber);
const country = this.phoneFormGroup.get('country').value; const country = this.phoneFormGroup.get('country').value;
if (parsedPhoneNumber?.country && parsedPhoneNumber?.country !== country) { if (parsedPhoneNumber?.country && parsedPhoneNumber?.country !== country) {
this.phoneFormGroup.get('country').patchValue(parsedPhoneNumber.country, {emitEvent: true}); this.phoneFormGroup.get('country').patchValue(parsedPhoneNumber.country, {emitEvent: true});
} }
} }
}
private getPhoneNumberPattern(): RegExp {
return new RegExp(`^${this.countryCallingCode.replace('+', '\\+')}$|^\\+[1-9]\\d{1,14}$`);
}
validate(): ValidationErrors | null { validate(): ValidationErrors | null {
const phoneNumber = this.phoneFormGroup.get('phoneNumber'); const phoneNumber = this.phoneFormGroup.get('phoneNumber');
@ -247,6 +249,8 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
if (phoneNumber) { if (phoneNumber) {
const parsedPhoneNumber = this.parsePhoneNumberFromString(phoneNumber); const parsedPhoneNumber = this.parsePhoneNumberFromString(phoneNumber);
if (parsedPhoneNumber?.isValid() && parsedPhoneNumber?.isPossible()) { if (parsedPhoneNumber?.isValid() && parsedPhoneNumber?.isPossible()) {
country = parsedPhoneNumber?.country || this.defaultCountry;
this.updateModelValueInFormat(parsedPhoneNumber);
this.isLegacy = false; this.isLegacy = false;
} else { } else {
const validators = [Validators.maxLength(255)]; const validators = [Validators.maxLength(255)];
@ -260,18 +264,19 @@ export class PhoneInputComponent implements OnInit, ControlValueAccessor, Valida
this.isLegacy = false; this.isLegacy = false;
} }
this.phoneFormGroup.updateValueAndValidity({emitEvent: false}); this.phoneFormGroup.updateValueAndValidity({emitEvent: false});
country = phoneNumber ? this.parsePhoneNumberFromString(phoneNumber)?.country || this.defaultCountry : this.defaultCountry;
this.getFlagAndPhoneNumberData(country); this.getFlagAndPhoneNumberData(country);
} }
this.phoneFormGroup.reset({phoneNumber, country}, {emitEvent: false}); this.phoneFormGroup.reset({phoneNumber, country}, {emitEvent: false});
} }
private updateModel() { private updateModel(parsedPhoneNumber?) {
const phoneNumber = this.phoneFormGroup.get('phoneNumber'); const phoneNumber = this.phoneFormGroup.get('phoneNumber');
if (phoneNumber.value === '+' || phoneNumber.value === this.countryCallingCode) { if (phoneNumber.value === '+' || phoneNumber.value === this.countryCallingCode) {
this.propagateChange(null); this.propagateChange(null);
} else if (phoneNumber.valid) { } else if (phoneNumber.valid) {
this.modelValue = phoneNumber.value; if (parsedPhoneNumber) {
this.updateModelValueInFormat(parsedPhoneNumber);
}
this.propagateChange(this.modelValue); this.propagateChange(this.modelValue);
} else { } else {
this.propagateChange(null); this.propagateChange(null);