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

This commit is contained in:
TsMask
2024-11-15 15:20:09 +08:00
parent 44cd1d354a
commit 802a91a96d
4 changed files with 50 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);
} }
}); });
@@ -124,8 +120,7 @@ onBeforeUnmount(() => {
<input <input
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"
@@ -142,4 +137,32 @@ onBeforeUnmount(() => {
.iti .iti__country-container .iti__search-input { .iti .iti__country-container .iti__search-input {
padding: 4px 8px; padding: 4px 8px;
} }
.iti .ant-input {
box-sizing: border-box;
margin: 0;
font-variant: tabular-nums;
list-style: none;
font-feature-settings: 'tnum';
position: relative;
display: inline-block;
width: 100%;
min-width: 0;
padding: 4px 11px;
color: #000000d9;
font-size: 14px;
line-height: 1.5715;
background-color: transparent;
background-image: none;
border: 1px solid #424242;
border-radius: 6px;
transition: all 0.3s;
}
.iti .ant-input:focus,
.iti .ant-input-focused {
border-color: var(--ant-primary-color-hover);
box-shadow: 0 0 0 2px var(--ant-primary-color-outline);
border-right-width: 1px !important;
outline: 0;
}
</style> </style>

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

@@ -1423,7 +1423,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>