@@ -9,11 +9,13 @@ import {
9
9
type CountryCode ,
10
10
getExampleNumber ,
11
11
type CountryCallingCode ,
12
+ isValidPhoneNumber ,
12
13
} from ' libphonenumber-js' ;
13
14
// @ts-expect-error import not found
14
15
import examples from ' libphonenumber-js/mobile/examples' ;
15
16
import { countries , type TCountryCode , getEmojiFlag } from ' countries-list' ;
16
- import { TemplateOnlyComponent } from ' @ember/component/template-only' ;
17
+ import type { TemplateOnlyComponent } from ' @ember/component/template-only' ;
18
+ import { debounce } from ' lodash' ;
17
19
18
20
interface Signature {
19
21
Args: {};
@@ -58,9 +60,19 @@ const getCountryInfo = (countryCode: CountryCode): CountryInfo | undefined => {
58
60
export class PhoneInput extends Component <Signature > {
59
61
@tracked items: Array <CountryInfo > = [];
60
62
@tracked selectedItem: CountryInfo = getCountryInfo (' US' )! ;
63
+ @tracked validationState: InputValidationState = ' initial' ;
64
+ @tracked input: string = ' ' ;
61
65
62
66
@action onSelectItem(item : CountryInfo ): void {
63
67
this .selectedItem = item ;
68
+ if (this .input .length > 0 ) {
69
+ this .validationState = isValidPhoneNumber (
70
+ this .input ,
71
+ this .selectedItem .code ,
72
+ )
73
+ ? ' valid'
74
+ : ' invalid' ;
75
+ }
64
76
}
65
77
66
78
constructor (owner : unknown , args : any ) {
@@ -79,14 +91,26 @@ export class PhoneInput extends Component<Signature> {
79
91
return undefined ;
80
92
}
81
93
82
- get validationState(): InputValidationState {
83
- return this .placeholder ? ' valid' : ' invalid' ;
94
+ get phoneNumber(): string {
95
+ return ` +${this .selectedItem .callingCode } ` ;
96
+ }
97
+
98
+ @action onInput(v : string ): void {
99
+ this .debouncedInput (v );
84
100
}
85
101
102
+ private debouncedInput = debounce ((input : string ) => {
103
+ this .validationState = isValidPhoneNumber (input , this .selectedItem .code )
104
+ ? ' valid'
105
+ : ' invalid' ;
106
+ this .input = input ;
107
+ }, 300 );
108
+
86
109
<template >
87
110
<BoxelInputGroup
88
111
@ placeholder ={{this .placeholder }}
89
112
@ state ={{this .validationState }}
113
+ @ onInput ={{this .onInput }}
90
114
>
91
115
<: before as | Accessories | >
92
116
<Accessories.Select
0 commit comments