MonthCalendar.vue
3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<template>
<view class="month-calendar">
<view class="calendar-header">
<view class="arrow-box" @click="changeYear(-1)">
<uni-icons :style="{ opacity: canGoPrev ? 1 : 0 }" type="left" size="16" color="#414348"></uni-icons>
</view>
<view class="year-text">{{ displayYear }}</view>
<view class="arrow-box" @click="changeYear(1)">
<uni-icons :style="{ opacity: canGoNext ? 1 : 0 }" type="right" size="16" color="#414348"></uni-icons>
</view>
</view>
<view class="month-grid">
<view
class="month-item"
v-for="m in 12"
:key="m"
:class="{
'active': isSelected(m),
'disabled': isDisabled(m)
}"
@click="onMonthClick(m)"
>
{{ m }}月
</view>
</view>
</view>
</template>
<script>
import dayjs from 'dayjs';
export default {
name: 'MonthCalendar',
props: {
// 默认选中的日期,格式 YYYY-MM
defaultDate: {
type: String,
default: ''
}
},
data() {
// 1. 先计算出上个月的时间对象
const lastMonthDate = dayjs().subtract(1, 'month');
// 2. 分别获取
const yearNum = lastMonthDate.year();
const monthNum = lastMonthDate.month() + 1;
const todayYear = dayjs().year();
const todayMonth = dayjs().month() + 1;
const lastYear = lastMonthDate.year();
return {
today: { year: todayYear, month: todayMonth },
displayYear: lastYear,
selected: { year: yearNum, month: monthNum }
}
},
computed: {
canGoNext() {
if (dayjs().month() === 0) return false
return this.displayYear < this.today.year
},
canGoPrev() {
return this.displayYear > this.today.year - 1
}
},
watch: {
defaultDate: {
handler(val) {
if (val) {
const d = dayjs(val)
if (!d.isValid()) return
const y = d.year()
const m = d.month() + 1
this.selected = { year: y, month: m }
this.displayYear = y
}
},
immediate: true
}
},
methods: {
changeYear(step) {
const targetYear = this.displayYear + step
// 不允许切换到未来年份
if (step > 0 && targetYear > this.today.year) return
// 不允许切换到早于去年的年份
if (step < 0 && targetYear < this.today.year - 1) return
this.displayYear = targetYear
},
isSelected(m) {
return this.selected.year === this.displayYear && this.selected.month === m
},
isDisabled(m) {
// 未来年份的所有月份都不可选(虽然已经限制了年份切换,但作为兜底)
if (this.displayYear > this.today.year) return true
// 当前年份,超过当前月份的不可选
if (this.displayYear === this.today.year && m > this.today.month - 1) return true
return false
},
onMonthClick(m) {
if (this.isDisabled(m)) return
this.selected = {
year: this.displayYear,
month: m
}
const date = dayjs().year(this.displayYear).month(m - 1).date(1)
this.$emit('confirm', date.format('YYYY-MM'))
}
}
}
</script>
<style lang="scss" scoped>
.month-calendar {
padding: 16rpx 0;
}
.calendar-header {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 24rpx;
.year-text {
font-size: 28rpx;
color: #37393D;
margin: 0 60rpx;
}
.arrow-box {
padding: 10rpx;
display: flex;
align-items: center;
justify-content: center;
}
}
.month-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
row-gap: 32rpx;
column-gap: 30rpx;
}
.month-item {
height: 88rpx;
display: flex;
justify-content: center;
align-items: center;
font-size: 32rpx;
color: #37393D;
border-radius: 12rpx;
&.active {
background-color: #4673FF;
color: #fff;
}
&.disabled {
color: #AAAFBD;
background-color: transparent;
}
}
</style>