Showing
2 changed files
with
62 additions
and
65 deletions
| @@ -6,17 +6,18 @@ const TAG = "RMCalendar" | @@ -6,17 +6,18 @@ const TAG = "RMCalendar" | ||
| 6 | @Component | 6 | @Component |
| 7 | export struct RMCalendar { | 7 | export struct RMCalendar { |
| 8 | @State selectItem: RMCalendarBean = new RMCalendarBean() | 8 | @State selectItem: RMCalendarBean = new RMCalendarBean() |
| 9 | - private today: Date = new Date() // 当天 | 9 | + //选中的日期 |
| 10 | + private selectDay: Date = new Date() | ||
| 10 | // 开始日期 | 11 | // 开始日期 |
| 11 | startDate: Date = new Date() | 12 | startDate: Date = new Date() |
| 12 | // 截止日期 | 13 | // 截止日期 |
| 13 | endDate: Date = new Date() | 14 | endDate: Date = new Date() |
| 14 | //当前日期-当前显示的月份的第一天 | 15 | //当前日期-当前显示的月份的第一天 |
| 15 | - private startDay: Date = new Date( | ||
| 16 | - this.today.getFullYear(), | ||
| 17 | - this.today.getMonth(), | ||
| 18 | - 1 | ||
| 19 | - ) | 16 | + // private startDay: Date = new Date( |
| 17 | + // this.selectDay.getFullYear(), | ||
| 18 | + // this.selectDay.getMonth(), | ||
| 19 | + // 1 | ||
| 20 | + // ) | ||
| 20 | // 是否有上一个月 | 21 | // 是否有上一个月 |
| 21 | @State private hasPre: boolean = true | 22 | @State private hasPre: boolean = true |
| 22 | // 是否有下一个月 | 23 | // 是否有下一个月 |
| @@ -42,12 +43,12 @@ export struct RMCalendar { | @@ -42,12 +43,12 @@ export struct RMCalendar { | ||
| 42 | itemFontColor: ResourceColor = "#333333" | 43 | itemFontColor: ResourceColor = "#333333" |
| 43 | itemFontWeight: FontWeight = FontWeight.Bold | 44 | itemFontWeight: FontWeight = FontWeight.Bold |
| 44 | // 今日字体颜色 | 45 | // 今日字体颜色 |
| 45 | - todayFontColor: ResourceColor = "#ED2800" | 46 | + selectDayFontColor: ResourceColor = "#ED2800" |
| 46 | // 不能使用的日期字体颜色 | 47 | // 不能使用的日期字体颜色 |
| 47 | disabledFontColor: ResourceColor = "#CCCCCC" | 48 | disabledFontColor: ResourceColor = "#CCCCCC" |
| 48 | // 选中日期字体颜色 | 49 | // 选中日期字体颜色 |
| 49 | selectFontColor: ResourceColor = "#FFFFFF" | 50 | selectFontColor: ResourceColor = "#FFFFFF" |
| 50 | - // 选中日期背景颜色, 默认与todayFontColor一致 | 51 | + // 选中日期背景颜色, 默认与selectDayFontColor一致 |
| 51 | selectItemBgColor: ResourceColor = "#ED2800" | 52 | selectItemBgColor: ResourceColor = "#ED2800" |
| 52 | @State private title: string = '' | 53 | @State private title: string = '' |
| 53 | // 计算的总加载 | 54 | // 计算的总加载 |
| @@ -57,7 +58,7 @@ export struct RMCalendar { | @@ -57,7 +58,7 @@ export struct RMCalendar { | ||
| 57 | // 自定义每一项布局 | 58 | // 自定义每一项布局 |
| 58 | public cellLayout?: (item: RMCalendarBean) => void | 59 | public cellLayout?: (item: RMCalendarBean) => void |
| 59 | // 仅自定义 今日 样式,当使用cellLayout时,tadayLayout无效 | 60 | // 仅自定义 今日 样式,当使用cellLayout时,tadayLayout无效 |
| 60 | - todayLayout?: (item: RMCalendarBean) => void | 61 | + selectDayLayout?: (item: RMCalendarBean) => void |
| 61 | // 计算item时,如需添加更多自定义属性时使用 | 62 | // 计算item时,如需添加更多自定义属性时使用 |
| 62 | reBuildDateItem?: (item: RMCalendarBean) => RMCalendarBean | 63 | reBuildDateItem?: (item: RMCalendarBean) => RMCalendarBean |
| 63 | // 选择变化监听, | 64 | // 选择变化监听, |
| @@ -84,10 +85,10 @@ export struct RMCalendar { | @@ -84,10 +85,10 @@ export struct RMCalendar { | ||
| 84 | cellLayout: this.cellLayout, | 85 | cellLayout: this.cellLayout, |
| 85 | itemFontSize: this.itemFontSize, | 86 | itemFontSize: this.itemFontSize, |
| 86 | itemFontColor: this.itemFontColor, | 87 | itemFontColor: this.itemFontColor, |
| 87 | - today: this.today.getTime(), | 88 | + selectDay: this.selectDay.getTime(), |
| 88 | itemFontWeight: this.itemFontWeight, | 89 | itemFontWeight: this.itemFontWeight, |
| 89 | - todayFontColor: this.todayFontColor, | ||
| 90 | - todayLayout: this.todayLayout, | 90 | + selectDayFontColor: this.selectDayFontColor, |
| 91 | + selectDayLayout: this.selectDayLayout, | ||
| 91 | selectItem: $selectItem, | 92 | selectItem: $selectItem, |
| 92 | selectFontColor: this.selectFontColor, | 93 | selectFontColor: this.selectFontColor, |
| 93 | selectItemBgColor: this.selectItemBgColor, | 94 | selectItemBgColor: this.selectItemBgColor, |
| @@ -123,12 +124,12 @@ export struct RMCalendar { | @@ -123,12 +124,12 @@ export struct RMCalendar { | ||
| 123 | */ | 124 | */ |
| 124 | initAttr() { | 125 | initAttr() { |
| 125 | if (!this.selectItemBgColor) { | 126 | if (!this.selectItemBgColor) { |
| 126 | - this.selectItemBgColor = this.todayFontColor | 127 | + this.selectItemBgColor = this.selectDayFontColor |
| 127 | } | 128 | } |
| 128 | - this.today = new Date( | ||
| 129 | - this.today.getFullYear(), | ||
| 130 | - this.today.getMonth(), | ||
| 131 | - this.today.getDate(), | 129 | + this.selectDay = new Date( |
| 130 | + this.selectDay.getFullYear(), | ||
| 131 | + this.selectDay.getMonth(), | ||
| 132 | + this.selectDay.getDate(), | ||
| 132 | ) | 133 | ) |
| 133 | 134 | ||
| 134 | // 开始日期 | 135 | // 开始日期 |
| @@ -137,24 +138,24 @@ export struct RMCalendar { | @@ -137,24 +138,24 @@ export struct RMCalendar { | ||
| 137 | } | 138 | } |
| 138 | // 截止日期 | 139 | // 截止日期 |
| 139 | if (!this.endDate) { | 140 | if (!this.endDate) { |
| 140 | - this.endDate = new Date(this.today.getFullYear() + 10, 11, 31) | 141 | + this.endDate = new Date(this.selectDay.getFullYear() + 10, 11, 31) |
| 141 | } | 142 | } |
| 142 | 143 | ||
| 143 | - if (this.today.getTime() < this.startDate.getTime()) { | ||
| 144 | - this.startDay.setTime(this.startDate.getTime()) | ||
| 145 | - } else if (this.today.getTime() > this.endDate.getTime()) { | ||
| 146 | - this.startDay.setTime(this.endDate.getTime()) | 144 | + if (this.selectDay.getTime() < this.startDate.getTime()) { |
| 145 | + this.selectDay.setTime(this.startDate.getTime()) | ||
| 146 | + } else if (this.selectDay.getTime() > this.endDate.getTime()) { | ||
| 147 | + this.selectDay.setTime(this.endDate.getTime()) | ||
| 147 | } else { | 148 | } else { |
| 148 | - this.startDay.setTime(this.today.getTime()) | 149 | + this.selectDay.setTime(this.selectDay.getTime()) |
| 149 | } | 150 | } |
| 150 | } | 151 | } |
| 151 | 152 | ||
| 152 | aboutToAppear() { | 153 | aboutToAppear() { |
| 153 | this.initAttr() | 154 | this.initAttr() |
| 154 | let temp = new RMCalendarBean() | 155 | let temp = new RMCalendarBean() |
| 155 | - temp.time = this.today.getTime() | 156 | + temp.time = this.selectDay.getTime() |
| 156 | this.selectItem = temp | 157 | this.selectItem = temp |
| 157 | - this.calcDatas() | 158 | + this.calcData() |
| 158 | } | 159 | } |
| 159 | 160 | ||
| 160 | /** | 161 | /** |
| @@ -163,12 +164,12 @@ export struct RMCalendar { | @@ -163,12 +164,12 @@ export struct RMCalendar { | ||
| 163 | private nextMonth() { | 164 | private nextMonth() { |
| 164 | // this.dates.slice(0, this.dates.length) | 165 | // this.dates.slice(0, this.dates.length) |
| 165 | this.dates = [] | 166 | this.dates = [] |
| 166 | - const beforDate = new Date(this.startDay.getFullYear(), this.startDay.getMonth()) | ||
| 167 | - this.startDay.setMonth(this.startDay.getMonth() + 1) | 167 | + const beforDate = new Date(this.selectDay.getFullYear(), this.selectDay.getMonth()) |
| 168 | + this.selectDay.setMonth(this.selectDay.getMonth() + 1) | ||
| 168 | if (this.onMonthChange) { | 169 | if (this.onMonthChange) { |
| 169 | - this.onMonthChange(new Date(this.startDay.getFullYear(), this.startDay.getMonth()), beforDate) | 170 | + this.onMonthChange(new Date(this.selectDay.getFullYear(), this.selectDay.getMonth()), beforDate) |
| 170 | } | 171 | } |
| 171 | - this.calcDatas() | 172 | + this.calcData() |
| 172 | } | 173 | } |
| 173 | 174 | ||
| 174 | /** | 175 | /** |
| @@ -177,60 +178,57 @@ export struct RMCalendar { | @@ -177,60 +178,57 @@ export struct RMCalendar { | ||
| 177 | private preMonth() { | 178 | private preMonth() { |
| 178 | // this.dates.slice(0, this.dates.length) | 179 | // this.dates.slice(0, this.dates.length) |
| 179 | this.dates = [] | 180 | this.dates = [] |
| 180 | - const beforDate = new Date(this.startDay.getFullYear(), this.startDay.getMonth()) | ||
| 181 | - this.startDay.setMonth(this.startDay.getMonth() - 1) | 181 | + const beforDate = new Date(this.selectDay.getFullYear(), this.selectDay.getMonth()) |
| 182 | + this.selectDay.setMonth(this.selectDay.getMonth() - 1) | ||
| 182 | if (this.onMonthChange) { | 183 | if (this.onMonthChange) { |
| 183 | - this.onMonthChange(new Date(this.startDay.getFullYear(), this.startDay.getMonth()), beforDate) | 184 | + this.onMonthChange(new Date(this.selectDay.getFullYear(), this.selectDay.getMonth()), beforDate) |
| 184 | } | 185 | } |
| 185 | - this.calcDatas() | 186 | + this.calcData() |
| 186 | } | 187 | } |
| 187 | 188 | ||
| 188 | /** | 189 | /** |
| 189 | * 具体计算 | 190 | * 具体计算 |
| 190 | */ | 191 | */ |
| 191 | - private calcDatas() { | ||
| 192 | - const startDay = this.startDay | ||
| 193 | - this.title = `${startDay.getFullYear()}年${startDay.getMonth() + 1}月` | ||
| 194 | - startDay.setDate(1) | 192 | + private calcData() { |
| 195 | 193 | ||
| 196 | - if (startDay.getFullYear() < this.startDate.getFullYear() | ||
| 197 | - || (startDay.getFullYear() == this.startDate.getFullYear() && startDay.getMonth() <= this.startDate.getMonth())) { | 194 | + this.title = `${this.selectDay.getFullYear()}年${this.selectDay.getMonth() + 1}月` |
| 195 | + this.selectDay.setDate(1) | ||
| 196 | + | ||
| 197 | + if (this.selectDay.getFullYear() < this.startDate.getFullYear() | ||
| 198 | + || (this.selectDay.getFullYear() == this.startDate.getFullYear() && this.selectDay.getMonth() <= this.startDate.getMonth())) { | ||
| 198 | this.hasPre = false | 199 | this.hasPre = false |
| 199 | } else { | 200 | } else { |
| 200 | this.hasPre = true | 201 | this.hasPre = true |
| 201 | } | 202 | } |
| 202 | 203 | ||
| 203 | - if (startDay.getFullYear() > this.endDate.getFullYear() | ||
| 204 | - || (startDay.getFullYear() == this.endDate.getFullYear() && startDay.getMonth() >= this.endDate.getMonth())) { | 204 | + if (this.selectDay.getFullYear() > this.endDate.getFullYear() |
| 205 | + || (this.selectDay.getFullYear() == this.endDate.getFullYear() && this.selectDay.getMonth() >= this.endDate.getMonth())) { | ||
| 205 | this.hasNext = false | 206 | this.hasNext = false |
| 206 | } else { | 207 | } else { |
| 207 | this.hasNext = true | 208 | this.hasNext = true |
| 208 | } | 209 | } |
| 209 | - | ||
| 210 | - // 计算第一个月 | ||
| 211 | - // 获取第一个月总天数 | ||
| 212 | - let endDay: Date = new Date( | ||
| 213 | - startDay.getFullYear(), | ||
| 214 | - startDay.getMonth() + 1, | 210 | + // 创建一个整月的日期,获取一个月总天数 |
| 211 | + let selectDate: Date = new Date( | ||
| 212 | + this.selectDay.getFullYear(), | ||
| 213 | + this.selectDay.getMonth() + 1, | ||
| 215 | 0, 23, 59, 59) | 214 | 0, 23, 59, 59) |
| 216 | 215 | ||
| 217 | let tempDate: Date = new Date( | 216 | let tempDate: Date = new Date( |
| 218 | - startDay.getFullYear(), | ||
| 219 | - startDay.getMonth(), | ||
| 220 | - startDay.getDate() | 217 | + this.selectDay.getFullYear(), |
| 218 | + this.selectDay.getMonth(), | ||
| 219 | + this.selectDay.getDate() | ||
| 221 | ) | 220 | ) |
| 221 | + //获取当月的总天数 | ||
| 222 | + const count = selectDate.getDate() | ||
| 223 | + //当前日期是周几 | ||
| 224 | + const preCount = this.selectDay.getDay() | ||
| 225 | + const totalCount = count + preCount | ||
| 222 | 226 | ||
| 223 | - const count = endDay.getDate() | ||
| 224 | - const preCount = startDay.getDay() | ||
| 225 | - // const nextCount = 6 - endDay.getDay() | ||
| 226 | - const nextCount = 0 | ||
| 227 | - const finilCount = count + preCount + nextCount | ||
| 228 | - | ||
| 229 | - // 补齐上一个月 | ||
| 230 | - tempDate.setDate(tempDate.getDate() - preCount) | 227 | + // 补齐上一个月差的天数,需要在当月展示的部分,下面计算日期循环加1天 |
| 228 | + tempDate.setDate(this.selectDay.getDate() - preCount) | ||
| 231 | 229 | ||
| 232 | - // 添加日期 | ||
| 233 | - for (let index = 0; index < finilCount; index++) { | 230 | + // 添加当月需要展示的日期 |
| 231 | + for (let index = 0; index < totalCount; index++) { | ||
| 234 | let item = new RMCalendarBean( | 232 | let item = new RMCalendarBean( |
| 235 | tempDate.getFullYear(), | 233 | tempDate.getFullYear(), |
| 236 | tempDate.getMonth(), | 234 | tempDate.getMonth(), |
| @@ -7,12 +7,12 @@ export struct RMCalenderCell { | @@ -7,12 +7,12 @@ export struct RMCalenderCell { | ||
| 7 | itemFontSize: Length = 0 | 7 | itemFontSize: Length = 0 |
| 8 | itemFontColor: ResourceColor = {} as ResourceColor | 8 | itemFontColor: ResourceColor = {} as ResourceColor |
| 9 | itemFontWeight: FontWeight = FontWeight.Normal | 9 | itemFontWeight: FontWeight = FontWeight.Normal |
| 10 | - todayFontColor: ResourceColor = {} as ResourceColor | 10 | + selectDayFontColor: ResourceColor = {} as ResourceColor |
| 11 | selectFontColor: ResourceColor = {} as ResourceColor | 11 | selectFontColor: ResourceColor = {} as ResourceColor |
| 12 | selectItemBgColor: ResourceColor = {} as ResourceColor | 12 | selectItemBgColor: ResourceColor = {} as ResourceColor |
| 13 | disabledFontColor: ResourceColor = {} as ResourceColor | 13 | disabledFontColor: ResourceColor = {} as ResourceColor |
| 14 | // 今日时间戳 | 14 | // 今日时间戳 |
| 15 | - today: number = 0 | 15 | + selectDay: number = 0 |
| 16 | @Link selectItem: RMCalendarBean | 16 | @Link selectItem: RMCalendarBean |
| 17 | @Link selectedDates: Array<RMCalendarBean> | 17 | @Link selectedDates: Array<RMCalendarBean> |
| 18 | @Prop hasPre: boolean | 18 | @Prop hasPre: boolean |
| @@ -20,7 +20,7 @@ export struct RMCalenderCell { | @@ -20,7 +20,7 @@ export struct RMCalenderCell { | ||
| 20 | @ObjectLink item: RMCalendarBean | 20 | @ObjectLink item: RMCalendarBean |
| 21 | // 自定义每一项布局 | 21 | // 自定义每一项布局 |
| 22 | cellLayout?: (item: RMCalendarBean) => void | 22 | cellLayout?: (item: RMCalendarBean) => void |
| 23 | - todayLayout?: (item: RMCalendarBean) => void | 23 | + selectDayLayout?: (item: RMCalendarBean) => void |
| 24 | cellClick?: (item: RMCalendarBean) => void | 24 | cellClick?: (item: RMCalendarBean) => void |
| 25 | disableClick?: (item: RMCalendarBean) => void | 25 | disableClick?: (item: RMCalendarBean) => void |
| 26 | 26 | ||
| @@ -43,8 +43,6 @@ export struct RMCalenderCell { | @@ -43,8 +43,6 @@ export struct RMCalenderCell { | ||
| 43 | return this.disabledFontColor | 43 | return this.disabledFontColor |
| 44 | } else if (this.selectItem && this.selectItem.time == this.item.time) { | 44 | } else if (this.selectItem && this.selectItem.time == this.item.time) { |
| 45 | return this.selectFontColor | 45 | return this.selectFontColor |
| 46 | - } else if (this.item.time == this.today) { | ||
| 47 | - return this.todayFontColor | ||
| 48 | } | 46 | } |
| 49 | return this.itemFontColor | 47 | return this.itemFontColor |
| 50 | } | 48 | } |
| @@ -140,4 +138,5 @@ export struct RMCalenderCell { | @@ -140,4 +138,5 @@ export struct RMCalenderCell { | ||
| 140 | } | 138 | } |
| 141 | }) | 139 | }) |
| 142 | } | 140 | } |
| 141 | + | ||
| 143 | } | 142 | } |
-
Please register or login to post a comment