year-panel.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="wd-year-panel">
  3. <view v-if="showPanelTitle" class="wd-year-panel__title">{{ title }}</view>
  4. <scroll-view class="wd-year-panel__container" :style="`height: ${scrollHeight}px`" scroll-y @scroll="yearScroll" :scroll-top="scrollTop">
  5. <view v-for="(item, index) in years" :key="index" :id="`year${index}`">
  6. <year
  7. :type="type"
  8. :date="item.date"
  9. :value="value"
  10. :min-date="minDate"
  11. :max-date="maxDate"
  12. :max-range="maxRange"
  13. :formatter="formatter"
  14. :range-prompt="rangePrompt"
  15. :allow-same-day="allowSameDay"
  16. :default-time="defaultTime"
  17. :showTitle="index !== 0"
  18. @change="handleDateChange"
  19. />
  20. </view>
  21. </scroll-view>
  22. </view>
  23. </template>
  24. <script lang="ts">
  25. export default {
  26. options: {
  27. addGlobalClass: true,
  28. virtualHost: true,
  29. styleIsolation: 'shared'
  30. }
  31. }
  32. </script>
  33. <script lang="ts" setup>
  34. import { computed, ref, onMounted } from 'vue'
  35. import { compareYear, formatYearTitle, getYears } from '../utils'
  36. import { isArray, isNumber, pause } from '../../common/util'
  37. import Year from '../year/year.vue'
  38. import { yearPanelProps, type YearInfo, type YearPanelExpose } from './types'
  39. const props = defineProps(yearPanelProps)
  40. const emit = defineEmits(['change'])
  41. const scrollTop = ref<number>(0) // 滚动位置
  42. const scrollIndex = ref<number>(0) // 当前显示的年份索引
  43. // 滚动区域的高度
  44. const scrollHeight = computed(() => {
  45. const scrollHeight: number = props.panelHeight + (props.showPanelTitle ? 26 : 16)
  46. return scrollHeight
  47. })
  48. // 年份信息
  49. const years = computed<YearInfo[]>(() => {
  50. return getYears(props.minDate, props.maxDate).map((year, index) => {
  51. return {
  52. date: year,
  53. height: index === 0 ? 200 : 245
  54. }
  55. })
  56. })
  57. // 标题
  58. const title = computed(() => {
  59. return formatYearTitle(years.value[scrollIndex.value].date)
  60. })
  61. onMounted(() => {
  62. scrollIntoView()
  63. })
  64. async function scrollIntoView() {
  65. await pause()
  66. let activeDate: number | null = null
  67. if (isArray(props.value)) {
  68. activeDate = props.value![0]
  69. } else if (isNumber(props.value)) {
  70. activeDate = props.value
  71. }
  72. if (!activeDate) {
  73. activeDate = Date.now()
  74. }
  75. let top: number = 0
  76. for (let index = 0; index < years.value.length; index++) {
  77. if (compareYear(years.value[index].date, activeDate) === 0) {
  78. break
  79. }
  80. top += years.value[index] ? Number(years.value[index].height) : 0
  81. }
  82. scrollTop.value = 0
  83. if (top > 0) {
  84. await pause()
  85. scrollTop.value = top + 45
  86. }
  87. }
  88. const yearScroll = (event: { detail: { scrollTop: number } }) => {
  89. if (years.value.length <= 1) {
  90. return
  91. }
  92. const scrollTop = Math.max(0, event.detail.scrollTop)
  93. doSetSubtitle(scrollTop)
  94. }
  95. /**
  96. * 设置小标题
  97. * scrollTop 滚动条位置
  98. */
  99. function doSetSubtitle(scrollTop: number) {
  100. let height: number = 0 // 月份高度和
  101. for (let index = 0; index < years.value.length; index++) {
  102. height = height + years.value[index].height
  103. if (scrollTop < height) {
  104. scrollIndex.value = index
  105. return
  106. }
  107. }
  108. }
  109. function handleDateChange({ value }: { value: number[] }) {
  110. emit('change', {
  111. value
  112. })
  113. }
  114. defineExpose<YearPanelExpose>({
  115. scrollIntoView
  116. })
  117. </script>
  118. <style lang="scss" scoped>
  119. @import './index.scss';
  120. </style>