mirror of
https://gitee.com/wot-design-uni/wot-design-uni.git
synced 2025-12-06 17:18:40 +08:00
parent
a0da88c5db
commit
dd1a005964
@ -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),
|
||||
/**
|
||||
* 是否禁用
|
||||
*/
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user