index.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { ref, watch } from 'vue'
  2. import { ElCalendar, ElSelect, ElOption, ElDatePicker } from 'element-plus'
  3. import './index.less'
  4. import dayjs from 'dayjs'
  5. export default {
  6. setup (props, {slots}) {
  7. const calendar = ref()
  8. const globalDate = ref()
  9. const selectedDay = ref(0)
  10. const selectedMonth = ref(0)
  11. const selectedYear = ref()
  12. const monthOptions = [
  13. {
  14. value: 0,
  15. label: '1月',
  16. },
  17. {
  18. value: 1,
  19. label: '2月',
  20. },
  21. {
  22. value: 2,
  23. label: '3月',
  24. },
  25. {
  26. value: 3,
  27. label: '4月',
  28. },
  29. {
  30. value: 4,
  31. label: '5月',
  32. },
  33. {
  34. value: 5,
  35. label: '6月',
  36. },
  37. {
  38. value: 6,
  39. label: '7月',
  40. },
  41. {
  42. value: 7,
  43. label: '8月',
  44. },
  45. {
  46. value: 8,
  47. label: '9月',
  48. },
  49. {
  50. value: 9,
  51. label: '10月',
  52. },
  53. {
  54. value: 10,
  55. label: '11月',
  56. },
  57. {
  58. value: 11,
  59. label: '12月',
  60. }
  61. ]
  62. const handleDayClick = (date, type) => {
  63. selectedDay.value = type === 'current-month' ? date : 0
  64. }
  65. const handelYearSelect = (year) => {
  66. selectedYear.value = year
  67. const date = selectedYear.value.getFullYear() + '-' + (selectedMonth.value + 1)
  68. calendar.value.pickDay(dayjs(date))
  69. }
  70. const handelMonthSelect = (month) => {
  71. selectedMonth.value = month
  72. const date = selectedYear.value.getFullYear() + '-' + (selectedMonth.value + 1)
  73. calendar.value.pickDay(dayjs(date))
  74. }
  75. watch(globalDate, (globalDate, preGlobalDate) => {
  76. selectedYear.value = new Date(globalDate.substring(0, 4))
  77. selectedMonth.value = globalDate.substring(7, 9) - 1
  78. })
  79. return () => <ElCalendar class='cip-calendar' ref={calendar}>
  80. {{
  81. dateCell: ({data}) => {
  82. let dayClassName = ''
  83. const date = data.date.getDate()
  84. const day = data.date.getDay()
  85. if (data.type === 'current-month') {
  86. if (selectedDay.value === date) {
  87. if ([0, 6].includes(day)) {
  88. dayClassName = day === 6 ? 'current-month-week actived-sat' : 'current-month-week actived-sun'
  89. } else {
  90. dayClassName = 'current-month actived'
  91. }
  92. } else {
  93. dayClassName = [0, 6].includes(day) ? 'current-month-week' : 'current-month'
  94. }
  95. } else {
  96. dayClassName = 'other-month'
  97. }
  98. return <div class={dayClassName} onClick={handleDayClick.bind(null, date, data.type)}>
  99. <div class='day-title'>
  100. <div class='day-title__left'>{date}</div>
  101. {
  102. selectedDay.value === date && data.type === 'current-month' ? <div class='day-title__right'>{slots.titleRight?.()}</div> : ''
  103. }
  104. </div>
  105. <div class='day-detail'>
  106. {slots.dayDetail?.(date)}
  107. </div>
  108. </div>
  109. },
  110. header: ({date}) => {
  111. globalDate.value = date
  112. return <>
  113. {slots.headerLeft ? slots.headerLeft() : <div></div>}
  114. <div class='calendar__header--right'>
  115. <ElDatePicker
  116. class='year-select'
  117. type="year"
  118. modelValue={selectedYear.value}
  119. onUpdate:modelValue={(year) => handelYearSelect(year)}
  120. />
  121. <ElSelect class='month-select' modelValue={selectedMonth.value} onUpdate:modelValue={(month) => handelMonthSelect(month)}>
  122. {
  123. monthOptions.map(month => <ElOption
  124. key={month.value}
  125. label={month.label}
  126. value={month.value}
  127. />)
  128. }
  129. </ElSelect>
  130. </div>
  131. </>
  132. }
  133. }}
  134. </ElCalendar>
  135. }
  136. }