@@ -37,6 +37,8 @@ class PhoneInput extends React.Component {
3737 buttonClass : PropTypes . string ,
3838 dropdownClass : PropTypes . string ,
3939 searchClass : PropTypes . string ,
40+ // for styled-components
41+ className : PropTypes . string ,
4042
4143 autoFormat : PropTypes . bool ,
4244
@@ -125,6 +127,7 @@ class PhoneInput extends React.Component {
125127 buttonClass : '' ,
126128 dropdownClass : '' ,
127129 searchClass : '' ,
130+ className : '' ,
128131
129132 autoFormat : true ,
130133 enableAreaCodes : false ,
@@ -486,11 +489,13 @@ class PhoneInput extends React.Component {
486489 }
487490 }
488491
492+
493+
489494 handleFlagDropdownClick = ( e ) => {
490495 e . preventDefault ( ) ;
491496 if ( ! this . state . showDropdown && this . props . disabled ) return ;
492- const { preferredCountries, selectedCountry } = this . state
493- const allCountries = [ ... new Set ( preferredCountries . concat ( this . state . onlyCountries ) ) ] ;
497+ const { preferredCountries, onlyCountries , selectedCountry } = this . state
498+ const allCountries = this . concatPreferredCountries ( preferredCountries , onlyCountries ) ;
494499
495500 const highlightCountryIndex = allCountries . findIndex ( o =>
496501 o . dialCode === selectedCountry . dialCode && o . iso2 === selectedCountry . iso2 ) ;
@@ -620,7 +625,8 @@ class PhoneInput extends React.Component {
620625 showDropdown : false ,
621626 selectedCountry : newSelectedCountry ,
622627 freezeSelection : true ,
623- formattedNumber
628+ formattedNumber,
629+ searchValue : ''
624630 } , ( ) => {
625631 this . cursorToEnd ( ) ;
626632 if ( this . props . onChange ) this . props . onChange ( formattedNumber . replace ( / [ ^ 0 - 9 ] + / g, '' ) , this . getCountryData ( ) , e , formattedNumber ) ;
@@ -756,21 +762,26 @@ class PhoneInput extends React.Component {
756762
757763 if ( searchValue === '' && selectedCountry ) {
758764 const { onlyCountries } = this . state
759- highlightCountryIndex = preferredCountries . concat ( onlyCountries ) . findIndex ( o => o == selectedCountry ) ;
765+ highlightCountryIndex = this . concatPreferredCountries ( preferredCountries , onlyCountries ) . findIndex ( o => o == selectedCountry ) ;
760766 // wait asynchronous search results re-render, then scroll
761767 setTimeout ( ( ) => this . scrollTo ( this . getElement ( highlightCountryIndex ) ) , 100 )
762768 }
763769 this . setState ( { searchValue, highlightCountryIndex } ) ;
764770 }
765771
772+ concatPreferredCountries = ( preferredCountries , onlyCountries ) => {
773+ if ( preferredCountries . length > 0 ) { return [ ...new Set ( preferredCountries . concat ( onlyCountries ) ) ] }
774+ else { return onlyCountries }
775+ }
776+
766777 getDropdownCountryName = ( country ) => {
767778 return country . localName || country . name ;
768779 }
769780
770781 getSearchFilteredCountries = ( ) => {
771782 const { preferredCountries, onlyCountries, searchValue } = this . state
772783 const { enableSearch } = this . props
773- const allCountries = [ ... new Set ( preferredCountries . concat ( onlyCountries ) ) ] ;
784+ const allCountries = this . concatPreferredCountries ( preferredCountries , onlyCountries ) ;
774785 const sanitizedSearchValue = searchValue . trim ( ) . toLowerCase ( ) . replace ( '+' , '' ) ;
775786 if ( enableSearch && sanitizedSearchValue ) {
776787 // [...new Set()] to get rid of duplicates
@@ -942,7 +953,7 @@ class PhoneInput extends React.Component {
942953
943954 return (
944955 < div
945- className = { containerClasses }
956+ className = { ` ${ containerClasses } ${ this . props . className } ` }
946957 style = { this . props . style || this . props . containerStyle }
947958 onKeyDown = { this . handleKeydown } >
948959 { specialLabel && < div className = 'special-label' > { specialLabel } </ div > }
@@ -983,7 +994,7 @@ class PhoneInput extends React.Component {
983994 < div
984995 onClick = { disableDropdown ? undefined : this . handleFlagDropdownClick }
985996 className = { selectedFlagClasses }
986- title = { selectedCountry ? `${ selectedCountry . name } : + ${ selectedCountry . dialCode } ` : '' }
997+ title = { selectedCountry ? `${ selectedCountry . localName || selectedCountry . name } : + ${ selectedCountry . dialCode } ` : '' }
987998 tabIndex = { disableDropdown ? '-1' : '0' }
988999 role = 'button'
9891000 aria-haspopup = "listbox"
0 commit comments