Skip to content

Commit 83725ea

Browse files
authored
Merge pull request #250 from Bustuk/master
Allow defaultCountry prop to accept both iso2 and dialCode
2 parents 499b732 + b8482fa commit 83725ea

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

docs/documentation/v3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Read more on `vue-form-generator`'s [instruction page](https://icebob.gitbooks.i
107107
| -------- | ---- | ------------- | ----------- |
108108
| `autocomplete` | `String` | `'on'` | Native input 'autocomplete' attribute |
109109
| `autofocus` | `Boolean` | `false` | Native input 'autofocus' attribute |
110-
| `defaultCountry` | `String` | `''` | Default country, will override the country fetched from IP address of user |
110+
| `defaultCountry` | `String` | `Number` | `''` | Default country (by iso2 or dialCode), will override the country fetched from IP address of user |
111111
| `disabled` | `Boolean` | `false` | Disable input field |
112112
| `disabledFetchingCountry` | `Boolean` | `false` | Disable fetching current country based on IP address of user |
113113
| `dropdownOptions` | `Object` | `{ disabledDialCode: false, tabindex: 0 }` | Options for dropdown, supporting `disabledDialCode` and `tabindex`|

src/components/vue-tel-input.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ describe('Props', () => {
5858
expect(wrapper.find('.vti__selection > .vti__flag').classes()).toContain('au');
5959
});
6060
});
61+
describe(':defaultCountryByDialCode', () => {
62+
it('shows correct default country by dial code', async () => {
63+
const wrapper = shallowMount(VueTelInput, {
64+
propsData: {
65+
defaultCountry: 48,
66+
},
67+
});
68+
await Vue.nextTick();
69+
expect(wrapper.find('.vti__selection > .vti__flag').classes()).toContain('pl');
70+
});
71+
});
6172
describe(':disabled', () => {
6273
it('adds disabled class to component', () => {
6374
const wrapper = shallowMount(VueTelInput, {

src/components/vue-tel-input.vue

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default {
110110
defaultCountry: {
111111
// Default country code, ie: 'AU'
112112
// Will override the current country of user
113-
type: String,
113+
type: [String, Number],
114114
default: () => getDefault('defaultCountry'),
115115
},
116116
disabled: {
@@ -351,9 +351,19 @@ export default {
351351
* 2. Use default country if passed from parent
352352
*/
353353
if (this.defaultCountry) {
354-
this.choose(this.defaultCountry);
355-
resolve();
356-
return;
354+
if (typeof this.defaultCountry === 'string') {
355+
this.choose(this.defaultCountry);
356+
resolve();
357+
return;
358+
}
359+
if (typeof this.defaultCountry === 'number') {
360+
const country = this.findCountryByDialCode(this.defaultCountry);
361+
if (country) {
362+
this.choose(country.iso2);
363+
resolve();
364+
return;
365+
}
366+
}
357367
}
358368
359369
const fallbackCountry = this.preferredCountries[0] || this.filteredCountries[0];
@@ -395,6 +405,9 @@ export default {
395405
findCountry(iso = '') {
396406
return this.filteredCountries.find((country) => country.iso2 === iso.toUpperCase());
397407
},
408+
findCountryByDialCode(dialCode) {
409+
return this.filteredCountries.find((country) => Number(country.dialCode) === dialCode);
410+
},
398411
getItemClass(index, iso2) {
399412
const highlighted = this.selectedIndex === index;
400413
const lastPreferred = index === this.preferredCountries.length - 1;

0 commit comments

Comments
 (0)