mirror of
https://gitee.com/wot-design-uni/wot-design-uni.git
synced 2025-12-06 09:08:51 +08:00
parent
a0da88c5db
commit
dd1a005964
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* @Author: weisheng
|
* @Author: weisheng
|
||||||
* @Date: 2024-03-15 20:40:34
|
* @Date: 2024-03-15 20:40:34
|
||||||
* @LastEditTime: 2024-12-31 00:33:21
|
* @LastEditTime: 2025-02-11 18:38:54
|
||||||
* @LastEditors: weisheng
|
* @LastEditors: weisheng
|
||||||
* @Description:
|
* @Description:
|
||||||
* @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-input-number/types.ts
|
* @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>
|
<template>
|
||||||
<view :class="`wd-input-number ${customClass} ${disabled ? 'is-disabled' : ''} ${withoutInput ? 'is-without-input' : ''}`" :style="customStyle">
|
<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">
|
<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>
|
<wd-icon name="decrease" custom-class="wd-input-number__action-icon"></wd-icon>
|
||||||
</view>
|
</view>
|
||||||
|
<!-- 输入框 -->
|
||||||
<view v-if="!withoutInput" class="wd-input-number__inner" @click.stop="">
|
<view v-if="!withoutInput" class="wd-input-number__inner" @click.stop="">
|
||||||
<input
|
<input
|
||||||
class="wd-input-number__input"
|
class="wd-input-number__input"
|
||||||
@ -18,6 +20,7 @@
|
|||||||
/>
|
/>
|
||||||
<view class="wd-input-number__input-border"></view>
|
<view class="wd-input-number__input-border"></view>
|
||||||
</view>
|
</view>
|
||||||
|
<!-- 加号按钮 -->
|
||||||
<view :class="`wd-input-number__action ${maxDisabled || disablePlus ? 'is-disabled' : ''}`" @click="add">
|
<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>
|
<wd-icon name="add" custom-class="wd-input-number__action-icon"></wd-icon>
|
||||||
</view>
|
</view>
|
||||||
@ -60,6 +63,7 @@ const maxDisabled = computed(() => {
|
|||||||
return disabled || Number(value) >= max || changeStep(value, step) > max
|
return disabled || Number(value) >= max || changeStep(value, step) > max
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 监听 modelValue 变化
|
||||||
watch(
|
watch(
|
||||||
() => props.modelValue,
|
() => props.modelValue,
|
||||||
(value) => {
|
(value) => {
|
||||||
@ -67,15 +71,18 @@ watch(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 监听 max, min, precision 变化
|
||||||
watch([() => props.max, () => props.min, () => props.precision], () => {
|
watch([() => props.max, () => props.min, () => props.precision], () => {
|
||||||
const value = formatValue(inputValue.value)
|
const value = formatValue(inputValue.value)
|
||||||
updateValue(value)
|
updateValue(value)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 判断两个值是否相等
|
||||||
function isValueEqual(value1: number | string, value2: number | string) {
|
function isValueEqual(value1: number | string, value2: number | string) {
|
||||||
return isEqual(String(value1), String(value2))
|
return isEqual(String(value1), String(value2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取初始值
|
||||||
function getInitValue() {
|
function getInitValue() {
|
||||||
const formatted = formatValue(props.modelValue)
|
const formatted = formatValue(props.modelValue)
|
||||||
if (!isValueEqual(formatted, props.modelValue)) {
|
if (!isValueEqual(formatted, props.modelValue)) {
|
||||||
@ -84,10 +91,13 @@ function getInitValue() {
|
|||||||
return formatted
|
return formatted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 将值转换为指定精度
|
||||||
function toPrecision(value: number) {
|
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) {
|
function getPrecision(value?: number) {
|
||||||
if (!isDef(value)) return 0
|
if (!isDef(value)) return 0
|
||||||
const valueString = value.toString()
|
const valueString = value.toString()
|
||||||
@ -99,12 +109,14 @@ function getPrecision(value?: number) {
|
|||||||
return precision
|
return precision
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 严格按照步进值递增或递减
|
||||||
function toStrictlyStep(value: number | string) {
|
function toStrictlyStep(value: number | string) {
|
||||||
const stepPrecision = getPrecision(props.step)
|
const stepPrecision = getPrecision(props.step)
|
||||||
const precisionFactory = Math.pow(10, stepPrecision)
|
const precisionFactory = Math.pow(10, stepPrecision)
|
||||||
return (Math.round(Number(value) / props.step) * precisionFactory * props.step) / precisionFactory
|
return (Math.round(Number(value) / props.step) * precisionFactory * props.step) / precisionFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新值
|
||||||
function updateValue(value: string | number, fromUser: boolean = false) {
|
function updateValue(value: string | number, fromUser: boolean = false) {
|
||||||
if (isValueEqual(value, inputValue.value)) {
|
if (isValueEqual(value, inputValue.value)) {
|
||||||
return
|
return
|
||||||
@ -130,38 +142,46 @@ function updateValue(value: string | number, fromUser: boolean = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据步进值改变值
|
||||||
function changeStep(val: string | number, step: number) {
|
function changeStep(val: string | number, step: number) {
|
||||||
val = Number(val)
|
val = Number(val)
|
||||||
if (isNaN(val)) {
|
if (isNaN(val)) {
|
||||||
return props.min
|
return props.min
|
||||||
}
|
}
|
||||||
const precisionFactory = Math.pow(10, props.precision)
|
const precision = Math.max(getPrecision(val), getPrecision(step))
|
||||||
return toPrecision((val * precisionFactory + step * precisionFactory) / precisionFactory)
|
const precisionFactor = Math.pow(10, precision)
|
||||||
|
return toPrecision((val * precisionFactor + step * precisionFactor) / precisionFactor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 改变值
|
||||||
function changeValue(step: number) {
|
function changeValue(step: number) {
|
||||||
if ((step < 0 && (minDisabled.value || props.disableMinus)) || (step > 0 && (maxDisabled.value || props.disablePlus))) return
|
if ((step < 0 && (minDisabled.value || props.disableMinus)) || (step > 0 && (maxDisabled.value || props.disablePlus))) return
|
||||||
const value = changeStep(inputValue.value, step)
|
const value = changeStep(inputValue.value, step)
|
||||||
updateValue(value, true)
|
updateValue(value, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 减少值
|
||||||
function sub() {
|
function sub() {
|
||||||
changeValue(-props.step)
|
changeValue(-props.step)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 增加值
|
||||||
function add() {
|
function add() {
|
||||||
changeValue(props.step)
|
changeValue(props.step)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理输入事件
|
||||||
function handleInput(event: any) {
|
function handleInput(event: any) {
|
||||||
const value = event.detail.value || ''
|
let value = event.detail.value || ''
|
||||||
updateValue(value, true)
|
updateValue(value, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理聚焦事件
|
||||||
function handleFocus(event: any) {
|
function handleFocus(event: any) {
|
||||||
emit('focus', event.detail)
|
emit('focus', event.detail)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理失焦事件
|
||||||
function handleBlur(event: any) {
|
function handleBlur(event: any) {
|
||||||
const value = event.detail.value || ''
|
const value = event.detail.value || ''
|
||||||
updateValue(value, true)
|
updateValue(value, true)
|
||||||
@ -170,6 +190,7 @@ function handleBlur(event: any) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 格式化值
|
||||||
function formatValue(value: string | number) {
|
function formatValue(value: string | number) {
|
||||||
if (props.allowNull && (!isDef(value) || value === '')) {
|
if (props.allowNull && (!isDef(value) || value === '')) {
|
||||||
return ''
|
return ''
|
||||||
@ -188,7 +209,7 @@ function formatValue(value: string | number) {
|
|||||||
formatted = Math.min(Math.max(formatted, props.min), props.max)
|
formatted = Math.min(Math.max(formatted, props.min), props.max)
|
||||||
|
|
||||||
if (isDef(props.precision)) {
|
if (isDef(props.precision)) {
|
||||||
formatted = Number(formatted.toFixed(props.precision))
|
formatted = toPrecision(formatted)
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatted
|
return formatted
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user