ngx-lib-phone-input
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 17.3.0. NgxPhoneNumberInput is an Angular library for easy integration of phone number input fields in your Angular applications.
91 lines (77 loc) • 2.12 kB
text/typescript
import {
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
HostListener,
Input,
OnInit,
forwardRef,
} from '@angular/core';
import {
ControlValueAccessor,
FormControl,
NG_VALUE_ACCESSOR,
} from '@angular/forms';
import { CountryListItemType, countries } from 'country-list-json';
export class PhoneInputComponent
implements ControlValueAccessor, AfterViewInit
{
phone!: FormControl;
countriesWithCodes: CountryListItemType[] = [];
selectedCountry: any = {};
showOptions: boolean = false;
constructor(private elementRef: ElementRef) {
this.phone = new FormControl(); // Initialize FormControl
this.countriesWithCodes = countries;
this.selectedCountry = this.countriesWithCodes[0];
}
propagateChange: any = () => {};
ngAfterViewInit(): void {
if (this.selectedCountry) {
setTimeout(() => {
this.setValue();
});
}
this.phone.valueChanges.subscribe((value) => {
this.propagateChange(value);
});
}
setValue() {
this.phone.setValue(this.selectedCountry.dial_code);
}
writeValue(value: any): void {
this.phone.setValue(value);
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {}
showDropdown() {
this.showOptions = !this.showOptions;
}
onClick(event: Event) {
if (!this.elementRef.nativeElement.contains(event.target)) {
this.showOptions = false;
}
}
countrySelect(country: CountryListItemType) {
this.selectedCountry = country;
this.phone.setValue(this.selectedCountry.dial_code);
this.showOptions = false;
this.propagateChange(this.selectedCountry?.dial_code);
}
}