index.vue 4.4 KB

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