11<template >
2- <field-wrap :label = " label" :errors = " errors" >
3- <div class = " container-input-tel" @click = " inputTel?.focus()" :class = " {
2+ <field-wrap :errors = " errors" >
3+ <div class = " container-input-tel" @click = " inputTel?.focus?.()"
4+ :class = " {
45 'container-input-tel_disabled': disabled,
56 'container-input-tel_error': errors.length
67 }" >
@@ -22,11 +23,10 @@ import {computed, ref} from "vue";
2223import {parsePhoneNumber , AsYouType } from ' libphonenumber-js'
2324
2425interface Props {
25- label? : string ,
2626 errors: string [],
2727 modelValue: any ,
2828 disabled: boolean ,
29- autofocus: boolean
29+ autofocus? : boolean
3030}
3131
3232const props = defineProps <Props >()
@@ -36,17 +36,18 @@ const emit = defineEmits<{
3636
3737// Ref to Input
3838const inputTel = ref <HTMLInputElement >();
39-
4039/**
4140 * @description Using libphonenumber-js return pretty value of phone number. In Error case return user's target value.
4241 * */
43- const prettyValue = computed (() => {
42+ const prettyValue = computed (() => prettifyPhoneValue (props .modelValue ))
43+ function prettifyPhoneValue(value : unknown ) {
4444 try {
45- return new AsYouType ().input (props .modelValue )
46- } catch (e ) {
45+ if (typeof value !== ' string' ) return ' ' ;
46+ return new AsYouType ().input (value )
47+ } catch (e ) {
4748 return props .modelValue ;
48- }
49- })
49+ }
50+ }
5051const countryCode = computed (() => {
5152 try {
5253 return parsePhoneNumber (props .modelValue ).country ?.toLocaleLowerCase ();
@@ -55,21 +56,21 @@ const countryCode = computed(() => {
5556 }
5657})
5758
59+ function onInput(e : any ) {
60+ const value = parseTelStr (e .target .value );
61+
62+ emit (' update:modelValue' , value );
63+ }
64+
5865/**
5966 * @description Put just numeric and start +
6067 */
6168function parseTelStr(a : string ) {
6269 const numericStr = a .replace (/ \D + / g , ' ' );
63- return ' +' + numericStr ;
64- }
6570
66- function onInput(e : any ) {
67- const value = parseTelStr (e .target .value );
68-
69- emit (' update:modelValue' , value )
71+ return numericStr .length ? ' +' + numericStr : ' ' ;
7072}
7173
72-
7374 </script >
7475
7576<style scoped>
0 commit comments