2025-06-10 15:10:06 +08:00
|
|
|
|
<template>
|
2025-06-13 17:58:39 +08:00
|
|
|
|
<input
|
|
|
|
|
|
type="number"
|
2025-06-10 15:10:06 +08:00
|
|
|
|
:value="displayValue"
|
|
|
|
|
|
@input="handleInput"
|
|
|
|
|
|
@blur="handleBlur"
|
|
|
|
|
|
:class="inputClass"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: "NumberInput",
|
|
|
|
|
|
props: {
|
2025-06-13 17:58:39 +08:00
|
|
|
|
value: [Number, String],
|
|
|
|
|
|
inputClass: {
|
|
|
|
|
|
type: String,
|
2025-06-10 15:10:06 +08:00
|
|
|
|
default: ''
|
|
|
|
|
|
},
|
2025-06-13 17:58:39 +08:00
|
|
|
|
// 输入模式:integer(纯整数), decimal(纯小数), mixed(混合)
|
|
|
|
|
|
mode: {
|
|
|
|
|
|
type: String,
|
|
|
|
|
|
default: "mixed",
|
|
|
|
|
|
validator: (v) => ["integer", "decimal", "mixed"].includes(v),
|
|
|
|
|
|
},
|
|
|
|
|
|
// 小数位数限制
|
|
|
|
|
|
decimalLength: {
|
|
|
|
|
|
type: Number,
|
|
|
|
|
|
default: 2,
|
|
|
|
|
|
},
|
|
|
|
|
|
// 最小值
|
2025-06-10 15:10:06 +08:00
|
|
|
|
min: {
|
|
|
|
|
|
type: Number,
|
2025-06-13 17:58:39 +08:00
|
|
|
|
default: 0,
|
2025-06-10 15:10:06 +08:00
|
|
|
|
},
|
2025-06-13 17:58:39 +08:00
|
|
|
|
// 最大值
|
2025-06-10 15:10:06 +08:00
|
|
|
|
max: {
|
|
|
|
|
|
type: Number,
|
2025-06-13 17:58:39 +08:00
|
|
|
|
default: Infinity,
|
2025-06-10 15:10:06 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
2025-06-13 17:58:39 +08:00
|
|
|
|
displayValue: this.value?.toString() || "",
|
2025-06-10 15:10:06 +08:00
|
|
|
|
};
|
|
|
|
|
|
},
|
|
|
|
|
|
watch: {
|
|
|
|
|
|
value(newVal) {
|
2025-06-13 17:58:39 +08:00
|
|
|
|
this.displayValue = newVal?.toString() || "";
|
|
|
|
|
|
},
|
2025-06-10 15:10:06 +08:00
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
handleInput(e) {
|
2025-06-13 17:58:39 +08:00
|
|
|
|
let value = e.detail.value;
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 过滤非法字符
|
|
|
|
|
|
let filtered = value.replace(/[^\d.]/g, "");
|
2025-06-10 15:10:06 +08:00
|
|
|
|
|
2025-06-13 17:58:39 +08:00
|
|
|
|
// 2. 处理多余的小数点
|
|
|
|
|
|
if (this.mode === "integer") {
|
|
|
|
|
|
filtered = filtered.replace(/\./g, ""); // 整数模式删除所有小数点
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const firstDotIndex = filtered.indexOf(".");
|
|
|
|
|
|
if (firstDotIndex !== -1) {
|
|
|
|
|
|
// 只保留第一个小数点
|
|
|
|
|
|
filtered =
|
|
|
|
|
|
filtered.substring(0, firstDotIndex + 1) +
|
|
|
|
|
|
filtered.substring(firstDotIndex + 1).replace(/\./g, "");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 处理前导0
|
|
|
|
|
|
if (filtered.startsWith("0") && filtered.length > 1 && !filtered.startsWith("0.")) {
|
|
|
|
|
|
filtered = filtered.replace(/^0+/, "0").replace(/^0([^.])/, "$1");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 处理开头的小数点
|
|
|
|
|
|
if (filtered.startsWith(".")) {
|
|
|
|
|
|
if (this.mode === "integer") {
|
|
|
|
|
|
filtered = "";
|
|
|
|
|
|
} else {
|
|
|
|
|
|
filtered = "0" + filtered;
|
|
|
|
|
|
}
|
2025-06-10 15:10:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.displayValue = filtered;
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
handleBlur() {
|
2025-06-13 17:58:39 +08:00
|
|
|
|
let value = this.displayValue;
|
2025-06-10 15:10:06 +08:00
|
|
|
|
|
|
|
|
|
|
// 空值处理
|
2025-06-13 17:58:39 +08:00
|
|
|
|
if (value === "" || value === ".") {
|
|
|
|
|
|
this.$emit("input", "");
|
|
|
|
|
|
return;
|
2025-06-10 15:10:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 17:58:39 +08:00
|
|
|
|
// 处理结尾的小数点
|
|
|
|
|
|
if (value.endsWith(".")) {
|
|
|
|
|
|
value = value.slice(0, -1);
|
2025-06-10 15:10:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 转换为数字
|
2025-06-13 17:58:39 +08:00
|
|
|
|
const numValue = Number(value);
|
2025-06-10 15:10:06 +08:00
|
|
|
|
|
2025-06-13 17:58:39 +08:00
|
|
|
|
// 小数位数处理
|
|
|
|
|
|
let processedValue = value;
|
|
|
|
|
|
if (this.mode !== "integer" && value.includes(".")) {
|
|
|
|
|
|
const parts = value.split(".");
|
|
|
|
|
|
if (parts[1].length > this.decimalLength) {
|
|
|
|
|
|
processedValue = `${parts[0]}.${parts[1].slice(0, this.decimalLength)}`;
|
2025-06-10 15:10:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 17:58:39 +08:00
|
|
|
|
// 范围校验
|
|
|
|
|
|
let finalNum = Number(processedValue);
|
|
|
|
|
|
if (finalNum < this.min) finalNum = this.min;
|
|
|
|
|
|
if (finalNum > this.max) finalNum = this.max;
|
|
|
|
|
|
|
|
|
|
|
|
// 更新值
|
|
|
|
|
|
this.displayValue = finalNum.toString();
|
|
|
|
|
|
this.$emit("input", finalNum);
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
2025-06-10 15:10:06 +08:00
|
|
|
|
</script>
|
2025-06-13 17:58:39 +08:00
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.number-input {
|
|
|
|
|
|
border: 1px solid #ddd;
|
|
|
|
|
|
padding: 8px 12px;
|
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|