fix: 🐛 修复 InputNumber 设置了precision后无法输入小数点的问题 (#886)

 Closes: #878
This commit is contained in:
不如摸鱼去 2025-02-11 18:56:01 +08:00 committed by GitHub
parent a0da88c5db
commit dd1a005964
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 7 deletions

View File

@ -1,7 +1,7 @@
/*
* @Author: weisheng
* @Date: 2024-03-15 20:40:34
* @LastEditTime: 2024-12-31 00:33:21
* @LastEditTime: 2025-02-11 18:38:54
* @LastEditors: weisheng
* @Description:
* @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-input-number/types.ts
@ -37,7 +37,7 @@ export const inputNumberProps = {
/**
*
*/
precision: makeNumberProp(0),
precision: makeNumericProp(0),
/**
*
*/

View File

@ -1,8 +1,10 @@
<template>
<view :class="`wd-input-number ${customClass} ${disabled ? 'is-disabled' : ''} ${withoutInput ? 'is-without-input' : ''}`" :style="customStyle">
<!-- 减号按钮 -->
<view :class="`wd-input-number__action ${minDisabled || disableMinus ? 'is-disabled' : ''}`" @click="sub">
<wd-icon name="decrease" custom-class="wd-input-number__action-icon"></wd-icon>
</view>
<!-- 输入框 -->
<view v-if="!withoutInput" class="wd-input-number__inner" @click.stop="">
<input
class="wd-input-number__input"
@ -18,6 +20,7 @@
/>
<view class="wd-input-number__input-border"></view>
</view>
<!-- 加号按钮 -->
<view :class="`wd-input-number__action ${maxDisabled || disablePlus ? 'is-disabled' : ''}`" @click="add">
<wd-icon name="add" custom-class="wd-input-number__action-icon"></wd-icon>
</view>
@ -60,6 +63,7 @@ const maxDisabled = computed(() => {
return disabled || Number(value) >= max || changeStep(value, step) > max
})
// modelValue
watch(
() => props.modelValue,
(value) => {
@ -67,15 +71,18 @@ watch(
}
)
// max, min, precision
watch([() => props.max, () => props.min, () => props.precision], () => {
const value = formatValue(inputValue.value)
updateValue(value)
})
//
function isValueEqual(value1: number | string, value2: number | string) {
return isEqual(String(value1), String(value2))
}
//
function getInitValue() {
const formatted = formatValue(props.modelValue)
if (!isValueEqual(formatted, props.modelValue)) {
@ -84,10 +91,13 @@ function getInitValue() {
return formatted
}
//
function toPrecision(value: number) {
return Number(parseFloat(`${Math.round(value * Math.pow(10, props.precision)) / Math.pow(10, props.precision)}`).toFixed(props.precision))
const precision = Number(props.precision)
return Number(parseFloat(`${Math.round(value * Math.pow(10, precision)) / Math.pow(10, precision)}`).toFixed(precision))
}
//
function getPrecision(value?: number) {
if (!isDef(value)) return 0
const valueString = value.toString()
@ -99,12 +109,14 @@ function getPrecision(value?: number) {
return precision
}
//
function toStrictlyStep(value: number | string) {
const stepPrecision = getPrecision(props.step)
const precisionFactory = Math.pow(10, stepPrecision)
return (Math.round(Number(value) / props.step) * precisionFactory * props.step) / precisionFactory
}
//
function updateValue(value: string | number, fromUser: boolean = false) {
if (isValueEqual(value, inputValue.value)) {
return
@ -130,38 +142,46 @@ function updateValue(value: string | number, fromUser: boolean = false) {
}
}
//
function changeStep(val: string | number, step: number) {
val = Number(val)
if (isNaN(val)) {
return props.min
}
const precisionFactory = Math.pow(10, props.precision)
return toPrecision((val * precisionFactory + step * precisionFactory) / precisionFactory)
const precision = Math.max(getPrecision(val), getPrecision(step))
const precisionFactor = Math.pow(10, precision)
return toPrecision((val * precisionFactor + step * precisionFactor) / precisionFactor)
}
//
function changeValue(step: number) {
if ((step < 0 && (minDisabled.value || props.disableMinus)) || (step > 0 && (maxDisabled.value || props.disablePlus))) return
const value = changeStep(inputValue.value, step)
updateValue(value, true)
}
//
function sub() {
changeValue(-props.step)
}
//
function add() {
changeValue(props.step)
}
//
function handleInput(event: any) {
const value = event.detail.value || ''
let value = event.detail.value || ''
updateValue(value, true)
}
//
function handleFocus(event: any) {
emit('focus', event.detail)
}
//
function handleBlur(event: any) {
const value = event.detail.value || ''
updateValue(value, true)
@ -170,6 +190,7 @@ function handleBlur(event: any) {
})
}
//
function formatValue(value: string | number) {
if (props.allowNull && (!isDef(value) || value === '')) {
return ''
@ -188,7 +209,7 @@ function formatValue(value: string | number) {
formatted = Math.min(Math.max(formatted, props.min), props.max)
if (isDef(props.precision)) {
formatted = Number(formatted.toFixed(props.precision))
formatted = toPrecision(formatted)
}
return formatted