2023-09-12 08:13:10 +00:00
|
|
|
<template>
|
|
|
|
<div :class="wrapperClass" :style="inputStyle">
|
|
|
|
<slot name="label">
|
|
|
|
<label v-if="label" :for="id ? id : name"
|
2023-09-12 09:25:34 +00:00
|
|
|
:class="[theme.default.label, { 'uppercase text-xs': uppercaseLabels, 'text-sm': !uppercaseLabels }]">
|
2023-09-12 08:13:10 +00:00
|
|
|
{{ label }}
|
|
|
|
<span v-if="required" class="text-red-500 required-dot">*</span>
|
|
|
|
</label>
|
|
|
|
</slot>
|
|
|
|
<div v-if="help && helpPosition == 'above_input'" class="flex mb-1">
|
|
|
|
<small :class="theme.default.help" class="grow">
|
2023-09-12 09:25:34 +00:00
|
|
|
<slot name="help"><span class="field-help" v-html="help"/></slot>
|
2023-09-12 08:13:10 +00:00
|
|
|
</small>
|
|
|
|
</div>
|
2023-09-18 13:12:05 +00:00
|
|
|
<div :id="id ? id : name" :name="name" :style="inputStyle" class="flex items-center">
|
2023-09-20 10:14:08 +00:00
|
|
|
<v-select class="w-[110px]" dropdown-class="w-[350px]" input-class="rounded-r-none" :data="countries"
|
2023-09-18 13:12:05 +00:00
|
|
|
v-model="selectedCountryCode"
|
|
|
|
:has-error="hasValidation && form.errors.has(name)"
|
|
|
|
:disabled="disabled" :searchable="true" :search-keys="['name']" :option-key="'code'" :color="color"
|
|
|
|
: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">
|
|
|
|
<country-flag size="normal" class="!-mt-[9px]" :country="props.option.code"/>
|
|
|
|
<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-09-18 13:12:05 +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-09-12 09:25:34 +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-09-12 08:13:10 +00:00
|
|
|
</div>
|
2023-09-18 13:12:05 +00:00
|
|
|
<div v-if="help && helpPosition=='below_input'" class="flex">
|
|
|
|
<small :class="theme.default.help" class="grow">
|
|
|
|
<slot name="help"><span class="field-help" v-html="help"/></slot>
|
|
|
|
</small>
|
|
|
|
</div>
|
|
|
|
<has-error v-if="hasValidation" :form="form" :field="name"/>
|
2023-09-12 08:13:10 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-09-12 09:25:34 +00:00
|
|
|
import {directive as onClickaway} from 'vue-clickaway'
|
2023-09-12 08:13:10 +00:00
|
|
|
import inputMixin from '~/mixins/forms/input.js'
|
|
|
|
import countryCodes from '../../../data/country_codes.json'
|
|
|
|
import CountryFlag from 'vue-country-flag'
|
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-09-18 13:12:05 +00:00
|
|
|
components: {CountryFlag},
|
2023-09-12 08:13:10 +00:00
|
|
|
directives: {
|
|
|
|
onClickaway: onClickaway
|
|
|
|
},
|
|
|
|
mixins: [inputMixin],
|
2023-09-18 13:12:05 +00:00
|
|
|
props: {
|
|
|
|
canOnlyCountry: {type: Boolean, default: false}
|
|
|
|
},
|
2023-09-12 08:13:10 +00:00
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
2023-09-18 13:12:05 +00:00
|
|
|
selectedCountryCode: this.getCountryBy('US'), // Default US
|
2023-09-12 08:13:10 +00:00
|
|
|
countries: countryCodes,
|
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
|
|
|
|
|
|
|
mounted() {
|
|
|
|
if (this.compVal) {
|
|
|
|
const phoneObj = parsePhoneNumber(this.compVal)
|
|
|
|
if (phoneObj !== undefined && phoneObj) {
|
|
|
|
if (phoneObj.country !== undefined && phoneObj.country) {
|
|
|
|
this.selectedCountryCode = this.getCountryBy(phoneObj.country)
|
|
|
|
}
|
|
|
|
this.inputVal = phoneObj.nationalNumber
|
|
|
|
} else if (this.compVal) {
|
|
|
|
this.selectedCountryCode = this.getCountryBy(this.compVal, 'dial_code')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-12 08:13:10 +00:00
|
|
|
watch: {
|
2023-09-18 13:12:05 +00:00
|
|
|
inputVal: {
|
|
|
|
handler(val) {
|
|
|
|
if (val && val.startsWith('0')) {
|
|
|
|
val = val.substring(1)
|
|
|
|
}
|
|
|
|
this.compVal = (val) ? this.selectedCountryCode.dial_code + val : null
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
selectedCountryCode(newVal, oldVal) {
|
|
|
|
if (this.compVal) {
|
|
|
|
this.compVal = this.compVal.replace(oldVal.dial_code, newVal.dial_code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2023-09-18 13:12:05 +00:00
|
|
|
getCountryBy(code, type = 'code') {
|
|
|
|
return countryCodes.find((item) => {
|
|
|
|
return item[type] === code
|
|
|
|
})
|
2023-09-12 08:13:10 +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-09-18 13:12:05 +00:00
|
|
|
onChangeCountryCode() {
|
|
|
|
if (this.canOnlyCountry && (this.inputVal === null || this.inputVal === '' || !this.inputVal)) {
|
|
|
|
this.compVal = this.selectedCountryCode.dial_code
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 08:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|