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"
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 变化
6367watch (
6468 () => props .modelValue ,
6569 (value ) => {
6670 updateValue (value )
6771 }
6872)
6973
74+ // 监听 max, min, precision 变化
7075watch ([() => props .max , () => props .min , () => props .precision ], () => {
7176 const value = formatValue (inputValue .value )
7277 updateValue (value )
7378})
7479
80+ // 判断两个值是否相等
7581function isValueEqual(value1 : number | string , value2 : number | string ) {
7682 return isEqual (String (value1 ), String (value2 ))
7783}
7884
85+ // 获取初始值
7986function 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+ // 将值转换为指定精度
8795function 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+ // 获取值的精度
91101function 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+ // 严格按照步进值递增或递减
102113function 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+ // 更新值
108120function 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+ // 根据步进值改变值
133146function 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+ // 改变值
142157function 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+ // 减少值
148164function sub() {
149165 changeValue (- props .step )
150166}
151167
168+ // 增加值
152169function add() {
153170 changeValue (props .step )
154171}
155172
173+ // 处理输入事件
156174function handleInput(event : any ) {
157- const value = event .detail .value || ' '
175+ let value = event .detail .value || ' '
158176 updateValue (value , true )
159177}
160178
179+ // 处理聚焦事件
161180function handleFocus(event : any ) {
162181 emit (' focus' , event .detail )
163182}
164183
184+ // 处理失焦事件
165185function 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+ // 格式化值
173194function 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