Skip to content

Commit

Permalink
Merge pull request #420 from treadpit/hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
todrfu authored Mar 30, 2021
2 parents 7d9864c + bfcb62f commit efcf47a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
21 changes: 14 additions & 7 deletions docs/v2/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ title: 快速开始

`calendar` 文件夹拷贝至自己的组件目录,页面 `json` 文件中配置组件,组件路径根据项目实际情况填写

``` json {3}
```json {3}
{
"usingComponents": {
"calendar": "/component/calendar/index"
Expand All @@ -26,7 +26,7 @@ title: 快速开始

另外日历组件提供一些自定义事件,其中自定义事件功能对应如下,返回参数的具体格式可运行 `demo``pages/calendarV2/index` 页面查看

``` xml {2-6}
```xml {2-6}
<calendar
bind:takeoverTap="takeoverTap"
bind:afterTapDate="afterTapDate"
Expand Down Expand Up @@ -58,12 +58,18 @@ Page({
console.log('afterTapDate', e.detail) // => { year: 2019, month: 12, date: 3, ...}
},
/**
* 当日历滑动时触发(适用于周/月视图)
* 可在滑动时按需在该方法内获取当前日历的一些数据
* 当日历滑动时触发
*/
onSwipe(e) {
console.log('onSwipe', e.detail)
},
/**
* 当日历滑动时触发(适用于周视图)
* 可在滑动时按需在该方法内获取当前日历的一些数据
*/
whenChangeWeek(e) {
console.log('whenChangeWeek', e.detail)
},
/**
* 当改变月份时触发
* => current 当前年月 / next 切换后的年月
Expand All @@ -75,12 +81,11 @@ Page({
})
```


## 自定义配置

组件支持一系列配置,自定义配置需手动传给组件,如:

``` xml {2}
```xml {2}
<calendar
config="{{calendarConfig}}"
bind:takeoverTap="takeoverTap"
Expand All @@ -102,6 +107,7 @@ const conf = {
showLunar: true, // 是否显示农历,此配置会导致 setTodoLabels 中 showLabelAlways 配置失效
inverse: true, // 单选模式下是否支持取消选中,
markToday: '', // 当天日期展示不使用默认数字,用特殊文字标记
hideHeader: true, // 隐藏日历头部操作栏
takeoverTap: true, // 是否完全接管日期点击事件(日期不会选中)
emphasisWeek: true, // 是否高亮显示周末日期
chooseAreaMode: true, // 开启日期范围选择模式,该模式下只可选择时间段
Expand All @@ -124,4 +130,5 @@ const conf = {
}
Page(conf)
```
为了赋予日历组件更多能力,小历同学2.0使用了简单的插件系统,比如需要使用设置代办事项相关功能,则必须手动引入todo插件才可使用相关功能,这点与1.x有很大区别,插件的使用请参考下一节 [引入插件](./plugin.md)

为了赋予日历组件更多能力,小历同学 2.0 使用了简单的插件系统,比如需要使用设置代办事项相关功能,则必须手动引入 todo 插件才可使用相关功能,这点与 1.x 有很大区别,插件的使用请参考下一节 [引入插件](./plugin.md)
32 changes: 32 additions & 0 deletions src/component/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,39 @@ Component({
},
next: target
})
this.triggerEvent('onSwipe', {
current: {
year: +curYear,
month: +curMonth
},
next: target,
type: triggerEventName
})
})
},
doubleClickJumpToToday() {
const { multi, weekMode } = this.calendar.getCalendarConfig() || {}
if (multi || weekMode) return
if (this.count === undefined) {
this.count = 1
} else {
this.count += 1
}
if (this.lastClick) {
const difference = new Date().getTime() - this.lastClick
if (
difference < 500 &&
this.count >= 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()
}
}
}
})
8 changes: 5 additions & 3 deletions src/component/v2/index.wxml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<view class="flex b tb ac" wx:if="{{calendar}}">
<view class="calendar b tb">
<!-- 头部操作栏 -->
<view class="handle {{config.theme}}_handle-color fs28 b lr ac pc">
<view
wx:if="{{!config.hideHeader}}"
class="handle {{config.theme}}_handle-color fs28 b lr ac pc">
<view class="prev fs36" wx:if="{{!config.weekMode}}">
<text class="prev-handle iconfont icon-doubleleft" bindtap="changeDate" data-type="prev_year"></text>
<text class="prev-handle iconfont icon-left" bindtap="changeDate" data-type="prev_month"></text>
</view>
<view class="flex date-in-handle b lr cc" bindtap="doubleClickToToday">{{calendar.curYear || "--"}} 年 {{calendar.curMonth || "--"}} 月</view>
<view class="flex date-in-handle b lr cc" bindtap="doubleClickJumpToToday">{{calendar.curYear || "--"}} 年 {{calendar.curMonth || "--"}} 月</view>
<view class="next fs36" wx:if="{{!config.weekMode}}">
<text class="next-handle iconfont icon-right" bindtap="changeDate" data-type="next_month"></text>
<text class="next-handle iconfont icon-doubleright" bindtap="changeDate" data-type="next_year"></text>
Expand Down Expand Up @@ -55,4 +57,4 @@
</view>
</view>
</view>
</view>
</view>
3 changes: 2 additions & 1 deletion src/component/v2/plugins/preset/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
56 changes: 19 additions & 37 deletions src/component/v2/plugins/todo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -54,15 +33,28 @@ 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 = {}) => {
const calendar = getCalendarData('calendar', component)
if (!calendar || !calendar.dates) {
return Promise.reject('请等待日历初始化完成后再调用该方法')
}
let dates = [...calendar.dates]
const { curYear, curMonth } = calendar
const {
circle,
dotColor = '',
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/component/v2/plugins/week.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/pages/calendarV2/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
></calendar>
</view>
Expand All @@ -23,4 +24,4 @@
<view class="show-rst-wrap">
<view wx:if="{{rst}}" wx:for="{{rst}}" wx:key="index" class="show-rst">{{item}}</view>
<view wx:if="{{rstStr}}" wx:key="index" class="show-rst">{{rstStr}}</view>
</view>
</view>

0 comments on commit efcf47a

Please sign in to comment.