From bfcb62f691c45fb5baccbacda6cb53ccb5dcaea5 Mon Sep 17 00:00:00 2001 From: imfu Date: Tue, 30 Mar 2021 13:03:10 +0800 Subject: [PATCH] fix: fixed bugs #412 #416 #418 #419 --- docs/v2/guide.md | 21 ++++++---- src/component/v2/index.js | 32 ++++++++++++++ src/component/v2/index.wxml | 2 +- src/component/v2/plugins/preset/base.js | 3 +- src/component/v2/plugins/todo.js | 56 +++++++++---------------- src/component/v2/plugins/week.js | 2 +- src/pages/calendarV2/index.wxml | 5 ++- 7 files changed, 72 insertions(+), 49 deletions(-) diff --git a/docs/v2/guide.md b/docs/v2/guide.md index c642cc9..5dd69ef 100644 --- a/docs/v2/guide.md +++ b/docs/v2/guide.md @@ -6,7 +6,7 @@ title: 快速开始 将 `calendar` 文件夹拷贝至自己的组件目录,页面 `json` 文件中配置组件,组件路径根据项目实际情况填写 -``` json {3} +```json {3} { "usingComponents": { "calendar": "/component/calendar/index" @@ -26,7 +26,7 @@ title: 快速开始 另外日历组件提供一些自定义事件,其中自定义事件功能对应如下,返回参数的具体格式可运行 `demo` 中 `pages/calendarV2/index` 页面查看 -``` xml {2-6} +```xml {2-6} { year: 2019, month: 12, date: 3, ...} }, /** - * 当日历滑动时触发(适用于周/月视图) - * 可在滑动时按需在该方法内获取当前日历的一些数据 + * 当日历滑动时触发 */ onSwipe(e) { console.log('onSwipe', e.detail) }, + /** + * 当日历滑动时触发(适用于周视图) + * 可在滑动时按需在该方法内获取当前日历的一些数据 + */ + whenChangeWeek(e) { + console.log('whenChangeWeek', e.detail) + }, /** * 当改变月份时触发 * => current 当前年月 / next 切换后的年月 @@ -75,12 +81,11 @@ Page({ }) ``` - ## 自定义配置 组件支持一系列配置,自定义配置需手动传给组件,如: -``` xml {2} +```xml {2} = 2 && + typeof this.calendar.jump === 'function' + ) { + const today = dateUtil.todayFMD() + this.calendar.jump(today) + } + this.count = undefined + this.lastClick = undefined + } else { + this.lastClick = new Date().getTime() + } } } }) diff --git a/src/component/v2/index.wxml b/src/component/v2/index.wxml index e9ad6c7..7a6bd7f 100644 --- a/src/component/v2/index.wxml +++ b/src/component/v2/index.wxml @@ -8,7 +8,7 @@ - {{calendar.curYear || "--"}} 年 {{calendar.curMonth || "--"}} 月 + {{calendar.curYear || "--"}} 年 {{calendar.curMonth || "--"}} 月 diff --git a/src/component/v2/plugins/preset/base.js b/src/component/v2/plugins/preset/base.js index d367623..66ddec2 100644 --- a/src/component/v2/plugins/preset/base.js +++ b/src/component/v2/plugins/preset/base.js @@ -57,7 +57,8 @@ export default () => { if (selectedDates.length) { preSelectedDate = [...selectedDates].pop() || {} } - if (!inverse && +preSelectedDate.date === +tapedDate.date) { + const timeStr = dateUtil.toTimeStr + if (!inverse && timeStr(preSelectedDate) === timeStr(tapedDate)) { return calendar } let _tapedDate = { ...tapedDate, choosed: !tapedDate.choosed } diff --git a/src/component/v2/plugins/todo.js b/src/component/v2/plugins/todo.js index c7f8187..1559c6c 100644 --- a/src/component/v2/plugins/todo.js +++ b/src/component/v2/plugins/todo.js @@ -9,27 +9,6 @@ import { getCalendarData, dateUtil } from '../utils/index' import { renderCalendar } from '../render' -function filterTodos({ curYear, curMonth, exsitedTodos, toSetTodos }) { - const exsitedCurrentMonthTodos = dateUtil.filterDatesByYM( - { - year: curYear, - month: curMonth - }, - exsitedTodos - ) - const toSetTodosOfThisMonth = dateUtil.filterDatesByYM( - { - year: curYear, - month: curMonth - }, - toSetTodos - ) - const allTodosOfThisMonths = dateUtil.uniqueArrayByDate( - exsitedCurrentMonthTodos.concat(toSetTodosOfThisMonth) - ) - return allTodosOfThisMonths -} - function updateDatePropertyOfTodoLabel(todos, dates, showLabelAlways) { const datesInfo = [...dates] for (let todo of todos) { @@ -54,6 +33,21 @@ function updateDatePropertyOfTodoLabel(todos, dates, showLabelAlways) { export default () => { return { name: 'todo', + beforeRender(calendarData = {}, calendarConfig = {}, component) { + const { todos = [], dates = [], showLabelAlways } = calendarData + const dateWithTodoInfo = updateDatePropertyOfTodoLabel( + todos, + dates, + showLabelAlways + ) + return { + calendarData: { + ...calendarData, + dates: dateWithTodoInfo + }, + calendarConfig + } + }, methods(component) { return { setTodos: (options = {}) => { @@ -61,8 +55,6 @@ export default () => { if (!calendar || !calendar.dates) { return Promise.reject('请等待日历初始化完成后再调用该方法') } - let dates = [...calendar.dates] - const { curYear, curMonth } = calendar const { circle, dotColor = '', @@ -71,23 +63,13 @@ export default () => { dates: todoDates = [] } = options const { todos = [] } = calendar - const allTodosOfThisMonths = filterTodos({ - curYear, - curMonth, - exsitedTodos: todos, - toSetTodos: todoDates - }) - dates = updateDatePropertyOfTodoLabel( - allTodosOfThisMonths, - dates, - showLabelAlways + const tranformStr2NumOfTodo = todoDates.map(date => + dateUtil.tranformStr2NumOfDate(date) ) const calendarData = { - dates, + dates: calendar.dates, todos: dateUtil.uniqueArrayByDate( - todos.concat( - todoDates.map(date => dateUtil.tranformStr2NumOfDate(date)) - ) + todos.concat(tranformStr2NumOfTodo) ) } if (!circle) { diff --git a/src/component/v2/plugins/week.js b/src/component/v2/plugins/week.js index 3e15b34..673fb7e 100644 --- a/src/component/v2/plugins/week.js +++ b/src/component/v2/plugins/week.js @@ -113,7 +113,7 @@ function getDates(target, calendarDates = [], calendarConfig = {}) { const { firstDayOfWeek } = calendarConfig const firstDayOfWeekIsMon = firstDayOfWeek === 'Mon' if (firstDayOfWeekIsMon) { - const startIdx = date - targetDay + const startIdx = date - (targetDay || 7) return calendarDates.splice(startIdx, 7) } else { const startIdx = date - targetDay - 1 diff --git a/src/pages/calendarV2/index.wxml b/src/pages/calendarV2/index.wxml index 7fa3642..ce88fa8 100644 --- a/src/pages/calendarV2/index.wxml +++ b/src/pages/calendarV2/index.wxml @@ -3,9 +3,10 @@ id="calendar" config="{{calendarConfig}}" bind:onSwipe="onSwipe" + bind:whenChangeWeek="whenChangeWeek" + bind:whenChangeMonth="whenChangeMonth" bind:takeoverTap="takeoverTap" bind:afterTapDate="afterTapDate" - bind:whenChangeMonth="whenChangeMonth" bind:afterCalendarRender="afterCalendarRender" > @@ -23,4 +24,4 @@ {{item}} {{rstStr}} - \ No newline at end of file +