wd-calendar-view.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <view :class="`wd-calendar-view ${customClass}`">
  3. <year-panel
  4. v-if="type === 'month' || type === 'monthrange'"
  5. ref="yearPanelRef"
  6. :type="type"
  7. :value="modelValue"
  8. :min-date="minDate"
  9. :max-date="maxDate"
  10. :formatter="formatter"
  11. :max-range="maxRange"
  12. :range-prompt="rangePrompt"
  13. :allow-same-day="allowSameDay"
  14. :show-panel-title="showPanelTitle"
  15. :default-time="formatDefauleTime"
  16. :panel-height="panelHeight"
  17. @change="handleChange"
  18. />
  19. <month-panel
  20. v-else
  21. ref="monthPanelRef"
  22. :type="type"
  23. :value="modelValue"
  24. :min-date="minDate"
  25. :max-date="maxDate"
  26. :first-day-of-week="firstDayOfWeek"
  27. :formatter="formatter"
  28. :max-range="maxRange"
  29. :range-prompt="rangePrompt"
  30. :allow-same-day="allowSameDay"
  31. :show-panel-title="showPanelTitle"
  32. :default-time="formatDefauleTime"
  33. :panel-height="panelHeight"
  34. :immediate-change="immediateChange"
  35. :time-filter="timeFilter"
  36. :hide-second="hideSecond"
  37. @change="handleChange"
  38. @pickstart="handlePickStart"
  39. @pickend="handlePickEnd"
  40. />
  41. </view>
  42. </template>
  43. <script lang="ts">
  44. export default {
  45. name: 'wd-calendar-view',
  46. options: {
  47. addGlobalClass: true,
  48. virtualHost: true,
  49. styleIsolation: 'shared'
  50. }
  51. }
  52. </script>
  53. <script lang="ts" setup>
  54. import { ref, watch } from 'vue'
  55. import { getDefaultTime } from './utils'
  56. import yearPanel from './yearPanel/year-panel.vue'
  57. import MonthPanel from './monthPanel/month-panel.vue'
  58. import { calendarViewProps, type CalendarViewExpose } from './types'
  59. const props = defineProps(calendarViewProps)
  60. const emit = defineEmits(['change', 'update:modelValue', 'pickstart', 'pickend'])
  61. const formatDefauleTime = ref<number[][]>([])
  62. const yearPanelRef = ref()
  63. const monthPanelRef = ref()
  64. watch(
  65. () => props.defaultTime,
  66. (newValue) => {
  67. formatDefauleTime.value = getDefaultTime(newValue)
  68. },
  69. {
  70. deep: true,
  71. immediate: true
  72. }
  73. )
  74. /**
  75. * 使当前日期或者选中日期滚动到可视区域
  76. */
  77. function scrollIntoView() {
  78. const panel = getPanel()
  79. panel.scrollIntoView && panel.scrollIntoView()
  80. }
  81. function getPanel() {
  82. return props.type.indexOf('month') > -1 ? yearPanelRef.value : monthPanelRef.value
  83. }
  84. function handleChange({ value }: { value: number | number[] | null }) {
  85. emit('update:modelValue', value)
  86. emit('change', {
  87. value
  88. })
  89. }
  90. function handlePickStart() {
  91. emit('pickstart')
  92. }
  93. function handlePickEnd() {
  94. emit('pickend')
  95. }
  96. defineExpose<CalendarViewExpose>({
  97. scrollIntoView
  98. })
  99. </script>
  100. <style lang="scss" scoped>
  101. @import './index.scss';
  102. </style>