# Calendar 日历
日历组件用于选择日期或日期区间。
# 引入
import PressCalendar from 'press-ui/press-calendar/press-calendar.vue';
export default {
components: {
PressCalendar,
}
}
# 代码演示
# 选择单个日期
下面演示了结合单元格来使用日历组件的用法,日期选择完成后会触发confirm事件。
<press-cell title="选择单个日期" :value="date" @click="onDisplay" />
<press-calendar :show="show" @close="onClose" @confirm="onConfirm" />
export default {
data() {
return {
date: '',
show: false,
}
},
methods: {
onDisplay() {
this.show = true;
},
onClose() {
this.show = false;
},
formatDate(date) {
date = new Date(date);
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(detail) {
this.show = false;
this.date = this.formatDate(detail);
},
}
};
# 选择多个日期
设置type为multiple后可以选择多个日期,此时confirm事件返回的 date 为数组结构,数组包含若干个选中的日期。
<press-cell title="选择多个日期" :value="text" @click="onDisplay" />
<press-calendar
:show="show"
type="multiple"
@close="onClose"
@confirm="onConfirm"
/>
export default {
data() {
return {
text: '',
show: false,
}
},
methods: {
onDisplay() {
this.show = true;
},
onClose() {
this.show = false;
},
onConfirm(detail) {
this.show = false;
this.date = `选择了 ${detail.length} 个日期`;
},
}
};
# 选择日期区间
设置type为range后可以选择日期区间,此时confirm事件返回的 date 为数组结构,数组第一项为开始时间,第二项为结束时间。
<press-cell title="选择日期区间" :value="date" @click="onDisplay" />
<press-calendar
:show="show"
type="range"
@close="onClose"
@confirm="onConfirm"
/>
export default {
data() {
return {
date: '',
show: false,
}
},
methods: {
onDisplay() {
this.show = true;
},
onClose() {
this.show = false;
},
formatDate(date) {
date = new Date(date);
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(detail) {
const [start, end] = detail;
this.show = false;
this.date = `${this.formatDate(start)} - ${this.formatDate(end)}`;
},
}
};
Tips: 默认情况下,日期区间的起止时间不能为同一天,可以通过设置 allow-same-day 属性来允许选择同一天。
# 快捷选择
将show-confirm设置为false可以隐藏确认按钮,这种情况下选择完成后会立即触发confirm事件。
<press-calendar :show="show" :show-confirm="false" />
# 自定义颜色
通过color属性可以自定义日历的颜色,对选中日期和底部按钮生效。
<press-calendar :show="show" color="#07c160" />
# 自定义日期范围
通过min-date和max-date定义日历的范围,需要注意的是min-date和max-date的区间不宜过大,否则会造成严重的性能问题。
<press-calendar
:show="show"
:min-date="minDate"
:max-date="maxDate"
/>
export default {
data() {
return {
show: false,
minDate: new Date(2010, 0, 1).getTime(),
maxDate: new Date(2010, 0, 31).getTime(),
}
},
};
# 自定义按钮文字
通过confirm-text设置按钮文字,通过confirm-disabled-text设置按钮禁用时的文字。
<press-calendar
:show="show"
type="range"
confirm-text="完成"
confirm-disabled-text="请选择结束时间"
/>
# 自定义日期文案
通过传入formatter函数来对日历上每一格的内容进行格式化
<press-calendar :show="show" type="range" :formatter="formatter" />
export default {
data() {
return {
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。
<press-calendar :show="show" round="false" position="right" />
# 日期区间最大范围
选择日期区间时,可以通过max-range属性来指定最多可选天数,选择的范围超过最多可选天数时,会弹出相应的提示文案。
<press-calendar type="range" :max-range="3" />
# 自定义周起始日
通过 first-day-of-week 属性设置一周从哪天开始。
<press-calendar :first-day-of-week="1" />
# 平铺展示
将poppable设置为false,日历会直接展示在页面内,而不是以弹层的形式出现。
<press-calendar
title="日历"
:poppable="false"
:show-confirm="false"
class="calendar"
/>
.calendar {
--calendar-height: 500px;
}
# API
# Props
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| type | 选择类型:single表示选择单个日期,multiple表示选择多个日期,range表示选择日期区间 | string | single |
| title | 日历标题 | string | 日期选择 |
| color | 主题色,对底部按钮和选中日期生效 | string | #ee0a24 |
| min-date | 可选择的最小日期 | timestamp | 当前日期 |
| max-date | 可选择的最大日期 | timestamp | 当前日期的六个月后 |
| default-date | 默认选中的日期,type为multiple或range时为数组 | timestamp | timestamp[] | 今天 |
| 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 | 是否为只读状态,只读状态下不能选择日期 | boolean | false |
| button-style | 按钮自定义样式 | string | object | - |
| popup-style | 弹出层自定义样式 | string | object | - |
# 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 |
# Day 数据结构
日历中的每个日期都对应一个 Day 对象,通过formatter属性可以自定义 Day 对象的内容。
| 键名 | 说明 | 类型 |
|---|---|---|
| date | 日期对应的 Date 对象 | Date |
| type | 日期类型,可选值为selected、start、middle、end、disabled | string |
| text | 中间显示的文字 | string |
| topInfo | 上方的提示信息 | string |
| bottomInfo | 下方的提示信息 | string |
| className | 自定义 className | string |
# Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| @select | 点击任意日期时触发 | value: Date | Date[] |
| @unselect | 当 Calendar 的 type 为 multiple 时,点击已选中的日期时触发 | value: Date |
| @confirm | 日期选择完成后触发,若show-confirm为true,则点击确认按钮后触发 | value: Date | Date[] |
| @open | 打开弹出层时触发 | - |
| @close | 关闭弹出层时触发 | - |
| @opened | 打开弹出层且动画结束后触发 | - |
| @closed | 关闭弹出层且动画结束后触发 | - |
| @over-range | 范围选择超过最多可选天数时触发 | - |
@click-subtitle v1.8.1 | 点击日历副标题时触发 | WechatMiniprogram.TouchEvent |
# Slots
| 名称 | 说明 |
|---|---|
| title | 自定义标题 |
| footer | 自定义底部区域内容 |
# 方法
通过 selectComponent 可以获取到 Calendar 实例并调用实例方法。
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| reset | 重置选中的日期到默认值 | - | - |
# 在线调试
← Transition Cascader →