1
+ "use client" ;
2
+
1
3
import { ChangeEvent , KeyboardEvent , useCallback , useMemo , useRef , useState } from "react" ;
2
4
3
5
import { PhoneNumber , usePhoneOptions } from "./types" ;
@@ -10,11 +12,8 @@ const slots = new Set(".");
10
12
11
13
export const getMetadata = ( rawValue : string , countriesList : typeof countries = countries , country : any = null ) => {
12
14
country = country == null && rawValue . startsWith ( "44" ) ? "gb" : country ;
13
- if ( country != null ) {
14
- countriesList = countriesList . filter ( ( c ) => c [ 0 ] === country ) ;
15
- countriesList = countriesList . sort ( ( a , b ) => b [ 2 ] . length - a [ 2 ] . length ) ;
16
- }
17
- return countriesList . find ( ( c ) => rawValue . startsWith ( c [ 2 ] ) ) ;
15
+ if ( country != null ) countriesList = countriesList . filter ( ( c ) => c [ 0 ] === country ) ;
16
+ return [ ...countriesList ] . sort ( ( a , b ) => b [ 2 ] . length - a [ 2 ] . length ) . find ( ( c ) => rawValue . startsWith ( c [ 2 ] ) ) ;
18
17
}
19
18
20
19
export const getCountry = ( countryCode : keyof typeof countries ) => {
@@ -36,8 +35,9 @@ export const cleanInput = (input: any, pattern: string) => {
36
35
return Array . from ( pattern , c => input [ 0 ] === c || slots . has ( c ) ? input . shift ( ) || c : c ) ;
37
36
}
38
37
39
- export const getFormattedNumber = ( rawValue : any , pattern : string ) => {
38
+ export const getFormattedNumber = ( rawValue : any , pattern ? : string ) => {
40
39
/** Returns the reformatted input value based on the given pattern */
40
+ pattern = pattern || getMetadata ( rawValue ) ?. [ 3 ] || "" ;
41
41
return displayFormat ( cleanInput ( rawValue , pattern . replaceAll ( / \d / g, "." ) ) . join ( "" ) ) ;
42
42
}
43
43
0 commit comments