SwipeInput.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <el-input-number
  3. class="swipe-input"
  4. v-model="numberValue"
  5. :step="step"
  6. :max="max"
  7. :min="min"
  8. :controls="false"
  9. :label="label"
  10. @change="change"
  11. >
  12. </el-input-number>
  13. </template>
  14. <script setup lang="ts">
  15. import { usePointerSwipe, useVModel, isDefined, useMagicKeys } from '@vueuse/core'
  16. import { useSlots, watch, ref, computed } from 'vue'
  17. import { toFixed } from '@/utils/common'
  18. // import { useMainStore } from '@/store';
  19. // import { storeToRefs } from 'pinia';
  20. // import { mm2px, px2mm } from '@/utils/image';
  21. import useHandleActive from '@/hooks/useHandleActive'
  22. const { handleInput } = useHandleActive()
  23. const props = withDefaults(
  24. defineProps<{
  25. label?: string
  26. modelValue?: number
  27. modelEvent?: 'change' | 'input'
  28. step?: number
  29. max?: number
  30. min?: number
  31. }>(),
  32. {
  33. modelEvent: 'change',
  34. step: 0.01,
  35. },
  36. )
  37. const emit = defineEmits<{
  38. (e: 'update:modelValue', value: number | undefined): void
  39. (e: 'change', value: number | undefined, ev: Event): void
  40. (e: 'swipe', value: number | undefined, ev: Event): void
  41. }>()
  42. const slots = useSlots()
  43. const numberValue = useVModel(props, 'modelValue', emit)
  44. watch(numberValue, (value) => {
  45. if (!value) return
  46. numberValue.value = toFixed(value)
  47. }, { immediate: true})
  48. const change = (value: number | undefined, ev: Event) => {
  49. if (!value) return
  50. value = handleInput(value)
  51. emit('change', value, ev)
  52. }
  53. // Swipe
  54. // const { shift, alt } = useMagicKeys()
  55. // const labelRef = ref<HTMLElement>()
  56. // const startValue = ref<number>()
  57. // const { posStart, posEnd } = usePointerSwipe(labelRef, {
  58. // threshold: 0,
  59. // onSwipeStart: () => {
  60. // startValue.value = numberValue.value
  61. // },
  62. // onSwipe: (e) => {
  63. // // 检查startValue的值是否是数字,如果不是,退出函数
  64. // if (!isNumber(startValue.value)) return
  65. // // 根据props.step的值调整步长
  66. // let step = props.step
  67. // if (shift.value) step *= 10
  68. // if (alt.value) step /= 10
  69. // step = Math.max(step, 0.01)
  70. // // 根据鼠标拖动的距离计算新的数值
  71. // let value = startValue.value + Math.round(posEnd.x - posStart.x) * step
  72. // // 如果props.min或props.max存在,则确保新值在指定范围内
  73. // if (isDefined(props.min) && value < props.min) value = props.min
  74. // if (isDefined(props.max) && value > props.max) value = props.max
  75. // // 四舍五入计算的值,并将其分配给numberValue
  76. // value = toFixed(value)
  77. // numberValue.value = value
  78. // // 调用swipe函数并传递新值和事件对象
  79. // emit('swipe', value, e)
  80. // },
  81. // onSwipeEnd: (e) => {
  82. // emit('change', numberValue.value, e)
  83. // startValue.value = undefined
  84. // },
  85. // })
  86. // const hasLabel = computed(() => !!props.label || !!slots.label)
  87. </script>
  88. <style scoped lang="scss">
  89. .swipe-input {
  90. :deep(.el-input__wrapper) {
  91. padding: 1px;
  92. }
  93. :deep(.el-input__prefix-inner) {
  94. margin: 0;
  95. width: 25px;
  96. }
  97. :deep(.label-ref) {
  98. margin: 0;
  99. }
  100. }
  101. </style>