index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div :style="{ width: item.width ? item.width + 'px' : '100%' }">
  3. <v-text-field
  4. v-model="value"
  5. variant="outlined"
  6. :density="item.dense || 'compact'"
  7. :type="item.type"
  8. :rules="item.rules"
  9. :disabled="item.disabled"
  10. :style="{width: item.width}"
  11. :color="item.color || 'primary'"
  12. :label="item.label"
  13. :placeholder="item.placeholder || item.label"
  14. :autofocus="item.autofocus"
  15. :required="item.required"
  16. :class="item.class"
  17. :suffix="item.suffix"
  18. :append-icon="item.appendIcon"
  19. :append-inner-icon="item.appendInnerIcon"
  20. :clearable="item.clearable"
  21. :readonly="item.readonly"
  22. :counter="item.counter"
  23. :prepend-inner-icon="item.prependInnerIcon"
  24. hide-spin-buttons
  25. :hide-details="item.hideDetails || false"
  26. @wheel="$event => handleWheel($event, item)"
  27. @update:modelValue="modelValueUpDate"
  28. @click:append="appendClick"
  29. @click:append-inner="appendInnerClick"
  30. @keyup.enter="handleKeyup"
  31. @click:clear="handleClear"
  32. ></v-text-field>
  33. </div>
  34. </template>
  35. <script setup>
  36. import { debounce } from 'lodash'
  37. import { defineEmits, ref, watch } from 'vue';
  38. defineOptions({ name:'FormUI-v-text-field'})
  39. const props = defineProps({item: Object, modelValue: [String, Number]})
  40. const emit = defineEmits(['update:modelValue', 'change', 'appendClick', 'appendInnerClick', 'enter'])
  41. const item = props.item
  42. const value = ref(props.modelValue)
  43. const searchDebouncedTime = item?.searchDebouncedTime === 0 ? ref(0) : ref(500)
  44. watch(() => props.modelValue, (newVal) => {
  45. value.value = newVal
  46. })
  47. const modelValueUpDate = (val) => {
  48. value.value = val
  49. emit('update:modelValue', value.value)
  50. debouncedCallbackUpDate(value.value) // emit('change', value.value)
  51. }
  52. const debouncedCallbackUpDate = debounce(newValue => {
  53. emit('change', newValue)
  54. }, searchDebouncedTime.value)
  55. const appendClick = () => {
  56. if (item.appendClick) item.appendClick()
  57. emit('appendClick', value.value)
  58. }
  59. const appendInnerClick = () => {
  60. if (item.appendInnerClick) item.appendInnerClick(value.value)
  61. emit('appendInnerClick', value.value)
  62. }
  63. const handleClear = () => {
  64. emit('enter', value.value)
  65. }
  66. const handleKeyup = () => {
  67. emit('enter', value.value)
  68. }
  69. const handleWheel = (event, item) => {
  70. if (item.type !== 'number') return
  71. event.preventDefault()
  72. if (event.deltaY > 0) {
  73. item.value--
  74. } else {
  75. item.value++
  76. }
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. </style>