Skip to content

Commit dd1a005

Browse files
fix: 🐛 修复 InputNumber 设置了precision后无法输入小数点的问题 (#886)
Closes: #878
1 parent a0da88c commit dd1a005

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

src/uni_modules/wot-design-uni/components/wd-input-number/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @Author: weisheng
33
* @Date: 2024-03-15 20:40:34
4-
* @LastEditTime: 2024-12-31 00:33:21
4+
* @LastEditTime: 2025-02-11 18:38:54
55
* @LastEditors: weisheng
66
* @Description:
77
* @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-input-number/types.ts
@@ -37,7 +37,7 @@ export const inputNumberProps = {
3737
/**
3838
* 数值精度
3939
*/
40-
precision: makeNumberProp(0),
40+
precision: makeNumericProp(0),
4141
/**
4242
* 是否禁用
4343
*/

src/uni_modules/wot-design-uni/components/wd-input-number/wd-input-number.vue

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<template>
22
<view :class="`wd-input-number ${customClass} ${disabled ? 'is-disabled' : ''} ${withoutInput ? 'is-without-input' : ''}`" :style="customStyle">
3+
<!-- 减号按钮 -->
34
<view :class="`wd-input-number__action ${minDisabled || disableMinus ? 'is-disabled' : ''}`" @click="sub">
45
<wd-icon name="decrease" custom-class="wd-input-number__action-icon"></wd-icon>
56
</view>
7+
<!-- 输入框 -->
68
<view v-if="!withoutInput" class="wd-input-number__inner" @click.stop="">
79
<input
810
class="wd-input-number__input"
@@ -18,6 +20,7 @@
1820
/>
1921
<view class="wd-input-number__input-border"></view>
2022
</view>
23+
<!-- 加号按钮 -->
2124
<view :class="`wd-input-number__action ${maxDisabled || disablePlus ? 'is-disabled' : ''}`" @click="add">
2225
<wd-icon name="add" custom-class="wd-input-number__action-icon"></wd-icon>
2326
</view>
@@ -60,22 +63,26 @@ const maxDisabled = computed(() => {
6063
return disabled || Number(value) >= max || changeStep(value, step) > max
6164
})
6265
66+
// 监听 modelValue 变化
6367
watch(
6468
() => props.modelValue,
6569
(value) => {
6670
updateValue(value)
6771
}
6872
)
6973
74+
// 监听 max, min, precision 变化
7075
watch([() => props.max, () => props.min, () => props.precision], () => {
7176
const value = formatValue(inputValue.value)
7277
updateValue(value)
7378
})
7479
80+
// 判断两个值是否相等
7581
function isValueEqual(value1: number | string, value2: number | string) {
7682
return isEqual(String(value1), String(value2))
7783
}
7884
85+
// 获取初始值
7986
function getInitValue() {
8087
const formatted = formatValue(props.modelValue)
8188
if (!isValueEqual(formatted, props.modelValue)) {
@@ -84,10 +91,13 @@ function getInitValue() {
8491
return formatted
8592
}
8693
94+
// 将值转换为指定精度
8795
function toPrecision(value: number) {
88-
return Number(parseFloat(`${Math.round(value * Math.pow(10, props.precision)) / Math.pow(10, props.precision)}`).toFixed(props.precision))
96+
const precision = Number(props.precision)
97+
return Number(parseFloat(`${Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision)}`).toFixed(precision))
8998
}
9099
100+
// 获取值的精度
91101
function getPrecision(value?: number) {
92102
if (!isDef(value)) return 0
93103
const valueString = value.toString()
@@ -99,12 +109,14 @@ function getPrecision(value?: number) {
99109
return precision
100110
}
101111
112+
// 严格按照步进值递增或递减
102113
function toStrictlyStep(value: number | string) {
103114
const stepPrecision = getPrecision(props.step)
104115
const precisionFactory = Math.pow(10, stepPrecision)
105116
return (Math.round(Number(value) / props.step) * precisionFactory * props.step) / precisionFactory
106117
}
107118
119+
// 更新值
108120
function updateValue(value: string | number, fromUser: boolean = false) {
109121
if (isValueEqual(value, inputValue.value)) {
110122
return
@@ -130,38 +142,46 @@ function updateValue(value: string | number, fromUser: boolean = false) {
130142
}
131143
}
132144
145+
// 根据步进值改变值
133146
function changeStep(val: string | number, step: number) {
134147
val = Number(val)
135148
if (isNaN(val)) {
136149
return props.min
137150
}
138-
const precisionFactory = Math.pow(10, props.precision)
139-
return toPrecision((val * precisionFactory + step * precisionFactory) / precisionFactory)
151+
const precision = Math.max(getPrecision(val), getPrecision(step))
152+
const precisionFactor = Math.pow(10, precision)
153+
return toPrecision((val * precisionFactor + step * precisionFactor) / precisionFactor)
140154
}
141155
156+
// 改变值
142157
function changeValue(step: number) {
143158
if ((step < 0 && (minDisabled.value || props.disableMinus)) || (step > 0 && (maxDisabled.value || props.disablePlus))) return
144159
const value = changeStep(inputValue.value, step)
145160
updateValue(value, true)
146161
}
147162
163+
// 减少值
148164
function sub() {
149165
changeValue(-props.step)
150166
}
151167
168+
// 增加值
152169
function add() {
153170
changeValue(props.step)
154171
}
155172
173+
// 处理输入事件
156174
function handleInput(event: any) {
157-
const value = event.detail.value || ''
175+
let value = event.detail.value || ''
158176
updateValue(value, true)
159177
}
160178
179+
// 处理聚焦事件
161180
function handleFocus(event: any) {
162181
emit('focus', event.detail)
163182
}
164183
184+
// 处理失焦事件
165185
function handleBlur(event: any) {
166186
const value = event.detail.value || ''
167187
updateValue(value, true)
@@ -170,6 +190,7 @@ function handleBlur(event: any) {
170190
})
171191
}
172192
193+
// 格式化值
173194
function formatValue(value: string | number) {
174195
if (props.allowNull && (!isDef(value) || value === '')) {
175196
return ''
@@ -188,7 +209,7 @@ function formatValue(value: string | number) {
188209
formatted = Math.min(Math.max(formatted, props.min), props.max)
189210
190211
if (isDef(props.precision)) {
191-
formatted = Number(formatted.toFixed(props.precision))
212+
formatted = toPrecision(formatted)
192213
}
193214
194215
return formatted

0 commit comments

Comments
 (0)