index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <el-date-picker class="cip-form-picker"
  3. :model-value="transValue"
  4. @update:modelValue="emitDateInput"
  5. :format="formatter"
  6. :disabled="disabled"
  7. :type="type"
  8. :placeholder="placeholder"
  9. :clearable="clearable"
  10. :style="{width}"
  11. :disabled-date="disabledDate"
  12. ></el-date-picker>
  13. </template>
  14. <script>
  15. import { ElDatePicker } from 'element-plus'
  16. import { useFormInput } from '@cip/components/hooks/form-input'
  17. import { formInputProps } from '../../form-input-props'
  18. import { computed } from 'vue'
  19. import dayjs from 'dayjs'
  20. import advancedFormat from 'dayjs/plugin/advancedFormat'
  21. import weekYear from 'dayjs/plugin/weekYear'
  22. import weekOfYear from 'dayjs/plugin/weekOfYear'
  23. import { isArray, isNotEmpty, isNumber } from '@cip/utils/util'
  24. dayjs.extend(advancedFormat)
  25. dayjs.extend(weekYear)
  26. dayjs.extend(weekOfYear)
  27. export default {
  28. components: { ElDatePicker },
  29. props: formInputProps,
  30. inheritAttrs: false,
  31. emits: ['update:modelValue'],
  32. setup (props, context) {
  33. const { emitInput, ...formInput } = useFormInput(props, context)
  34. const disabled = computed(() => {
  35. return props.config?.disabled ?? false
  36. })
  37. const isTimestamp = computed(() => {
  38. return props.config?.isTimestamp ?? false
  39. })
  40. const transValue = computed({
  41. get () {
  42. if (!props.modelValue) return null
  43. if (isTimestamp.value) {
  44. return dayjs(props.modelValue).format(formatter.value)
  45. }
  46. if (type.value === 'dates') {
  47. return props.modelValue.split(',')
  48. }
  49. return dayjs(props.modelValue).format(formatter.value)
  50. }
  51. })
  52. const formatter = computed(() => {
  53. if (props.config?.formatter) return props.config?.formatter
  54. const typeToFormatter = {
  55. year: 'YYYY',
  56. month: 'YYYY-MM',
  57. week: 'YYYY 第 ww 周',
  58. date: 'YYYY-MM-DD',
  59. datetime: 'YYYY-MM-DD HH:mm:ss'
  60. }
  61. return typeToFormatter[type.value] ?? typeToFormatter.date
  62. })
  63. const type = computed(() => {
  64. const viewType = props.config?.viewType ?? 'date'
  65. return viewType.includes('range') ? 'date' : viewType
  66. })
  67. // 是否包含等于
  68. const isEqual = computed(() => {
  69. return props.config?.isEqual ?? false
  70. })
  71. // disable日期
  72. const disabledDate = computed(() => {
  73. return date => {
  74. if (props.config?.disabledDate) {
  75. return props.config?.disabledDate(date)
  76. }
  77. if (isNotEmpty(props.config?.max) || isNotEmpty(props.config?.min)) {
  78. return getDisabledConfig(date)
  79. }
  80. return undefined
  81. }
  82. })
  83. // disable时间转换
  84. const dateTransform = (date) => {
  85. if (isNumber(date)) return date
  86. if (!date?.includes(' ')) return new Date(date + ' 00:00:00').getTime()
  87. return new Date(date).getTime()
  88. }
  89. // min/max的disable实现
  90. const getDisabledConfig = (date) => {
  91. const maxDate = dateTransform(props.config?.max)
  92. const minDate = dateTransform(props.config?.min)
  93. const dateTime = date.getTime()
  94. if (isNotEmpty(props.config?.max) && isNotEmpty(props.config?.min)) {
  95. if (isEqual.value) return (dateTime >= maxDate) || (dateTime <= minDate)
  96. return (dateTime > maxDate) || (dateTime < minDate)
  97. }
  98. if (isNotEmpty(props.config?.max)) {
  99. if (isEqual.value) return dateTime >= maxDate
  100. return dateTime > maxDate
  101. }
  102. if (isNotEmpty(props.config?.min)) {
  103. if (isEqual.value) return dateTime <= minDate
  104. return dateTime < minDate
  105. }
  106. }
  107. /**
  108. * element plus的date-picker组件不再提供value-format属性
  109. * 提交val时候引入dayjs的format方法手动转换
  110. */
  111. const emitDateInput = (val) => {
  112. emitInput(typeToValue(val))
  113. }
  114. // 不同事件类型传值不同,做转换
  115. const typeToValueMap = {
  116. dates: (val) => {
  117. if (!isArray(val)) val = [val]
  118. return val?.map(i => dayjs(i).format(formatter.value)).join(',')
  119. },
  120. datetime: val => dayjs(val).format('YYYY-MM-DD HH:mm:ss'),
  121. week: val => dayjs(val).format('YYYY-MM-DD')
  122. }
  123. const typeToValue = (val) => {
  124. if (!val) return ''
  125. if (isTimestamp.value) return new Date(val).getTime()
  126. return typeToValueMap?.[type.value]?.(val) ?? dayjs(val).format(formatter.value)
  127. }
  128. return {
  129. ...formInput,
  130. transValue,
  131. disabled,
  132. formatter,
  133. emitDateInput,
  134. type,
  135. disabledDate
  136. }
  137. }
  138. }
  139. </script>
  140. <style lang="less">
  141. .el-input.el-date-editor.cip-form-picker{
  142. width: 100%;
  143. }
  144. </style>