Skip to content

Commit

Permalink
fix: country selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ibkhall committed Jan 28, 2023
1 parent 0b1569c commit 2a1c619
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ibkhall/v-phone-field",
"description": "Vuetify phone field",
"version": "1.1.0",
"version": "1.1.1",
"type": "module",
"author": "Ibrahim Boubacar",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ref } from 'vue';
import VPhoneField from './components/VPhoneField.vue';
const phone = ref('')
const phone = ref('+22792714229')
</script>

Expand Down
21 changes: 12 additions & 9 deletions src/components/VPhoneField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const props = defineProps({
},
defaultCountry: {
type: String,
default: 'af'
default: undefined
},
name: {
type: String,
Expand All @@ -56,13 +56,13 @@ const value = ref(props.modelValue)
let errors: string[] = reactive([])
const display_text = ref('')
const country = ref({iso2: '', dialCode: ''} as any)
if(props.defaultCountry) {
country.value = codes.find(el => el.iso2==props.defaultCountry)
}
const iso2 = () => {
if(navigator.onLine) {
return fetch('https://ip2c.org/s')
const fetchCountry = () => {
if(props.defaultCountry) {
country.value = codes.find(el => el.iso2==props.defaultCountry?.toLocaleLowerCase())
}else {
fetch('https://ip2c.org/s')
.then((response) => response.text())
.then((response) => {
const result = (response || '').toString();
Expand All @@ -72,7 +72,7 @@ const iso2 = () => {
}
country.value = codes.find(el => el.iso2==result.substr(2, 2).toLowerCase()) as any
});
}).catch(e => country.value = codes.find(el => el.iso2==codes[0].iso2));
}
}
Expand All @@ -87,10 +87,13 @@ const onUpdate = (v: any) => {
onMounted(() => {
iso2()
if(value.value) {
const pn = parsePhoneNumber(value.value as string)
display_text.value = pn.number?.national as string
country.value = codes.find(el => el.iso2==pn.regionCode?.toLocaleLowerCase())
}else {
fetchCountry()
}
})
Expand Down

0 comments on commit 2a1c619

Please sign in to comment.