Vant Weapp Calendar 日历
介绍
日历组件用于选择日期或日期区间。
引入
在app.json或index.json中引入组件,详细介绍见快速上手。
"usingComponents": {
"van-calendar": "@vant/weapp/calendar/index"
}
代码演示
选择单个日期
下面演示了结合单元格来使用日历组件的用法,日期选择完成后会触发confirm事件。
<van-cell title="选择单个日期" value="{{ date }}" bind:click="onDisplay" />
<van-calendar show="{{ show }}" bind:close="onClose" bind:confirm="onConfirm" />
Page({
data: {
date: '',
show: false,
},
onDisplay() {
this.setData({ show: true });
},
onClose() {
this.setData({ show: false });
},
formatDate(date) {
date = new Date(date);
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(event) {
this.setData({
show: false,
date: this.formatDate(event.detail),
});
},
});
选择多个日期
设置type为multiple后可以选择多个日期,此时confirm事件返回的 date 为数组结构,数组包含若干个选中的日期。
<van-cell title="选择多个日期" value="{{ text }}" bind:click="onDisplay" />
<van-calendar
show="{{ show }}"
type="multiple"
bind:close="onClose"
bind:confirm="onConfirm"
/>
Page({
data: {
text: '',
show: false,
},
onDisplay() {
this.setData({ show: true });
},
onClose() {
this.setData({ show: false });
},
onConfirm(event) {
this.setData({
show: false,
text: `选择了 ${event.detail.length} 个日期`,
});
},
});
选择日期区间
设置type为range后可以选择日期区间,此时confirm事件返回的 date 为数组结构,数组第一项为开始时间,第二项为结束时间。
<van-cell title="选择日期区间" value="{{ date }}" bind:click="onDisplay" />
<van-calendar
show="{{ show }}"
type="range"
bind:close="onClose"
bind:confirm="onConfirm"
/>
Page({
data: {
date: '',
show: false,
},
onDisplay() {
this.setData({ show: true });
},
onClose() {
this.setData({ show: false });
},
formatDate(date) {
date = new Date(date);
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(event) {
const [start, end] = event.detail;
this.setData({
show: false,
date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
});
},
});
Tips: 默认情况下,日期区间的起止时间不能为同一天,可以通过设置 allow-same-day 属性来允许选择同一天。
快捷选择
将show-confirm设置为false可以隐藏确认按钮,这种情况下选择完成后会立即触发confirm事件。
<van-calendar show="{{ show }}" show-confirm="{{ false }}" />
自定义颜色
通过color属性可以自定义日历的颜色,对选中日期和底部按钮生效。
<van-calendar show="{{ show }}" color="#07c160" />
自定义日期范围
通过min-date和max-date定义日历的范围,需要注意的是min-date和max-date的区间不宜过大,否则会造成严重的性能问题。
<van-calendar
show="{{ show }}"
min-date="{{ minDate }}"
max-date="{{ maxDate }}"
/>
Page({
data: {
show: false,
minDate: new Date(2010, 0, 1).getTime(),
maxDate: new Date(2010, 0, 31).getTime(),
},
});
自定义按钮文字
通过confirm-text设置按钮文字,通过confirm-disabled-text设置按钮禁用时的文字。
<van-calendar
show="{{ show }}"
type="range"
confirm-text="完成"
confirm-disabled-text="请选择结束时间"
/>
自定义日期文案
通过传入formatter函数来对日历上每一格的内容进行格式化
<van-calendar show="{{ show }}" type="range" formatter="{{ formatter }}" />
Page({
data: {
formatter(day) {
const month = day.date.getMonth() + 1;
const date = day.date.getDate();
if (month === 5) {
if (date === 1) {
day.topInfo = '劳动节';
} else if (date === 4) {
day.topInfo = '五四青年节';
} else if (date === 11) {
day.text = '今天';
}
}
if (day.type === 'start') {
day.bottomInfo = '入住';
} else if (day.type === 'end') {
day.bottomInfo = '离店';
}
return day;
},
},
});
自定义弹出位置
通过position属性自定义弹出层的弹出位置,可选值为top、left、right。
<van-calendar show="{{ show }}" round="false" position="right" />
日期区间最大范围
选择日期区间时,可以通过max-range属性来指定最多可选天数,选择的范围超过最多可选天数时,会弹出相应的提示文案。
<van-calendar type="range" max-range="{{ 3 }}" />
自定义周起始日
通过 first-day-of-week 属性设置一周从哪天开始。
<van-calendar first-day-of-week="{{ 1 }}" />
平铺展示
将poppable设置为false,日历会直接展示在页面内,而不是以弹层的形式出现。
<van-calendar
title="日历"
poppable="{{ false }}"
show-confirm="{{ false }}"
class="calendar"
/>
.calendar {
--calendar-height: 618px;
}
Tips: 注意,在自定义calendar的高度时,需要确保,滚动到当前月份时,所有的日期要展现在.van-calendar__body内,否则可能会出现滚动时头部月份与当前月份不同步的情况。目前平铺型预设高度618px,弹窗型高度90%。
API
Props
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
type | 选择类型:
single 表示选择单个日期,
multiple 表示选择多个日期,
range 表示选择日期区间 |
string | single
|
title | 日历标题 | string | 日期选择
|
color | 主题色,对底部按钮和选中日期生效 | string | #ee0a24
|
min-date | 可选择的最小日期 | timestamp | 当前日期 |
max-date | 可选择的最大日期 | timestamp | 当前日期的六个月后 |
default-date v1.10.21
|
默认选中的日期,type 为multiple 或range 时为数组,传入 null 表示默认不选择 |
timestamp | timestamp[] | null | 今天 |
row-height | 日期行高 | number | string | 64
|
formatter | 日期格式化函数 | (day: Day) => Day | - |
poppable | 是否以弹层的形式展示日历 | boolean | true
|
show-mark | 是否显示月份背景水印 | boolean | true
|
show-title | 是否展示日历标题 | boolean | true
|
show-subtitle | 是否展示日历副标题(年月) | boolean | true
|
show-confirm | 是否展示确认按钮 | boolean | true
|
confirm-text | 确认按钮的文字 | string | 确定
|
confirm-disabled-text | 确认按钮处于禁用状态时的文字 | string | 确定
|
first-day-of-week | 设置周起始日 | 0~6 | 0
|
readonly v1.9.1
|
是否为只读状态,只读状态下不能选择日期 | boolean | false
|
root-portal v1.11.3
|
是否从页面子树中脱离出来,用于解决各种 fixed 失效问题,微信基础库 >= 2.25.2
|
boolean | false
|
Poppable Props
当 Calendar 的 poppable 为 true 时,支持以下 props:
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
show | 是否显示日历弹窗 | boolean | false
|
position | 弹出位置,可选值为 top right left
|
string | bottom
|
round | 是否显示圆角弹窗 | boolean | true
|
close-on-click-overlay | 是否在点击遮罩层后关闭 | boolean | true
|
safe-area-inset-bottom | 是否开启底部安全区适配 | boolean | true
|
Range Props
当 Calendar 的 type 为 range 时,支持以下 props:
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
max-range | 日期区间最多可选天数,默认无限制 | number | string | - |
range-prompt | 范围选择超过最多可选天数时的提示文案 | string | null | 选择天数不能超过 xx 天
|
show-range-prompt | 范围选择超过最多可选天数时,是否展示提示文案 | boolean | true
|
allow-same-day | 是否允许日期范围的起止时间为同一天 | boolean | false
|
Multiple Props
当 Calendar 的 type 为 multiple 时,支持以下 props:
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
min-range | 日期最少可选天数 | number | string | 1
|
Day 数据结构
日历中的每个日期都对应一个 Day 对象,通过formatter属性可以自定义 Day 对象的内容。
键名 | 说明 | 类型 |
---|---|---|
date | 日期对应的 Date 对象 | Date |
type | 日期类型,可选值为selected 、start 、middle 、end 、disabled
|
string |
text | 中间显示的文字 | string |
topInfo | 上方的提示信息 | string |
bottomInfo | 下方的提示信息 | string |
className | 自定义 className | string |
Events
事件名 | 说明 | 回调参数 |
---|---|---|
bind:select | 点击任意日期时触发 | value: Date | Date[] |
bind:unselect | 当 Calendar 的 type 为 multiple 时,点击已选中的日期时触发 |
value: Date |
bind:confirm | 日期选择完成后触发,若show-confirm 为true ,则点击确认按钮后触发 |
value: Date | Date[] |
bind:open | 打开弹出层时触发 | - |
bind:close | 关闭弹出层时触发 | - |
bind:opened | 打开弹出层且动画结束后触发 | - |
bind:closed | 关闭弹出层且动画结束后触发 | - |
bind:over-range | 范围选择超过最多可选天数时触发 | - |
bind:click-subtitle v1.8.1
|
点击日历副标题时触发 | WechatMiniprogram.TouchEvent |
Slots
名称 | 说明 |
---|---|
title | 自定义标题 |
footer | 自定义底部区域内容 |
方法
通过 selectComponent 可以获取到 Calendar 实例并调用实例方法。
方法名 | 说明 | 参数 | 返回值 |
---|---|---|---|
reset | 重置选中的日期到默认值 | - | - |
实例演示
更多建议: