index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div :style="{ width: item.width ? item.width + 'px' : '100%' }">
  3. <VueDatePicker
  4. v-model="value"
  5. ref="datepicker"
  6. :options="item.options || {}"
  7. locale="zh-CN"
  8. :disabled="item.disabled || false"
  9. :range="item.range || false"
  10. :model-type="timestamp"
  11. :month-picker="month"
  12. :time-picker="time"
  13. :year-picker="year"
  14. auto-apply
  15. text-input
  16. :show-now-button="item.showToday"
  17. now-button-label="今天"
  18. :enable-time-picker="item.enableTimePicker ?? false"
  19. :clearable="item.clearable ?? true"
  20. :day-names="['一', '二', '三', '四', '五', '六', '七']"
  21. v-bind="$attrs"
  22. :class="{'detailMargin': detailMargin}"
  23. @open="handleOpen"
  24. @closed="handleClosed"
  25. @update:modelValue="modelValueUpDate"
  26. >
  27. <template #trigger>
  28. <v-text-field
  29. v-model="formatText"
  30. variant="outlined"
  31. :density="item.dense || 'compact'"
  32. type="text"
  33. :rules="rules"
  34. :disabled="item.disabled"
  35. :style="{width: item.width}"
  36. :color="item.color || 'primary'"
  37. :label="item.label"
  38. :placeholder="item.placeholder || item.label"
  39. :autofocus="item.autofocus"
  40. :required="item.required"
  41. :suffix="item.suffix"
  42. :append-icon="item.appendIcon"
  43. :append-inner-icon="item.appendInnerIcon"
  44. :clearable="item.clearable"
  45. :readonly="item.readonly"
  46. :counter="item.counter"
  47. :prepend-inner-icon="item.prependInnerIcon"
  48. hide-spin-buttons
  49. :class="item.class"
  50. :hide-details="hideDetails || false"
  51. @click:clear="handleClear"
  52. @click="inputClick"
  53. @blur="inputBlur"
  54. ></v-text-field>
  55. </template>
  56. </VueDatePicker>
  57. </div>
  58. </template>
  59. <script setup>
  60. import { timesTampChange } from '@/utils/date'
  61. import { computed, ref, watch } from 'vue'
  62. defineOptions({ name:'FormUI-v-text-field'})
  63. const props = defineProps({item: Object})
  64. const emit = defineEmits(['update:modelValue', 'change'])
  65. const item = props.item
  66. const value = ref(props.modelValue)
  67. watch(() => item.value, (newVal) => {
  68. value.value = newVal - 0
  69. if (value.value) getFormatText()
  70. })
  71. const timestamp = 'timestamp' // 固定不能变
  72. // const datepicker = ref()
  73. const formatText = ref('')
  74. const modelValueUpDate = (val) => {
  75. value.value = val
  76. getFormatText()
  77. emit('update:modelValue', value.value)
  78. emit('change', value.value)
  79. }
  80. const getFormatText = () => {
  81. const format = item.format || 'Y-M-D'
  82. formatText.value = timesTampChange(value.value, format)
  83. }
  84. const handleClear = () => {
  85. value.value = null
  86. emit('change', value.value)
  87. }
  88. const rules = ref(item.rules)
  89. const handleOpen = () => {
  90. rules.value = []
  91. }
  92. const handleClosed = () => {
  93. rules.value = item.rules
  94. }
  95. const hideDetails = ref(item.hideDetails || false)
  96. const detailMargin = ref(false)
  97. const inputClick = () => {
  98. if (item.hideDetails) return
  99. hideDetails.value = true
  100. detailMargin.value = true
  101. }
  102. // const inputFocus = () => {
  103. // if (item.hideDetails) return
  104. // hideDetails.value = true
  105. // detailMargin.value = true
  106. // }
  107. const inputBlur = () => {
  108. if (item.hideDetails) return
  109. hideDetails.value = item.hideDetails || false
  110. detailMargin.value = false
  111. }
  112. // dateType: 默认 date, 即年月日
  113. const year = computed(() => {
  114. return item.dateType === 'year'
  115. })
  116. const month = computed(() => {
  117. return item.dateType === 'month'
  118. })
  119. const time = computed(() => {
  120. return item.dateType === 'time'
  121. })
  122. if (!item.format) item.format = year.value ? 'Y' : month.value ? 'Y-M' : time.value ? 'Y-M-D h:m:s' : 'Y-M-D'
  123. if (item.value) value.value = item.value - 0; getFormatText()
  124. </script>
  125. <style lang="scss" scoped>
  126. // .removeDetailHeight {}
  127. ::v-deep .dp--menu-wrapper {
  128. // top: 50px !important;
  129. left: 0 !important;
  130. }
  131. .detailMargin {
  132. margin-bottom: 22px;
  133. }
  134. </style>