fix price input

This commit is contained in:
2026-06-22 12:08:47 +03:30
parent c3abeeecef
commit cc5e3ce5f2
2 changed files with 49 additions and 16 deletions
@@ -32,13 +32,24 @@
</a-select>
<a-input-number
v-else-if="fieldType === 'number'"
:value="numberValue"
v-else-if="fieldType === 'price'"
:value="priceValue"
:disabled="disabled"
class="w-full"
:formatter="(v: string) => `${v}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
:parser="(v: string) => v.replace(/,/g, '')"
@update:value="onNumberChange"
@update:value="onPriceChange"
/>
<a-input
v-else-if="fieldType === 'number'"
:value="modelValue"
:disabled="disabled"
inputmode="numeric"
allow-clear
class="w-full"
@update:value="onPlainNumberChange"
@blur="emit('blur')"
/>
<a-textarea
@@ -119,18 +130,36 @@ function onSelectChange(val: string | undefined) {
emit('update:value', val ?? '')
}
const numberValue = computed(() => {
const priceValue = computed(() => {
const n = parseFloat(String(props.modelValue || '').replace(/,/g, ''))
return Number.isFinite(n) ? n : undefined
})
function onNumberChange(val: number | string | null) {
function onPriceChange(val: number | string | null) {
if (val === null || val === undefined || val === '') {
emit('update:value', '')
return
}
emit('update:value', String(val))
}
function normalizePlainNumberInput(value: string): string {
const latin = value.replace(/[۰-۹]/g, (d) => String('۰۱۲۳۴۵۶۷۸۹'.indexOf(d)))
let out = ''
let hasDot = false
for (const ch of latin.replace(/,/g, '')) {
if (ch >= '0' && ch <= '9') out += ch
else if (ch === '.' && !hasDot) {
hasDot = true
out += ch
}
}
return out
}
function onPlainNumberChange(val: string) {
emit('update:value', normalizePlainNumberInput(val ?? ''))
}
</script>
<style scoped>