This commit is contained in:
2026-06-30 22:21:56 +03:30
parent 8b855e5ee4
commit 4fa2c0d57d
6 changed files with 80 additions and 29 deletions
+30 -21
View File
@@ -36,6 +36,7 @@
import {usePrefix} from "@/utils/usePrefix";
import {computed} from "vue";
import {usePrice} from "@/components/BasicPacks/BasicPriceInput/src/utils/usePrice";
import {persianToEnglishDigits, shouldNormalizeInputDigits} from '@/utils/persianDigits';
//Props
interface Prop {
@@ -56,29 +57,42 @@ interface Prop {
const addonBgColor = computed(() => props.addonBgColor)
const addonTextColor = computed(() => props.addonTextColor)
//emits
const localValue = computed({
set(value) {
// console.log(value?.toString().replaceAll(',', ''))
props.type === 'price' ? emits('update:value', value?.toString().replaceAll(',', '')) : emits('update:value', value)
},
get() {
let a = props.type === 'price' ? props.value ? usePrice(props.value as number, true, '').trim() : '' : props.value
// console.log(props.value)
return a
},
})
const props = withDefaults(defineProps<Prop>(), {
isPassword: false
})
const emits = defineEmits(['update:value', 'onchange'])
const localValue = computed({
set(value) {
emits('update:value', normalizeValue(value))
},
get() {
return props.type === 'price'
? props.value ? usePrice(props.value as number, true, '').trim() : ''
: props.value
},
})
function normalizeValue(value: unknown) {
if (value == null || value === '') {
return value
}
let normalized = String(value)
if (shouldNormalizeInputDigits(props)) {
normalized = persianToEnglishDigits(normalized)
}
if (props.type === 'price') {
normalized = normalized.replaceAll(',', '')
}
return normalized
}
const handlerChange = (e: any) => {
// emits('onchange', e.target.value)
props.type === 'price' ? emits('update:value', e.target.value?.toString().replaceAll(',', '')) : emits('update:value', e.target.value)
props.type === 'price' ? emits('onchange', e.target.value?.toString().replaceAll(',', '')) : emits('onchange', e.target.value)
// console.log('sas')
const normalized = normalizeValue(e.target.value)
emits('update:value', normalized)
emits('onchange', normalized)
}
//css style
@@ -89,11 +103,6 @@ const {prefixCls} = usePrefix('input')
@prefix: ~'@{prefixCls}-input';
.@{prefix} {
/* .ant-input-group-addon:first-child {
!* border: #d9d9d9 1px solid;
border-radius: 0 6px 6px 0;*!
}*/
.ant-input-group {
direction: ltr;
}
@@ -1,5 +1,5 @@
<template>
<BasicInput v-model:value="_val">
<BasicInput v-model:value="_val" type="price">
<template v-for="(_, slotKey) in $slots" #[slotKey]="data">
<slot :name="slotKey" v-bind="data || {}"></slot>
</template>
@@ -10,6 +10,7 @@
import {usePrice} from './utils/usePrice'
import {computed} from "vue";
import BasicInput from "../../BasicInput.vue";
import {persianToEnglishDigits} from '@/utils/persianDigits'
interface Props {
value?: string | number
@@ -23,9 +24,7 @@ const props = withDefaults(defineProps<Props>(), {})
const _val = computed({
set(val: string) {
// const temp = val.toString().replaceAll(',', '')
const temp = val.toString().replaceAll(',', '')
emit('update:value', temp)
emit('update:value', persianToEnglishDigits(val).replaceAll(',', ''))
},
get() {
return props.value ? usePrice(props.value as number, true, '').trim() : ''
+16 -2
View File
@@ -32,9 +32,23 @@ import { computed, ref } from 'vue'
import type { Rule } from 'ant-design-vue/es/form'
import { useWalletStore } from '@/store/walletStore'
import {isToken} from "@/utils/index";
import { persianToEnglishDigits } from '@/utils/persianDigits'
export const mobileValidation = (title?: string) => {
return { len: 11, message: `شماره همراه باید 11 رقم باشد` }
export const mobileValidation = (title?: string): Rule => {
const label = title ?? 'شماره تلفن'
return {
validator: (_rule, value) => {
const normalized = persianToEnglishDigits(String(value ?? '').trim())
if (!normalized) {
return Promise.resolve()
}
if (!/^09\d{9}$/.test(normalized)) {
return Promise.reject(`${label} باید با 09 شروع شود و 11 رقم باشد`)
}
return Promise.resolve()
},
trigger: ['change', 'blur'],
}
}
export const required = (title?: string) => {
+29
View File
@@ -0,0 +1,29 @@
const PERSIAN_DIGITS = '۰۱۲۳۴۵۶۷۸۹'
const ARABIC_DIGITS = '٠١٢٣٤٥٦٧٨٩'
/** تبدیل ارقام فارسی/عربی و جداکننده اعشار به فرمت انگلیسی */
export function persianToEnglishDigits(value: string | number | null | undefined): string {
if (value == null) {
return ''
}
return String(value)
.replace(/[۰-۹]/g, (d) => String(PERSIAN_DIGITS.indexOf(d)))
.replace(/[٠-٩]/g, (d) => String(ARABIC_DIGITS.indexOf(d)))
.replace(/٫/g, '.')
}
export function shouldNormalizeInputDigits(options: {
isPassword?: boolean
type?: string
inputmode?: string
}): boolean {
if (options.isPassword) {
return false
}
if (options.type === 'price' || options.type === 'number') {
return true
}
const mode = String(options.inputmode ?? '').trim().toLowerCase()
return mode === 'numeric' || mode === 'decimal' || mode === 'number'
}
+1 -1
View File
@@ -25,7 +25,7 @@
<a-form-item v-if="!stepTwoVerify" :rules="[required('شماره تلفن'),mobileValidation()]"
class="home_appear_fade_in_down" label="شماره تلفن"
name="mobile">
<BasicInput v-model:value="userFormState.mobile" place-holder="شماره تلفن" size="large" inputmode="decimal"/>
<BasicInput v-model:value="userFormState.mobile" place-holder="شماره تلفن" size="large" inputmode="numeric"/>
</a-form-item>
<!-- </transition>-->
<!-- <transition name="fade" mode="out-in">-->
+1 -1
View File
@@ -12,7 +12,7 @@
</a-form-item>
<a-form-item :rules="[required(),mobileValidation()]" class="home_appear_fade_in_down" label="شماره تلفن"
name="mobile">
<BasicInput v-model:value="userFormState.mobile" place-holder="شماره تلفن" inputmode="decimal"/>
<BasicInput v-model:value="userFormState.mobile" place-holder="شماره تلفن" inputmode="numeric"/>
</a-form-item>
<a-form-item :rules="[required()]" class="home_appear_fade_in_down" label="کلمه عبور" name="password">
<!-- <BasicInput v-model:value="userFormState.password" size="large" place-holder="کلمه عبور"/>-->