index.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. @blur="handleBlur"
  33. ></v-text-field>
  34. </div>
  35. </template>
  36. <script setup>
  37. import { debounce } from 'lodash'
  38. import { ref, watch } from 'vue';
  39. defineOptions({ name:'FormUI-v-text-field'})
  40. const props = defineProps({item: Object, modelValue: [String, Number]})
  41. const emit = defineEmits(['update:modelValue', 'change', 'appendClick', 'appendInnerClick', 'enter', 'blur'])
  42. const item = props.item
  43. const value = ref(props.modelValue)
  44. const searchDebouncedTime = item?.searchDebouncedTime === 0 ? ref(0) : ref(500)
  45. watch(() => props.modelValue, (newVal) => {
  46. value.value = newVal
  47. })
  48. const modelValueUpDate = (val) => {
  49. value.value = val
  50. emit('update:modelValue', value.value)
  51. debouncedCallbackUpDate(value.value) // emit('change', value.value)
  52. }
  53. const debouncedCallbackUpDate = debounce(newValue => {
  54. emit('change', newValue)
  55. }, searchDebouncedTime.value)
  56. const appendClick = () => {
  57. if (item.appendClick) item.appendClick()
  58. emit('appendClick', value.value)
  59. }
  60. const appendInnerClick = () => {
  61. if (item.appendInnerClick) item.appendInnerClick(value.value)
  62. emit('appendInnerClick', value.value)
  63. }
  64. const handleClear = () => {
  65. emit('enter', value.value)
  66. }
  67. const handleKeyup = () => {
  68. emit('enter', value.value)
  69. }
  70. const handleBlur = () => {
  71. emit('blur', props.item, value.value)
  72. }
  73. const handleWheel = (event, item) => {
  74. if (item.type !== 'number') return
  75. event.preventDefault()
  76. if (event.deltaY > 0) {
  77. item.value--
  78. } else {
  79. item.value++
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. </style>