Skip to content

Commit 8228cfe

Browse files
bl00mberdavid58coronellwTaras Semenyukalicerocheman
committed
update
* add preferredCountries check according to #492 * clear search field after selection, close #388 * integrate i18n fix for title, close #462 * add className to work with styled-components, close #477 * Update README, CHANGELOG Co-authored-by: Dávid Barbora <[email protected]> Co-authored-by: Wiston Coronell <[email protected]> Co-authored-by: Taras Semenyuk <[email protected]> Co-authored-by: Alice <[email protected]> Co-authored-by: rubenofen <[email protected]>
1 parent c4eb555 commit 8228cfe

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.15.0 (February 3, 2022)
2+
* Hungarian, Polish, Korean translation
3+
* numerous fixes
4+
5+
16
## 2.13.8 (August 25, 2020)
27
* `specialLabel`, `disableCountryGuess`
38
* Add some missing flags to css

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ Country data object not returns from onKeyDown event
296296
/>
297297
```
298298

299-
### Predefined localizations
299+
### Predefined localization
300300
`es` Spanish, `de` Deutsch, `ru` Russian, `fr` French<br/>
301301
`jp` Japanese, `cn` Chinese, `pt` Portuguese, `it` Italian<br/>
302302
`ir` Iranian, `ar` Arabic, `tr` Turkish, `id` Indonesian<br/>
303-
`hu` Hungarian
303+
`hu` Hungarian, `pl` Polish, `ko` Korean
304304

305305
```jsx
306306
import es from 'react-phone-input-2/lang/es.json'
@@ -469,6 +469,31 @@ import startsWith from 'lodash.startswith';
469469
### Clear country
470470
To clear `country`, pass `null` as `value`.
471471

472+
### Dynamic placeholder
473+
<details>
474+
<summary>Show</summary>
475+
476+
```jsx
477+
const phoneCountryFormat = useMemo(() => phoneCountry?.format || undefined, [phoneCountry]);
478+
const placeholder = useMemo(() => {
479+
if (phoneCountryFormat) {
480+
const words = phoneCountryFormat.split(' ');
481+
words.shift(); // I'm using only local numbers so here I remove the country code from the format
482+
let text = words.join(' ');
483+
// first digit is special on french numbers, these 3 lines could be removed
484+
if (country === 'fr') {
485+
text = text.replace('.', '6');
486+
}
487+
while (text.indexOf('.') > -1) {
488+
text = text.replace('.', `${Math.min(9, Math.max(1, Math.floor(Math.random() * 10)))}`);
489+
}
490+
return text;
491+
}
492+
return '';
493+
}, [phoneCountryFormat, country]);
494+
```
495+
</details>
496+
472497
### CDN
473498
```html
474499
<script src="https://unpkg.com/[email protected]/dist/lib.js"></script>

src/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)