-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
intl-tel-input.component.ts
65 lines (54 loc) · 2.23 KB
/
intl-tel-input.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { AfterViewInit, Component, ElementRef, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { ControlContainer, NgForm } from '@angular/forms';
import intlTelInput, {Iti, SomeOptions} from 'intl-tel-input';
@Component({
selector: 'intl-tel-input',
templateUrl: './intl-tel-input.component.html',
styleUrls: ['./intl-tel-input.component.scss'],
viewProviders: [{ provide: ControlContainer, useExisting: NgForm }]
})
export class IntlTelInputComponent implements AfterViewInit {
@Input() cssClass!: string;
@Input() E164PhoneNumber!: string | null;
@Input() label!: string;
@Input() labelCssClass!: string;
@Input() name = 'intl-tel-input-name';
@Input() options: SomeOptions = {};
@Input() required!: boolean;
@Input() isMobileOnly: boolean = true;
@Output() private E164PhoneNumberChange = new EventEmitter<string | null>();
@ViewChild('intlTelInput') private _inputElement!: ElementRef;
private _phoneNumber!: string;
private _intlTelInput!: Iti;
ngAfterViewInit(): void {
this._intlTelInput = intlTelInput(this._inputElement.nativeElement, this.options);
}
get intlTelInput(): Iti {
return this._intlTelInput;
}
get phoneNumber(): string {
return this._phoneNumber;
}
set phoneNumber(value: string) {
if (!!value) {
this._intlTelInput.setNumber(value);
}
this._phoneNumber = value;
this.i18nizePhoneNumber();
}
i18nizePhoneNumber(): void {
this.E164PhoneNumber = null;
if (this._intlTelInput.isValidNumber(this.isMobileOnly)) {
this.E164PhoneNumber = this._intlTelInput.getNumber();
}
this.E164PhoneNumberChange.emit(this.E164PhoneNumber);
}
}