fix: 修复TelInput组件号码无法解析问题

This commit is contained in:
TsMask
2024-11-14 20:08:40 +08:00
parent dab76add73
commit 20c1c455c4
4 changed files with 22 additions and 22 deletions

View File

@@ -1,9 +1,9 @@
<!-- https://github.com/jackocnr/intl-tel-input/blob/master/react/src/intl-tel-input/react.tsx --> <!-- https://github.com/jackocnr/intl-tel-input/blob/master/react/src/intl-tel-input/react.tsx -->
<script lang="ts" setup> <script lang="ts" setup>
import intlTelInput, { Iti, SomeOptions } from 'intl-tel-input'; import { Iti } from 'intl-tel-input';
import intlTelInput from 'intl-tel-input/intlTelInputWithUtils';
import 'intl-tel-input/build/css/intlTelInput.min.css'; import 'intl-tel-input/build/css/intlTelInput.min.css';
import 'intl-tel-input/build/js/utils.js'; import { ref, onMounted, onBeforeUnmount, nextTick } from 'vue';
import { ref, onMounted, onBeforeUnmount, nextTick, watch } from 'vue';
import useI18n from '@/hooks/useI18n'; import useI18n from '@/hooks/useI18n';
const { currentLocale } = useI18n(); const { currentLocale } = useI18n();
const emit = defineEmits(['update:value', 'update:change']); const emit = defineEmits(['update:value', 'update:change']);
@@ -45,13 +45,13 @@ const itiRef = ref<Iti | null>(null);
function fnChange() { function fnChange() {
if (!itiRef.value) return; if (!itiRef.value) return;
const num = itiRef.value?.getNumber() || ''; const number = itiRef.value?.getNumber() || '';
const countryIso = itiRef.value?.getSelectedCountryData().iso2 || ''; const countryIso = itiRef.value?.getSelectedCountryData().iso2 || '';
// note: this number will be in standard E164 format, but any container component can use // note: this number will be in standard E164 format, but any container component can use
// intlTelInputUtils.formatNumber() to convert this to another format // intlTelInputUtils.formatNumber() to convert this to another format
// as well as intlTelInputUtils.getNumberType() etc. if need be // as well as intlTelInputUtils.getNumberType() etc. if need be
let data = { let data = {
num, number,
countryIso, countryIso,
validity: false, validity: false,
errorCode: -1, errorCode: -1,
@@ -69,21 +69,11 @@ function fnChange() {
data.errorCode = errorCode; data.errorCode = errorCode;
} }
// console.log(data); // console.log(data);
emit('update:value', num);
emit('update:value', number);
emit('update:change', data); emit('update:change', data);
} }
watch(
() => props.value,
v => {
if (v) {
itiRef.value?.setNumber(v);
} else {
itiRef.value?.setNumber('');
}
}
);
onMounted(() => { onMounted(() => {
nextTick(async () => { nextTick(async () => {
if (inputRef.value) { if (inputRef.value) {
@@ -106,7 +96,13 @@ onMounted(() => {
formatOnDisplay: true, formatOnDisplay: true,
autoPlaceholder: 'polite', autoPlaceholder: 'polite',
i18n: i18n, i18n: i18n,
} as SomeOptions); });
if (props.value) {
itiRef.value.setNumber(props.value);
}
if (props.disabled) {
itiRef.value.setDisabled(props.disabled);
}
inputRef.value.addEventListener('countrychange', fnChange); inputRef.value.addEventListener('countrychange', fnChange);
} }
}); });
@@ -125,7 +121,6 @@ onBeforeUnmount(() => {
type="tel" type="tel"
class="ant-input" class="ant-input"
ref="inputRef" ref="inputRef"
:value="value"
:disabled="disabled" :disabled="disabled"
:placeholder="placeholder" :placeholder="placeholder"
:maxlength="maxlength" :maxlength="maxlength"

View File

@@ -8,3 +8,8 @@ declare module '*.vue' {
} }
declare module 'vue3-smooth-dnd'; declare module 'vue3-smooth-dnd';
declare module 'intl-tel-input/intlTelInputWithUtils' {
import intlTelInput from 'intl-tel-input';
export default intlTelInput;
}

View File

@@ -169,7 +169,7 @@ onMounted(() => {
<IntlTelInput <IntlTelInput
v-model:value="stateForm.form.phonenumber" v-model:value="stateForm.form.phonenumber"
allow-clear allow-clear
:maxlength="16" :maxlength="20"
:placeholder="t('views.account.settings.phonenumberPleace')" :placeholder="t('views.account.settings.phonenumberPleace')"
></IntlTelInput> ></IntlTelInput>
</a-form-item> </a-form-item>

View File

@@ -1367,7 +1367,7 @@ onMounted(() => {
<IntlTelInput <IntlTelInput
v-model:value="modalState.from.phonenumber" v-model:value="modalState.from.phonenumber"
allow-clear allow-clear
:maxlength="16" :maxlength="20"
:placeholder="t('common.inputPlease')" :placeholder="t('common.inputPlease')"
></IntlTelInput> ></IntlTelInput>
</a-form-item> </a-form-item>