Skip to content

Commit d4d5977

Browse files
authored
Merge pull request #161 from Jenesius/issue_158
Resolve #158
2 parents dbb8e06 + aafdba4 commit d4d5977

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

project/pages/test/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<div :key = "pureValue">Pure values: {{pureValue}}</div>
77
<div :key = "pureAvailabilities">Pure av: {{pureAvailabilities}}</div>
88
<form-field type = "country" name = "country" label = "Country" />
9+
<form-field type = "tel" name = "phone" label = "Phone" required />
910
<form-field type = "date" name = "birthday" label = "Bithday" />
1011

1112

src/widgets/inputs/input-tel/widget-input-tel.vue

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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";
2223
import {parsePhoneNumber, AsYouType} from 'libphonenumber-js'
2324
2425
interface Props {
25-
label?: string,
2626
errors: string[],
2727
modelValue: any,
2828
disabled: boolean,
29-
autofocus: boolean
29+
autofocus?: boolean
3030
}
3131
3232
const props = defineProps<Props>()
@@ -36,17 +36,18 @@ const emit = defineEmits<{
3636
3737
// Ref to Input
3838
const 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+
}
5051
const 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
*/
6168
function 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

Comments
 (0)