CalendarView 内联日历
本地日期日历,支持单选、多选和范围;范围第一下只改变草稿,不提交 modelValue。
引入
uni-app 配置 easycom 后使用 hui-calendar-view。普通 Vue 从 @nscode/handy-uni/web 具名导入 HuiCalendarView,或安装预览插件。
示例
交互与事件结果
操作组件后,下方显示最近一次事件及完整参数。弹层选择器先修改草稿,确认后提交;取消保留原值。
vue
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
const value = ref([])
function event0(arg0: unknown) {
result.value = 'update:modelValue' + ': ' + JSON.stringify([arg0])
}
function event1(arg0: unknown, arg1: unknown) {
result.value = 'change' + ': ' + JSON.stringify([arg0, arg1])
}
function event2(arg0: unknown, arg1: unknown) {
result.value = 'select' + ': ' + JSON.stringify([arg0, arg1])
}
function event3(arg0: unknown) {
result.value = 'update:month' + ': ' + JSON.stringify([arg0])
}
function event4(arg0: unknown) {
result.value = 'over-range' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view>
<hui-calendar-view
v-model="value"
select-mode="range"
default-month="2026-11"
min="2026-11-01"
max="2027-12-31"
:max-range="7"
@update:model-value="event0"
@change="event1"
@select="event2"
@update:month="event3"
@over-range="event4"
></hui-calendar-view>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model-value | CalendarModelValue | undefined | undefined | 受控提交值;父组件不回写时展示保持原值。 |
default-value | CalendarModelValue | undefined | null | 非受控初始值。 |
select-mode | CalendarSelectMode | undefined | 'single' | single 单选、multiple 多选、range 范围。 |
value-format | string | undefined | 'YYYY-MM-DD' | 模型格式;支持格式字符串、timestamp 或 date。 |
min | CalendarValue | undefined | — | 最小值;在多选中表示最少选择数量。 |
max | CalendarValue | undefined | — | 最大值;在评分中是星数,在多选中是选择数量上限。 |
disabled-date | ((date: Date) => boolean) | undefined | — | 返回 true 禁用日期。 |
month | string | undefined | — | 受控显示月份,YYYY-MM。 |
default-month | string | undefined | — | 初始显示月份,YYYY-MM。 |
first-day-of-week | number | undefined | 7 | 每周第一天:1 为周一,7 为周日。 |
max-range | number | undefined | — | 范围最大跨度,日期包含首尾,年月/年份按自然单位。 |
allow-same-day | boolean | undefined | true | 范围允许相同起止日期。 |
max-count | number | undefined | — | 多选日期数上限。 |
formatter | ((day: CalendarDay) => Partial<Pick<CalendarDay, "disabled" | "topText" | "bottomText" | "className">>) | undefined | — | 日历单元格文字/状态格式化,重算日期状态或 reset 时使用最新函数。 |
readonly | boolean | undefined | false | 只读;未设置时继承表单上下文。 |
custom-class | string | undefined | '' | 根节点附加类名。 |
custom-style | string | CSSProperties | undefined | — | 根节点附加样式。 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:modelValue | (value: CalendarModelValue) | 请求更新 modelValue;受控模式由父级回写。 |
change | (value: CalendarModelValue, context: { dates: Date[]; }) | 提交后的值与上下文,发生在模型更新事件之后。 |
select | (value: CalendarModelValue, context: { dates: Date[]; }) | 选择中的草稿及原始数据,不代表已经提交。 |
update:month | (month: string) | 请求更新 month;受控模式由父级回写。 |
over-range | (info: { reason: "max-range" | "max-count"; }) | 报告 over-range,完整参数见示例事件输出。 |
Slots
| 插槽 | 说明 |
|---|---|
header | 月份头,作用域 {month,prev,next,canPrev,canNext}。 |
day | 日历单元格,作用域 {day}。 |
方法
| 方法 | 签名 |
|---|---|
scrollToMonth | (month: string) => void |
reset | () => void |
类型
CalendarViewProps、CalendarViewEmits、CalendarViewExpose。公开类型由包入口统一导出。
平台说明
| 范围 | 行为与验证边界 |
|---|---|
| 公共实现 | 日历网格由普通组件节点渲染;日期计算与单选、多选、范围规则三端共享。 |
| 设备待验 | 核对长月份范围的滚动、日期点击和范围提示;实际设备检查触摸区域及屏幕阅读。 |
对应操作见 examples/uni-demo/smoke/CalendarView.md。单元测试不替代真机验收。