2023-09-12 08:13:10 +00:00
|
|
|
<template>
|
2023-11-17 11:48:43 +00:00
|
|
|
<input-wrapper
|
|
|
|
v-bind="$props"
|
|
|
|
>
|
|
|
|
<template #label>
|
|
|
|
<slot name="label" />
|
|
|
|
</template>
|
|
|
|
|
2023-09-18 13:12:05 +00:00
|
|
|
<div :id="id ? id : name" :name="name" :style="inputStyle" class="flex items-center">
|
2023-10-12 11:30:09 +00:00
|
|
|
<v-select v-model="selectedCountryCode" class="w-[130px]" dropdown-class="w-[300px]" input-class="rounded-r-none"
|
|
|
|
:data="countries"
|
|
|
|
:disabled="disabled || countries.length===1" :searchable="true" :search-keys="['name']" :option-key="'code'" :color="color"
|
2023-09-18 13:12:05 +00:00
|
|
|
:has-error="hasValidation && form.errors.has(name)"
|
2023-10-12 11:30:09 +00:00
|
|
|
:placeholder="'Select a country'" :uppercase-labels="true" :theme="theme" @input="onChangeCountryCode"
|
|
|
|
>
|
2023-09-12 08:13:10 +00:00
|
|
|
<template #option="props">
|
2023-09-12 09:25:34 +00:00
|
|
|
<div class="flex items-center space-x-2 hover:text-white">
|
2023-10-12 11:30:09 +00:00
|
|
|
<country-flag size="normal" class="!-mt-[9px]" :country="props.option.code" />
|
2023-09-12 09:25:34 +00:00
|
|
|
<span class="grow">{{ props.option.name }}</span>
|
2023-09-12 08:13:10 +00:00
|
|
|
<span>{{ props.option.dial_code }}</span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template #selected="props">
|
2023-09-12 09:25:34 +00:00
|
|
|
<div class="flex items-center space-x-2 justify-center overflow-hidden">
|
2023-10-12 11:30:09 +00:00
|
|
|
<country-flag size="normal" class="!-mt-[9px]" :country="props.option.code" />
|
2023-09-12 08:13:10 +00:00
|
|
|
<span>{{ props.option.dial_code }}</span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</v-select>
|
2023-09-18 13:12:05 +00:00
|
|
|
<input v-model="inputVal" type="text" class="inline-flex-grow !border-l-0 !rounded-l-none" :disabled="disabled"
|
2023-11-17 11:48:43 +00:00
|
|
|
:class="[theme.default.input, { '!ring-red-500 !ring-2': hasValidation && form.errors.has(name), '!cursor-not-allowed !bg-gray-200': disabled }]"
|
|
|
|
:placeholder="placeholder" :style="inputStyle" @input="onInput"
|
2023-10-12 11:30:09 +00:00
|
|
|
>
|
2023-09-12 08:13:10 +00:00
|
|
|
</div>
|
2023-11-17 11:48:43 +00:00
|
|
|
|
|
|
|
<template #help>
|
|
|
|
<slot name="help" />
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<template #error>
|
|
|
|
<slot name="error" />
|
|
|
|
</template>
|
|
|
|
|
|
|
|
</input-wrapper>
|
2023-09-12 08:13:10 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-11-17 11:48:43 +00:00
|
|
|
import { inputProps, useFormInput } from './useFormInput.js'
|
|
|
|
import InputWrapper from './components/InputWrapper.vue'
|
2023-09-12 08:13:10 +00:00
|
|
|
import countryCodes from '../../../data/country_codes.json'
|
2023-10-14 15:31:30 +00:00
|
|
|
import CountryFlag from 'vue-country-flag-next'
|
2023-09-18 13:12:05 +00:00
|
|
|
import parsePhoneNumber from 'libphonenumber-js'
|
2023-09-12 08:13:10 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
phone: 'PhoneInput',
|
2023-11-17 11:48:43 +00:00
|
|
|
components: { InputWrapper, CountryFlag },
|
2023-09-18 13:12:05 +00:00
|
|
|
props: {
|
2023-11-17 11:48:43 +00:00
|
|
|
...inputProps,
|
2023-10-12 11:30:09 +00:00
|
|
|
canOnlyCountry: { type: Boolean, default: false },
|
2023-10-14 15:31:30 +00:00
|
|
|
unavailableCountries: { type: Array, default: () => [] }
|
2023-09-18 13:12:05 +00:00
|
|
|
},
|
2023-09-12 08:13:10 +00:00
|
|
|
|
2023-11-17 11:48:43 +00:00
|
|
|
setup (props, context) {
|
|
|
|
const {
|
|
|
|
compVal,
|
|
|
|
inputStyle,
|
|
|
|
hasValidation,
|
|
|
|
hasError
|
|
|
|
} = useFormInput(props, context)
|
|
|
|
|
|
|
|
return {
|
|
|
|
compVal,
|
|
|
|
inputStyle,
|
|
|
|
hasValidation,
|
|
|
|
hasError
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-10-03 15:50:46 +00:00
|
|
|
data () {
|
2023-09-12 08:13:10 +00:00
|
|
|
return {
|
2023-10-03 15:50:46 +00:00
|
|
|
selectedCountryCode: null,
|
2023-09-18 13:12:05 +00:00
|
|
|
inputVal: null
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
},
|
2023-09-18 13:12:05 +00:00
|
|
|
|
2023-10-14 15:31:30 +00:00
|
|
|
computed: {
|
|
|
|
countries () {
|
|
|
|
return countryCodes.filter((item) => {
|
|
|
|
return !this.unavailableCountries.includes(item.code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-12 08:13:10 +00:00
|
|
|
watch: {
|
2023-09-18 13:12:05 +00:00
|
|
|
inputVal: {
|
2023-10-03 15:50:46 +00:00
|
|
|
handler (val) {
|
2023-09-18 13:12:05 +00:00
|
|
|
if (val && val.startsWith('0')) {
|
|
|
|
val = val.substring(1)
|
|
|
|
}
|
2023-10-03 15:50:46 +00:00
|
|
|
if (this.canOnlyCountry) {
|
2023-10-12 11:30:09 +00:00
|
|
|
this.compVal = (val) ? this.selectedCountryCode.code + this.selectedCountryCode.dial_code + val : this.selectedCountryCode.code + this.selectedCountryCode.dial_code
|
2023-10-03 15:50:46 +00:00
|
|
|
} else {
|
|
|
|
this.compVal = (val) ? this.selectedCountryCode.code + this.selectedCountryCode.dial_code + val : null
|
|
|
|
}
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
},
|
2023-10-03 15:50:46 +00:00
|
|
|
selectedCountryCode (newVal, oldVal) {
|
|
|
|
if (this.compVal && newVal && oldVal) {
|
|
|
|
this.compVal = this.compVal.replace(oldVal.code + oldVal.dial_code, newVal.code + newVal.dial_code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
mounted () {
|
|
|
|
if (this.compVal) {
|
2023-10-12 11:30:09 +00:00
|
|
|
if (!this.compVal.startsWith('+')) {
|
2023-10-03 15:50:46 +00:00
|
|
|
this.selectedCountryCode = this.getCountryBy(this.compVal.substring(2, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
const phoneObj = parsePhoneNumber(this.compVal)
|
|
|
|
if (phoneObj !== undefined && phoneObj) {
|
|
|
|
if (!this.selectedCountryCode && phoneObj.country !== undefined && phoneObj.country) {
|
|
|
|
this.selectedCountryCode = this.getCountryBy(phoneObj.country)
|
|
|
|
}
|
|
|
|
this.inputVal = phoneObj.nationalNumber
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 11:30:09 +00:00
|
|
|
if (!this.selectedCountryCode) {
|
2023-10-03 15:50:46 +00:00
|
|
|
this.selectedCountryCode = this.getCountryBy()
|
|
|
|
}
|
2023-10-12 11:30:09 +00:00
|
|
|
if (!this.selectedCountryCode || this.countries.length === 1) {
|
|
|
|
this.selectedCountryCode = this.countries[0]
|
|
|
|
}
|
2023-09-12 08:13:10 +00:00
|
|
|
},
|
2023-10-12 11:30:09 +00:00
|
|
|
|
2023-09-12 08:13:10 +00:00
|
|
|
methods: {
|
2023-10-03 15:50:46 +00:00
|
|
|
getCountryBy (code = 'US', type = 'code') {
|
2023-10-12 11:30:09 +00:00
|
|
|
if (!code) code = 'US' // Default US
|
|
|
|
return this.countries.find((item) => {
|
2023-09-18 13:12:05 +00:00
|
|
|
return item[type] === code
|
2023-10-12 11:30:09 +00:00
|
|
|
}) ?? null
|
2023-09-12 08:13:10 +00:00
|
|
|
},
|
2023-10-03 15:50:46 +00:00
|
|
|
onInput (event) {
|
2023-09-18 13:12:05 +00:00
|
|
|
this.inputVal = event.target.value.replace(/[^0-9]/g, '')
|
2023-09-12 08:13:10 +00:00
|
|
|
},
|
2023-10-03 15:50:46 +00:00
|
|
|
onChangeCountryCode () {
|
2023-10-12 11:30:09 +00:00
|
|
|
if (!this.selectedCountryCode && this.countries.length > 0) {
|
|
|
|
this.selectedCountryCode = this.countries[0]
|
|
|
|
}
|
2023-09-18 13:12:05 +00:00
|
|
|
if (this.canOnlyCountry && (this.inputVal === null || this.inputVal === '' || !this.inputVal)) {
|
2023-10-03 15:50:46 +00:00
|
|
|
this.compVal = this.selectedCountryCode.code + this.selectedCountryCode.dial_code
|
2023-09-18 13:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|