indexCopy.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div :style="{ width: item.width ? item.width + 'px' : '100%' }">
  3. <div class="d-flex align-center">
  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 : []"
  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 class="form-label" :style="{'width': item.labelWidth + 'px'}">{{ item.label }}</div>
  64. <el-config-provider :locale="zhCn">
  65. <el-date-picker
  66. style="flex: 1;"
  67. v-model="value"
  68. size="large"
  69. :type="item.mode || 'date'"
  70. :placeholder="item.placeholder || '请选择'"
  71. :start-placeholder="item.startPlaceholder || '开始日期'"
  72. :end-placeholder="item.endPlaceholder || '结束日期'"
  73. :format="item.format"
  74. :value-format="item.valueFormat || 'x'"
  75. :disabled="item.disabled"
  76. :disabled-date="disabledDates"
  77. :date-format="item.dateFormat"
  78. :time-format="item.timeFormat"
  79. :default-value="item.defaultValue"
  80. @change="modelValueUpDate"
  81. @clear="handleClear"
  82. @blur="handleOpen"
  83. >
  84. </el-date-picker>
  85. </el-config-provider>
  86. </div>
  87. </div>
  88. </template>
  89. <script setup>
  90. defineOptions({ name:'FormUI-v-text-field'})
  91. import { timesTampChange } from '@/utils/date'
  92. import { computed, ref, watch } from 'vue'
  93. import zhCn from 'element-plus/es/locale/lang/zh-cn'
  94. // import { ElDatePicker } from 'element-plus'
  95. const props = defineProps({item: Object, modelValue: [String, Number, Boolean, Array], changeFn: Function})
  96. const emit = defineEmits(['update:modelValue', 'change'])
  97. const item = props.item
  98. const value = ref(props.modelValue)
  99. watch(() => props.modelValue,
  100. (newVal) => {
  101. modelValueUpDate(newVal)
  102. },
  103. // { immediate: true },
  104. // { deep: true }
  105. )
  106. // 过去的日期不可选
  107. const disabledDates = (date) => {
  108. const currentDate = new Date()
  109. if (!props.item.disabledDate) return false
  110. currentDate.setDate(currentDate.getDate() - 1)
  111. return date.getTime() < currentDate.getTime()
  112. }
  113. // const timestamp = 'timestamp' // 固定不能变
  114. const formatText = ref('')
  115. const modelValueUpDate = (val) => {
  116. value.value = val
  117. getFormatText()
  118. emit('update:modelValue', value.value)
  119. emit('change', value.value)
  120. }
  121. const getFormatText = () => {
  122. const format = item.format || 'Y-M-D'
  123. formatText.value = timesTampChange(value.value, format)
  124. }
  125. const handleClear = () => {
  126. value.value = null
  127. emit('change', value.value)
  128. }
  129. const rules = ref(item.rules)
  130. watch(() => item.rules,
  131. (newVal) => {
  132. rules.value = newVal
  133. },
  134. { immediate: true },
  135. { deep: true }
  136. )
  137. const handleOpen = () => {
  138. rules.value = []
  139. }
  140. // const handleClosed = () => {
  141. // rules.value = item.rules
  142. // }
  143. // const hideDetails = ref(item.hideDetails || false)
  144. // const detailMargin = ref(false)
  145. // const inputClick = () => {
  146. // if (item.hideDetails) return
  147. // hideDetails.value = true
  148. // detailMargin.value = true
  149. // }
  150. // const inputBlur = () => {
  151. // if (item.hideDetails) return
  152. // hideDetails.value = item.hideDetails || false
  153. // detailMargin.value = false
  154. // }
  155. // dateType: 默认 date, 即年月日
  156. const year = computed(() => {
  157. return item.dateType === 'year'
  158. })
  159. const month = computed(() => {
  160. return item.dateType === 'month'
  161. })
  162. const time = computed(() => {
  163. return item.dateType === 'time'
  164. })
  165. if (!item.format) item.format = year.value ? 'Y' : month.value ? 'Y-M' : time.value ? 'Y-M-D h:m:s' : 'Y-M-D'
  166. if (item.value) value.value = item.value; getFormatText()
  167. </script>
  168. <style lang="scss" scoped>
  169. // .removeDetailHeight {}
  170. :deep(.dp--menu-wrapper) {
  171. // top: 50px !important;
  172. left: 0 !important;
  173. }
  174. .detailMargin {
  175. margin-bottom: 22px;
  176. }
  177. </style>