fs-week-bar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <view class="fs-week-bar">
  3. <view class="fs-week-arrow" @click="handleChange('left')">
  4. <fs-icon type="icon-d-down" rotate="90" size="28rpx" :color="textColor"></fs-icon>
  5. </view>
  6. <view class="fs-week-list">
  7. <view
  8. class="fs-week-item"
  9. :class="{'fs-week-item-active': state.activeDate === item.fullDate}"
  10. v-for="(item, index) in state.week"
  11. :key="index"
  12. @click="handleClick(item)"
  13. >
  14. <view class="fs-week-item-hd">周{{item.day}}</view>
  15. <view class="fs-week-item-bd">{{item.date}}</view>
  16. </view>
  17. </view>
  18. <view class="fs-week-arrow" @click="handleChange('right')">
  19. <fs-icon type="icon-d-down" rotate="-90" size="28rpx" :color="textColor"></fs-icon>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. export default {
  25. name: "fs-week-bar"
  26. }
  27. </script>
  28. <script setup>
  29. import { ref, reactive, watch } from 'vue'
  30. import dayjs from 'dayjs'
  31. const props = defineProps({
  32. bgColor: {
  33. type: String,
  34. default: '#fff'
  35. },
  36. textColor: {
  37. type: String,
  38. default: '#666666'
  39. },
  40. activeColor: {
  41. type: String,
  42. default: '#165DFF'
  43. }
  44. })
  45. const emits = defineEmits(['change', 'click'])
  46. const state = reactive({
  47. week: [],
  48. radix: 0,
  49. activeDate: dayjs().format('YYYY-MM-DD')
  50. })
  51. const curDay = dayjs().day()
  52. const dayMap = {
  53. 1: '一',
  54. 2: '二',
  55. 3: '三',
  56. 4: '四',
  57. 5: '五',
  58. 6: '六',
  59. 7: '日',
  60. }
  61. const initWeek = () => {
  62. state.week = []
  63. for (let i = 1; i < curDay; i++) {
  64. const diffDay = curDay - i + 7 * state.radix
  65. state.week.push({
  66. day: dayMap[i],
  67. date: dayjs().subtract(diffDay, 'day').format('MM-DD'),
  68. fullDate: dayjs().subtract(diffDay, 'day').format('YYYY-MM-DD'),
  69. })
  70. }
  71. for (let i = curDay; i <= 7; i++) {
  72. const diffDay = i - curDay + 7 * state.radix
  73. state.week.push({
  74. day: dayMap[i],
  75. date: dayjs().add(diffDay, 'day').format('MM-DD'),
  76. fullDate: dayjs().add(diffDay, 'day').format('YYYY-MM-DD'),
  77. })
  78. }
  79. }
  80. const handleChange = type => {
  81. type === 'left' ? state.radix-- : state.radix++
  82. }
  83. watch(() => state.radix, (val) => {
  84. initWeek()
  85. emits('change', state.week)
  86. }, { immediate: true })
  87. const handleClick= item => {
  88. state.activeDate = item.fullDate
  89. emits('click', state.activeDate)
  90. }
  91. </script>
  92. <style lang="scss" scoped>
  93. .fs-week{
  94. &-bar{
  95. display: flex;
  96. align-items: center;
  97. background-color: v-bind(bgColor);
  98. padding: 20rpx 0;
  99. }
  100. &-arrow{
  101. padding: 0 10rpx;
  102. }
  103. &-list{
  104. display: flex;
  105. flex: 1;
  106. }
  107. &-item{
  108. text-align: center;
  109. flex: 1;
  110. color: v-bind(textColor);
  111. &-active{
  112. color: v-bind(activeColor);
  113. }
  114. &-hd{
  115. font-size: 15px;
  116. font-weight: bold;
  117. }
  118. &-bd{
  119. font-size: 13px;
  120. }
  121. }
  122. }
  123. </style>