index.vue 4.1 KB

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