Skip to content

Allow defaultCountry prop to accept both iso2 and dialCode #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/documentation/v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Read more on `vue-form-generator`'s [instruction page](https://icebob.gitbooks.i
| -------- | ---- | ------------- | ----------- |
| `autocomplete` | `String` | `'on'` | Native input 'autocomplete' attribute |
| `autofocus` | `Boolean` | `false` | Native input 'autofocus' attribute |
| `defaultCountry` | `String` | `''` | Default country, will override the country fetched from IP address of user |
| `defaultCountry` | `String` | `Number` | `''` | Default country (by iso2 or dialCode), will override the country fetched from IP address of user |
| `disabled` | `Boolean` | `false` | Disable input field |
| `disabledFetchingCountry` | `Boolean` | `false` | Disable fetching current country based on IP address of user |
| `dropdownOptions` | `Object` | `{ disabledDialCode: false, tabindex: 0 }` | Options for dropdown, supporting `disabledDialCode` and `tabindex`|
Expand Down
11 changes: 11 additions & 0 deletions src/components/vue-tel-input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ describe('Props', () => {
expect(wrapper.find('.vti__selection > .vti__flag').classes()).toContain('au');
});
});
describe(':defaultCountryByDialCode', () => {
it('shows correct default country by dial code', async () => {
const wrapper = shallowMount(VueTelInput, {
propsData: {
defaultCountry: 48,
},
});
await Vue.nextTick();
expect(wrapper.find('.vti__selection > .vti__flag').classes()).toContain('pl');
});
});
describe(':disabled', () => {
it('adds disabled class to component', () => {
const wrapper = shallowMount(VueTelInput, {
Expand Down
21 changes: 17 additions & 4 deletions src/components/vue-tel-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default {
defaultCountry: {
// Default country code, ie: 'AU'
// Will override the current country of user
type: String,
type: [String, Number],
default: () => getDefault('defaultCountry'),
},
disabled: {
Expand Down Expand Up @@ -350,9 +350,19 @@ export default {
* 2. Use default country if passed from parent
*/
if (this.defaultCountry) {
this.choose(this.defaultCountry);
resolve();
return;
if (typeof this.defaultCountry === 'string') {
this.choose(this.defaultCountry);
resolve();
return;
}
if (typeof this.defaultCountry === 'number') {
const country = this.findCountryByDialCode(this.defaultCountry);
if (country) {
this.choose(country.iso2);
resolve();
return;
}
}
}

const fallbackCountry = this.preferredCountries[0] || this.filteredCountries[0];
Expand Down Expand Up @@ -394,6 +404,9 @@ export default {
findCountry(iso = '') {
return this.filteredCountries.find((country) => country.iso2 === iso.toUpperCase());
},
findCountryByDialCode(dialCode) {
return this.filteredCountries.find((country) => Number(country.dialCode) === dialCode);
},
getItemClass(index, iso2) {
const highlighted = this.selectedIndex === index;
const lastPreferred = index === this.preferredCountries.length - 1;
Expand Down