hesabixCore/webUI/src/components/forms/Hnumberinput.vue

83 lines
1.7 KiB
Vue
Raw Normal View History

2025-03-30 00:06:17 +03:30
<template>
2025-04-04 23:37:58 +03:30
<v-text-field
v-model="inputValue"
v-bind="$attrs"
type="text"
:rules="combinedRules"
@keypress="restrictToNumbers"
dir="ltr"
dense
/>
</template>
<script>
export default {
name: 'HNumberInput',
inheritAttrs: false,
props: {
modelValue: {
type: [Number, String],
default: null
2025-03-30 00:06:17 +03:30
},
2025-04-04 23:37:58 +03:30
rules: {
type: Array,
default: () => []
}
},
data() {
return {
inputValue: ''
}
},
computed: {
combinedRules() {
return [
v => !v || /^\d+$/.test(v.replace(/[^0-9]/g, '')) || 'فقط عدد انگلیسی مجاز است',
...this.rules
]
}
},
watch: {
modelValue: {
immediate: true,
handler(newVal) {
if (newVal !== null && newVal !== undefined) {
const cleaned = String(newVal).replace(/[^0-9]/g, '')
this.inputValue = cleaned ? Number(cleaned).toLocaleString('en-US') : ''
} else {
this.inputValue = ''
}
2025-03-30 00:06:17 +03:30
}
},
2025-04-04 23:37:58 +03:30
inputValue(newVal) {
if (newVal === '' || newVal === null || newVal === undefined) {
this.$emit('update:modelValue', 0) // وقتی خالی است، صفر ارسال شود
} else {
const cleaned = String(newVal).replace(/[^0-9]/g, '')
const numericValue = cleaned ? Number(cleaned) : 0
this.$emit('update:modelValue', numericValue)
2025-03-30 00:06:17 +03:30
}
2025-04-04 23:37:58 +03:30
}
},
methods: {
restrictToNumbers(event) {
const charCode = event.charCode
if (charCode < 48 || charCode > 57) {
event.preventDefault()
2025-03-30 00:06:17 +03:30
}
}
}
2025-04-04 23:37:58 +03:30
}
</script>
<style scoped>
:deep(.v-text-field input) {
direction: ltr;
text-align: left;
}
</style>