@@ -32,6 +32,7 @@ import {
3232 usePhone ,
3333} from "../phone-hooks" ;
3434
35+ import locale from "./locale" ;
3536import { injectMergedStyles } from "./styles" ;
3637import { PhoneInputProps , PhoneNumber } from "./types" ;
3738
@@ -46,6 +47,8 @@ const PhoneInput = forwardRef(({
4647 onlyCountries = [ ] ,
4748 excludeCountries = [ ] ,
4849 preferredCountries = [ ] ,
50+ searchNotFound : defaultSearchNotFound = "No country found" ,
51+ searchPlaceholder : defaultSearchPlaceholder = "Search country" ,
4952 dropdownRender = ( node ) => node ,
5053 onMount : handleMount = ( ) => null ,
5154 onInput : handleInput = ( ) => null ,
@@ -66,8 +69,8 @@ const PhoneInput = forwardRef(({
6669 const [ countryCode , setCountryCode ] = useState < string > ( country ) ;
6770
6871 const {
69- searchNotFound = "No country found" ,
70- searchPlaceholder = "Search country" ,
72+ searchNotFound = defaultSearchNotFound ,
73+ searchPlaceholder = defaultSearchPlaceholder ,
7174 countries = new Proxy ( { } , ( { get : ( _ : any , prop : any ) => prop } ) ) ,
7275 } = ( locale as any ) . PhoneInput || { } ;
7376
@@ -130,6 +133,7 @@ const PhoneInput = forwardRef(({
130133 const phoneMetadata = parsePhoneNumber ( formattedNumber , countriesList ) ;
131134 setCountryCode ( phoneMetadata . isoCode as any ) ;
132135 setValue ( formattedNumber ) ;
136+ setQuery ( "" ) ;
133137 handleChange ( { ...phoneMetadata , valid : ( strict : boolean ) => checkValidity ( phoneMetadata , strict ) } , event ) ;
134138 } , [ countriesList , handleChange , pattern , setValue ] )
135139
@@ -180,6 +184,18 @@ const PhoneInput = forwardRef(({
180184 setValue ( formattedNumber ) ;
181185 } , [ countriesList , metadata , onMount , pattern , setValue , value ] )
182186
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+
183199 const countriesSelect = useMemo ( ( ) => (
184200 < Select
185201 suffixIcon = { null }
@@ -203,7 +219,6 @@ const PhoneInput = forwardRef(({
203219 } }
204220 optionLabelProp = "label"
205221 dropdownStyle = { { minWidth} }
206- notFoundContent = { searchNotFound }
207222 onDropdownVisibleChange = { onDropdownVisibleChange }
208223 dropdownRender = { ( menu ) => (
209224 < div className = { `${ prefixCls } -phone-input-search-wrapper` } >
@@ -215,10 +230,22 @@ const PhoneInput = forwardRef(({
215230 onInput = { ( { target} : any ) => setQuery ( target . value ) }
216231 />
217232 ) }
218- { menu }
233+ { countriesList . length ? menu : (
234+ < div className = "ant-select-item-empty" > { searchNotFound } </ div >
235+ ) }
219236 </ div >
220237 ) }
221238 >
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+ />
222249 { countriesList . map ( ( [ iso , name , dial , pattern ] ) => {
223250 const mask = disableParentheses ? pattern . replace ( / [ ( ) ] / g, "" ) : pattern ;
224251 return (
@@ -227,15 +254,7 @@ const PhoneInput = forwardRef(({
227254 key = { `${ iso } _${ mask } ` }
228255 label = { < >
229256 < 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 }
239258 </ > }
240259 children = { < div className = { `${ prefixCls } -phone-input-select-item` } >
241260 < div className = { `flag ${ iso } ` } />
@@ -245,7 +264,7 @@ const PhoneInput = forwardRef(({
245264 )
246265 } ) }
247266 </ 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 ] )
249268
250269 return (
251270 < div className = { `${ prefixCls } -phone-input-wrapper` }
@@ -266,3 +285,4 @@ const PhoneInput = forwardRef(({
266285} )
267286
268287export default PhoneInput ;
288+ export type { PhoneInputProps , PhoneNumber , locale } ;
0 commit comments