opnform-host-nginx/client/components/forms/PhoneInput.vue

159 lines
5.0 KiB
Vue
Raw Normal View History

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