-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathphone-input.js
More file actions
45 lines (39 loc) · 1.48 KB
/
phone-input.js
File metadata and controls
45 lines (39 loc) · 1.48 KB
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
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { debug } from '@ember/debug';
import intlTelInput from 'intl-tel-input';
export default class PhoneInputComponent extends Component {
@service fetch;
@tracked iti;
@action setupIntlTelInput(element) {
this.iti = intlTelInput(element, {
containerClass: `w-full ${this.args.wrapperClass ?? ''}`,
initialCountry: 'auto',
separateDialCode: true,
formatAsYouType: true,
geoIpLookup: async (success, failure) => {
try {
const { country_code } = await this.fetch.get('lookup/whois');
success(country_code);
} catch (error) {
debug('Failed to lookup country code with whois API.');
failure(error);
}
},
utilsScript: '/assets/libphonenumber/utils.js',
});
if (typeof this.args.onInit === 'function') {
this.args.onInit(this.iti);
}
element.addEventListener('countrychange', this.args.onCountryChange);
}
@action onInput() {
const { onInput } = this.args;
const number = this.iti.getNumber(intlTelInput.utils.numberFormat.E164);
if (typeof onInput === 'function') {
onInput(number, ...arguments);
}
}
}