11<template >
22 <view :class =" `wd-input-number ${customClass} ${disabled ? 'is-disabled' : ''} ${withoutInput ? 'is-without-input' : ''}`" :style =" customStyle" >
33 <!-- 减号按钮 -->
4- <view :class =" `wd-input-number__action ${minDisabled || disableMinus ? 'is-disabled' : ''}`" @click =" sub" >
4+ <view
5+ :class =" `wd-input-number__action ${minDisabled || disableMinus ? 'is-disabled' : ''}`"
6+ @click =" handleClick('sub')"
7+ @touchstart =" handleTouchStart('sub')"
8+ @touchend.stop =" handleTouchEnd"
9+ >
510 <wd-icon name =" decrease" custom-class =" wd-input-number__action-icon" ></wd-icon >
611 </view >
712 <!-- 输入框 -->
2126 <view class =" wd-input-number__input-border" ></view >
2227 </view >
2328 <!-- 加号按钮 -->
24- <view :class =" `wd-input-number__action ${maxDisabled || disablePlus ? 'is-disabled' : ''}`" @click =" add" >
29+ <view
30+ :class =" `wd-input-number__action ${maxDisabled || disablePlus ? 'is-disabled' : ''}`"
31+ @click =" handleClick('add')"
32+ @touchstart =" handleTouchStart('add')"
33+ @touchend.stop =" handleTouchEnd"
34+ >
2535 <wd-icon name =" add" custom-class =" wd-input-number__action-icon" ></wd-icon >
2636 </view >
2737 </view >
@@ -42,12 +52,13 @@ export default {
4252import wdIcon from ' ../wd-icon/wd-icon.vue'
4353import { computed , nextTick , ref , watch } from ' vue'
4454import { isDef , isEqual } from ' ../common/util'
45- import { inputNumberProps , InputNumberEventType } from ' ./types'
55+ import { inputNumberProps , InputNumberEventType , type OperationType } from ' ./types'
4656import { callInterceptor } from ' ../common/interceptor'
4757
4858const props = defineProps (inputNumberProps )
4959const emit = defineEmits ([' focus' , ' blur' , ' change' , ' update:modelValue' ])
5060const inputValue = ref <string | number >(getInitValue ()) // 输入框的值
61+ let longPressTimer: ReturnType <typeof setTimeout > | null = null // 长按定时器
5162
5263/**
5364 * 判断数字是否达到最小值限制
@@ -219,14 +230,10 @@ function changeValue(step: number) {
219230 updateValue (value , InputNumberEventType .Button )
220231}
221232
222- // 减少值
223- function sub() {
224- changeValue (- props .step )
225- }
226-
227- // 增加值
228- function add() {
229- changeValue (props .step )
233+ // 增减值
234+ function handleClick(type : OperationType ) {
235+ const diff = type === ' add' ? props .step : - props .step
236+ changeValue (diff )
230237}
231238
232239function handleInput(event : any ) {
@@ -240,6 +247,39 @@ function handleBlur(event: any) {
240247 emit (' blur' , { value })
241248}
242249
250+ // 每隔一段时间,重新调用自身,达到长按加减效果
251+ function longPressStep(type : OperationType ) {
252+ clearlongPressTimer ()
253+ longPressTimer = setTimeout (() => {
254+ handleClick (type )
255+ longPressStep (type )
256+ }, 250 )
257+ }
258+
259+ // 按下一段时间后,达到长按状态
260+ function handleTouchStart(type : OperationType ) {
261+ if (! props .longPress ) return
262+ clearlongPressTimer ()
263+ longPressTimer = setTimeout (() => {
264+ handleClick (type )
265+ longPressStep (type )
266+ }, 600 )
267+ }
268+
269+ // 触摸结束,清除定时器,停止长按加减
270+ function handleTouchEnd() {
271+ if (! props .longPress ) return
272+ clearlongPressTimer ()
273+ }
274+
275+ // 清除定时器
276+ function clearlongPressTimer() {
277+ if (longPressTimer ) {
278+ clearTimeout (longPressTimer )
279+ longPressTimer = null
280+ }
281+ }
282+
243283// 处理聚焦事件
244284function handleFocus(event : any ) {
245285 emit (' focus' , event .detail )
0 commit comments