Skip to content

Commit f9b4945

Browse files
committed
Use smaller phone number library on phone input
1 parent 4049897 commit f9b4945

File tree

3 files changed

+45
-47
lines changed

3 files changed

+45
-47
lines changed

packages/boxel-ui/addon/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@embroider/addon-shim": "^1.8.9",
4141
"@floating-ui/dom": "^1.6.3",
4242
"@glint/template": "1.3.0",
43+
"awesome-phonenumber": "^7.2.0",
4344
"classnames": "^2.3.2",
4445
"countries-list": "^3.1.1",
4546
"dayjs": "^1.11.7",
@@ -60,7 +61,6 @@
6061
"ember-velcro": "^2.1.3",
6162
"file-loader": "^6.2.0",
6263
"focus-trap": "^7.4.3",
63-
"libphonenumber-js": "^1.11.15",
6464
"lodash": "^4.17.21",
6565
"pluralize": "^8.0.0",
6666
"tracked-built-ins": "^3.2.0",

packages/boxel-ui/addon/src/components/phone-input/index.gts

+34-33
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import type { TemplateOnlyComponent } from '@ember/component/template-only';
21
import { action } from '@ember/object';
32
import Component from '@glimmer/component';
43
import { tracked } from '@glimmer/tracking';
5-
import { type TCountryCode, countries, getEmojiFlag } from 'countries-list';
64
import {
7-
type CountryCallingCode,
8-
type CountryCode,
9-
getCountries,
10-
getCountryCallingCode,
11-
getExampleNumber,
12-
isValidPhoneNumber,
13-
} from 'libphonenumber-js';
14-
// @ts-expect-error import not found
15-
import examples from 'libphonenumber-js/mobile/examples';
5+
getCountryCodeForRegionCode,
6+
getExample,
7+
getSupportedRegionCodes,
8+
parsePhoneNumber,
9+
} from 'awesome-phonenumber';
10+
import { type TCountryCode, countries, getEmojiFlag } from 'countries-list';
1611
import { debounce } from 'lodash';
1712

1813
import { type InputValidationState } from '../input/index.gts';
@@ -30,19 +25,20 @@ interface Signature {
3025
}
3126

3227
interface CountryInfo {
33-
callingCode?: CountryCallingCode;
34-
code: CountryCode;
28+
callingCode?: string;
29+
code: string;
3530
example?: {
36-
callingCode: CountryCallingCode;
31+
callingCode: string;
3732
nationalNumber: string;
3833
};
3934
flag?: string;
4035
name?: string;
4136
}
4237

43-
const getCountryInfo = (countryCode: CountryCode): CountryInfo | undefined => {
44-
let example = getExampleNumber(countryCode, examples);
45-
let callingCode = getCountryCallingCode(countryCode);
38+
const getCountryInfo = (countryCode: string): CountryInfo | undefined => {
39+
let example = getExample(countryCode);
40+
let callingCode = getCountryCodeForRegionCode(countryCode);
41+
4642
let c = countries[countryCode as TCountryCode];
4743
if (c === undefined) {
4844
//here some country code may not be found due to the discrepancy between countries-list and libphonenumber-js library
@@ -52,13 +48,13 @@ const getCountryInfo = (countryCode: CountryCode): CountryInfo | undefined => {
5248
}
5349
return {
5450
code: countryCode,
55-
callingCode,
51+
callingCode: callingCode.toString(),
5652
name: c ? c.name : undefined,
5753
flag: getEmojiFlag(countryCode as TCountryCode),
5854
example: example
5955
? {
60-
callingCode,
61-
nationalNumber: example.format('NATIONAL'),
56+
callingCode: callingCode.toString(),
57+
nationalNumber: example.number?.international ?? '',
6258
}
6359
: undefined,
6460
};
@@ -73,18 +69,16 @@ class PhoneInput extends Component<Signature> {
7369
@action onSelectItem(item: CountryInfo): void {
7470
this.selectedItem = item;
7571
if (this.input.length > 0) {
76-
this.validationState = isValidPhoneNumber(
77-
this.input,
78-
this.selectedItem.code,
79-
)
80-
? 'valid'
81-
: 'invalid';
72+
const parsedPhoneNumber = parsePhoneNumber(this.input, {
73+
regionCode: this.selectedItem.code,
74+
});
75+
this.validationState = parsedPhoneNumber.valid ? 'valid' : 'invalid';
8276
}
8377
}
8478

8579
constructor(owner: unknown, args: any) {
8680
super(owner, args);
87-
this.items = getCountries()
81+
this.items = getSupportedRegionCodes()
8882
.map((code) => {
8983
return getCountryInfo(code);
9084
})
@@ -107,10 +101,17 @@ class PhoneInput extends Component<Signature> {
107101
}
108102

109103
private debouncedInput = debounce((input: string) => {
110-
this.validationState = isValidPhoneNumber(input, this.selectedItem.code)
111-
? 'valid'
112-
: 'invalid';
113104
this.input = input;
105+
106+
if (input === '') {
107+
this.validationState = 'initial';
108+
return;
109+
}
110+
111+
const parsedPhoneNumber = parsePhoneNumber(input, {
112+
regionCode: this.selectedItem.code,
113+
});
114+
this.validationState = parsedPhoneNumber.valid ? 'valid' : 'invalid';
114115
//save when the state is valid
115116
if (this.validationState === 'valid') {
116117
this.args.onInput(this.input);
@@ -151,13 +152,13 @@ export interface SelectedItemSignature {
151152
Element: HTMLDivElement;
152153
}
153154

154-
const PhoneSelectedItem: TemplateOnlyComponent<SelectedItemSignature> =
155-
// eslint-disable-next-line prettier/prettier TODO: fix this eslint error --> Insert `·[` prettier/prettier
155+
class PhoneSelectedItem extends Component<SelectedItemSignature> {
156156
<template>
157157
<div>
158158
{{@option.flag}}
159159
+{{@option.callingCode}}
160160
</div>
161-
</template>;
161+
</template>
162+
}
162163

163164
export default PhoneInput;

pnpm-lock.yaml

+10-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)