index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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="soFar || 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="soFar ? [] : rules"
  36. :disabled="soFar || 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. const soFar = ref(false)
  71. watch(() => item.value, (newVal) => {
  72. value.value = newVal - 0
  73. if (value.value) getFormatText()
  74. })
  75. const timestamp = 'timestamp' // 固定不能变
  76. const formatText = ref('')
  77. const modelValueUpDate = (val) => {
  78. value.value = val
  79. getFormatText()
  80. emit('update:modelValue', value.value)
  81. emit('change', value.value)
  82. }
  83. const getFormatText = () => {
  84. const format = item.format || 'Y-M-D'
  85. formatText.value = timesTampChange(value.value, format)
  86. }
  87. const handleClear = () => {
  88. value.value = null
  89. emit('change', value.value)
  90. }
  91. const rules = ref(item.rules)
  92. const handleOpen = () => {
  93. rules.value = []
  94. }
  95. const handleClosed = () => {
  96. rules.value = item.rules
  97. }
  98. const hideDetails = ref(item.hideDetails || false)
  99. const detailMargin = ref(false)
  100. const inputClick = () => {
  101. if (item.hideDetails) return
  102. hideDetails.value = true
  103. detailMargin.value = true
  104. }
  105. const inputBlur = () => {
  106. if (item.hideDetails) return
  107. hideDetails.value = item.hideDetails || false
  108. detailMargin.value = false
  109. }
  110. // dateType: 默认 date, 即年月日
  111. const year = computed(() => {
  112. return item.dateType === 'year'
  113. })
  114. const month = computed(() => {
  115. return item.dateType === 'month'
  116. })
  117. const time = computed(() => {
  118. return item.dateType === 'time'
  119. })
  120. if (!item.format) item.format = year.value ? 'Y' : month.value ? 'Y-M' : time.value ? 'Y-M-D h:m:s' : 'Y-M-D'
  121. if (item.value) value.value = item.value; getFormatText()
  122. </script>
  123. <style lang="scss" scoped>
  124. // .removeDetailHeight {}
  125. :deep(.dp--menu-wrapper) {
  126. // top: 50px !important;
  127. left: 0 !important;
  128. }
  129. .detailMargin {
  130. margin-bottom: 22px;
  131. }
  132. </style>