@@ -12,6 +12,7 @@ const usePhoneTester = ({
12
12
onlyCountries = [ ] ,
13
13
excludeCountries = [ ] ,
14
14
preferredCountries = [ ] ,
15
+ disableParentheses = false ,
15
16
} ) => {
16
17
const initiatedRef = useRef < boolean > ( false ) ;
17
18
const [ query , setQuery ] = useState < string > ( "" ) ;
@@ -31,6 +32,7 @@ const usePhoneTester = ({
31
32
onlyCountries,
32
33
excludeCountries,
33
34
preferredCountries,
35
+ disableParentheses,
34
36
} ) ;
35
37
36
38
const update = useCallback ( ( value : string ) => {
@@ -50,7 +52,8 @@ const usePhoneTester = ({
50
52
const search = useCallback ( setQuery , [ ] ) ;
51
53
52
54
const select = useCallback ( ( isoCode : string ) => {
53
- const mask = ( countriesList . find ( ( [ iso ] ) => iso === isoCode ) as any ) [ 3 ] ;
55
+ const pattern = ( countriesList . find ( ( [ iso ] ) => iso === isoCode ) as any ) [ 3 ] ;
56
+ const mask = disableParentheses ? pattern . replace ( / [ ( ) ] / g, "" ) : pattern ;
54
57
setValue ( displayFormat ( cleanInput ( mask , mask ) . join ( "" ) ) ) ;
55
58
setCountryCode ( isoCode ) ;
56
59
} , [ setValue , countriesList ] ) ;
@@ -121,6 +124,24 @@ describe("Verifying the functionality of hooks", () => {
121
124
expect ( ( result . current . metadata as any ) [ 0 ] ) . toBe ( "am" ) ;
122
125
} )
123
126
127
+ it ( "Check usePhone without parentheses" , ( ) => {
128
+ const { result} = renderHook ( usePhoneTester , {
129
+ initialProps : {
130
+ country : "au" ,
131
+ disableParentheses : true ,
132
+ }
133
+ } ) ;
134
+
135
+ act ( ( ) => result . current . update ( "6104111" ) ) ;
136
+
137
+ expect ( result . current . value ) . toBe ( "+61 0 4111" ) ;
138
+ expect ( ( result . current . metadata as any ) [ 0 ] ) . toBe ( "au" ) ;
139
+
140
+ act ( ( ) => result . current . select ( "ms" ) ) ;
141
+
142
+ expect ( result . current . value ) . toBe ( "+1 664" ) ;
143
+ } )
144
+
124
145
it ( "Check usePhone for country detection" , ( ) => {
125
146
const { result} = renderHook ( usePhoneTester , {
126
147
initialProps : { }
0 commit comments