@@ -32,6 +32,7 @@ import {
32
32
usePhone ,
33
33
} from "../phone-hooks" ;
34
34
35
+ import locale from "./locale" ;
35
36
import { injectMergedStyles } from "./styles" ;
36
37
import { PhoneInputProps , PhoneNumber } from "./types" ;
37
38
@@ -46,6 +47,8 @@ const PhoneInput = forwardRef(({
46
47
onlyCountries = [ ] ,
47
48
excludeCountries = [ ] ,
48
49
preferredCountries = [ ] ,
50
+ searchNotFound : defaultSearchNotFound = "No country found" ,
51
+ searchPlaceholder : defaultSearchPlaceholder = "Search country" ,
49
52
dropdownRender = ( node ) => node ,
50
53
onMount : handleMount = ( ) => null ,
51
54
onInput : handleInput = ( ) => null ,
@@ -66,8 +69,8 @@ const PhoneInput = forwardRef(({
66
69
const [ countryCode , setCountryCode ] = useState < string > ( country ) ;
67
70
68
71
const {
69
- searchNotFound = "No country found" ,
70
- searchPlaceholder = "Search country" ,
72
+ searchNotFound = defaultSearchNotFound ,
73
+ searchPlaceholder = defaultSearchPlaceholder ,
71
74
countries = new Proxy ( { } , ( { get : ( _ : any , prop : any ) => prop } ) ) ,
72
75
} = ( locale as any ) . PhoneInput || { } ;
73
76
@@ -130,6 +133,7 @@ const PhoneInput = forwardRef(({
130
133
const phoneMetadata = parsePhoneNumber ( formattedNumber , countriesList ) ;
131
134
setCountryCode ( phoneMetadata . isoCode as any ) ;
132
135
setValue ( formattedNumber ) ;
136
+ setQuery ( "" ) ;
133
137
handleChange ( { ...phoneMetadata , valid : ( strict : boolean ) => checkValidity ( phoneMetadata , strict ) } , event ) ;
134
138
} , [ countriesList , handleChange , pattern , setValue ] )
135
139
@@ -180,6 +184,18 @@ const PhoneInput = forwardRef(({
180
184
setValue ( formattedNumber ) ;
181
185
} , [ countriesList , metadata , onMount , pattern , setValue , value ] )
182
186
187
+ const suffixIcon = useMemo ( ( ) => {
188
+ return enableArrow && (
189
+ < span role = "img" className = "anticon" style = { { paddingLeft : 8 } } >
190
+ < svg className = "icon" viewBox = "0 0 1024 1024"
191
+ focusable = "false" fill = "currentColor" width = "16" height = "18" >
192
+ < path
193
+ d = "M848 368a48 48 0 0 0-81.312-34.544l-0.016-0.016-254.784 254.784-251.488-251.488a48 48 0 1 0-71.04 64.464l-0.128 0.128 288 288 0.016-0.016a47.84 47.84 0 0 0 34.544 14.688h0.224a47.84 47.84 0 0 0 34.544-14.688l0.016 0.016 288-288-0.016-0.016c8.32-8.624 13.44-20.368 13.44-33.312z" />
194
+ </ svg >
195
+ </ span >
196
+ ) ;
197
+ } , [ enableArrow ] )
198
+
183
199
const countriesSelect = useMemo ( ( ) => (
184
200
< Select
185
201
suffixIcon = { null }
@@ -203,7 +219,6 @@ const PhoneInput = forwardRef(({
203
219
} }
204
220
optionLabelProp = "label"
205
221
dropdownStyle = { { minWidth} }
206
- notFoundContent = { searchNotFound }
207
222
onDropdownVisibleChange = { onDropdownVisibleChange }
208
223
dropdownRender = { ( menu ) => (
209
224
< div className = { `${ prefixCls } -phone-input-search-wrapper` } >
@@ -215,10 +230,22 @@ const PhoneInput = forwardRef(({
215
230
onInput = { ( { target} : any ) => setQuery ( target . value ) }
216
231
/>
217
232
) }
218
- { menu }
233
+ { countriesList . length ? menu : (
234
+ < div className = "ant-select-item-empty" > { searchNotFound } </ div >
235
+ ) }
219
236
</ div >
220
237
) }
221
238
>
239
+ < Select . Option
240
+ children = { null }
241
+ value = { selectValue }
242
+ style = { { display : "none" } }
243
+ key = { `${ countryCode } _default` }
244
+ label = { < >
245
+ < div className = { `flag ${ countryCode } ` } />
246
+ { suffixIcon }
247
+ </ > }
248
+ />
222
249
{ countriesList . map ( ( [ iso , name , dial , pattern ] ) => {
223
250
const mask = disableParentheses ? pattern . replace ( / [ ( ) ] / g, "" ) : pattern ;
224
251
return (
@@ -227,15 +254,7 @@ const PhoneInput = forwardRef(({
227
254
key = { `${ iso } _${ mask } ` }
228
255
label = { < >
229
256
< div className = { `flag ${ iso } ` } />
230
- { enableArrow && (
231
- < span role = "img" className = "anticon" style = { { paddingLeft : 8 } } >
232
- < svg className = "icon" viewBox = "0 0 1024 1024"
233
- focusable = "false" fill = "currentColor" width = "16" height = "18" >
234
- < path
235
- d = "M848 368a48 48 0 0 0-81.312-34.544l-0.016-0.016-254.784 254.784-251.488-251.488a48 48 0 1 0-71.04 64.464l-0.128 0.128 288 288 0.016-0.016a47.84 47.84 0 0 0 34.544 14.688h0.224a47.84 47.84 0 0 0 34.544-14.688l0.016 0.016 288-288-0.016-0.016c8.32-8.624 13.44-20.368 13.44-33.312z" />
236
- </ svg >
237
- </ span >
238
- ) }
257
+ { suffixIcon }
239
258
</ > }
240
259
children = { < div className = { `${ prefixCls } -phone-input-select-item` } >
241
260
< div className = { `flag ${ iso } ` } />
@@ -245,7 +264,7 @@ const PhoneInput = forwardRef(({
245
264
)
246
265
} ) }
247
266
</ Select >
248
- ) , [ selectValue , query , enableArrow , disabled , disableParentheses , disableDropdown , onDropdownVisibleChange , minWidth , searchNotFound , countries , countriesList , setFieldValue , setValue , prefixCls , enableSearch , searchPlaceholder ] )
267
+ ) , [ selectValue , suffixIcon , countryCode , query , disabled , disableParentheses , disableDropdown , onDropdownVisibleChange , minWidth , searchNotFound , countries , countriesList , setFieldValue , setValue , prefixCls , enableSearch , searchPlaceholder ] )
249
268
250
269
return (
251
270
< div className = { `${ prefixCls } -phone-input-wrapper` }
@@ -266,3 +285,4 @@ const PhoneInput = forwardRef(({
266
285
} )
267
286
268
287
export default PhoneInput ;
288
+ export type { PhoneInputProps , PhoneNumber , locale } ;
0 commit comments