HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
19 KiB
title, description, component
| title | description | component |
|---|---|---|
| Calendar 日历 | 可组合日期选择器,支持月、周、日视图、导航与年份选择 | calendar |
用法
<script setup lang="ts">
import { Calendar } from "@heroui-vue/components";
</script>
组件结构
<script setup lang="ts">
import {
Calendar,
CalendarHeader,
CalendarHeading,
CalendarNavButton,
CalendarGrid,
CalendarGridHeader,
CalendarGridBody,
CalendarHeaderCell,
CalendarCell,
} from "@heroui-vue/components";
</script>
<template>
<Calendar aria-label="活动日期">
<CalendarHeader>
<CalendarHeading />
<CalendarNavButton slot="previous" />
<CalendarNavButton slot="next" />
</CalendarHeader>
<CalendarGrid>
<CalendarGridHeader v-slot="{ day }">
<CalendarHeaderCell>{{ day }}</CalendarHeaderCell>
</CalendarGridHeader>
<CalendarGridBody v-slot="{ date }">
<CalendarCell :date="date" />
</CalendarGridBody>
</CalendarGrid>
</Calendar>
</template>
示例
禁用
只读
默认值
年份选择
CalendarYearPickerTrigger、CalendarYearPickerGrid 及其 body/cell 子组件提供集成的年份导航模式。
受控组件
使用受控的 v-model 与 v-model:focusedValue 进行外部状态协调与自定义快捷操作。
日期范围限制
不可用日期
使用 isDateUnavailable 阻止周末、节假日或已预订时段等日期。
月份周数
将 weeksInMonth 设为固定值(如 6)可在月份切换时保持网格高度稳定。在非公历 locale 中请谨慎使用,类似 firstDayOfWeek。
周视图
设置 :visible-duration="{ weeks: n }" 可一次显示一周或多周。导航按可见周范围前进。显示多周时使用 pageBehavior="single" 可每次移动一周。
日视图
设置 :visible-duration="{ days: n }" 可显示连续日期的滚动窗口。导航按可见日范围前进。显示多天时使用 pageBehavior="single" 可每次移动一天。
多选
设置 selection-mode="multiple" 允许选择多个日期。modelValue、defaultValue 与 @change 使用日期数组。
聚焦值
使用 v-model:focusedValue 与 @focusChange 以编程方式控制聚焦日期。
单元格标记
可自定义 CalendarCell 子节点,并使用 CalendarCellIndicator 显示事件等元数据。
自定义导航图标
向 CalendarNavButton 传入子节点以替换默认 chevron 图标。
多月份展示
使用 visibleDuration 与 offset 渲染多个月份网格,适用于预订与规划场景。
典型场景
国际化日历
默认情况下,Calendar 使用用户 locale 的历法系统显示日期。可用 ConfigProvider 包裹 Calendar 并设置 Unicode 历法 locale 扩展 来覆盖。
以下示例展示印度历法系统:
Note: @change 事件始终返回与 value 或 defaultValue 相同历法系统的日期(未提供 value 时为公历),无论显示 locale 如何。这确保应用逻辑在单一历法系统下一致运行,同时仍以用户偏好的格式显示日期。
自定义样式
Tailwind CSS
全局 CSS
@layer components {
.hv-calendar {
@apply w-72 rounded-2xl border border-border bg-surface p-3 shadow-sm;
}
.hv-calendar__heading {
@apply text-sm font-semibold text-default-foreground;
}
.hv-calendar__cell[data-selected="true"] {
@apply bg-accent text-accent-foreground;
}
}
样式参考
HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。
CSS 类
Calendar 在 packages/styles/components/calendar.css 与 packages/styles/components/calendar-year-picker.css 中使用以下类:
.hv-calendar- 根容器.hv-calendar__header- 包含导航按钮与标题的头部行.hv-calendar__heading- 当前月份标签.hv-calendar__nav-button- 上/下月导航控件.hv-calendar__grid- 主日期网格.hv-calendar__grid-header- 星期标题行包装器.hv-calendar__grid-body- 日期行包装器.hv-calendar__header-cell- 星期标题单元格.hv-calendar__cell- 可交互的日期单元格.hv-calendar__cell-indicator- 日期单元格内的点指示器.hv-calendar-year-picker__trigger- 年份选择器切换按钮.hv-calendar-year-picker__trigger-heading- 年份选择器触发器内的标题文本.hv-calendar-year-picker__trigger-indicator- 年份选择器触发器内的指示图标.hv-calendar-year-picker__year-grid- 可选年份的覆盖网格.hv-calendar-year-picker__year-cell- 单个年份选项
交互状态
Calendar 同时支持伪类与 状态 data 属性:
- Selected:
[data-selected="true"] - Today:
[data-today="true"] - Unavailable:
[data-unavailable="true"] - Outside month:
[data-outside-month="true"] - Hovered:
:hover或[data-hovered="true"] - Pressed:
:active或[data-pressed="true"] - Focus visible:
:focus-visible或[data-focus-visible="true"] - Disabled:
:disabled或[data-disabled="true"]
API 参考
Calendar
Calendar 保留固定基线的主要属性与部件,以 Vue 模型、具名部件和作用域插槽表达;不继承 React 专属属性。
| Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|
selectionMode |
'single' | 'multiple' |
'single' |
是否可选择单个或多个日期 |
value |
DateValue | null 或 DateValue[] | null |
- | 受控选中日期。selectionMode 为 multiple 时使用数组 |
defaultValue |
DateValue | null 或 DateValue[] | null |
- | 初始选中日期(非受控) |
@change |
(value: DateValue | null) 或 (value: DateValue[] | null) => void |
- | 选择变化时调用 |
focusedValue |
DateValue |
- | 受控聚焦日期 |
@focusChange |
(value: DateValue) => void |
- | 焦点移至其他日期时调用 |
minValue |
DateValue |
历法感知的 1900-01-01 |
最早可选日期 |
maxValue |
DateValue |
历法感知的 2099-12-31 |
最晚可选日期 |
weeksInMonth |
number |
- | 月份中的周数,覆盖 locale 默认值 |
isDateUnavailable |
(date: DateValue) => boolean |
- | 标记日期为不可用 |
firstDayOfWeek |
'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' |
- | 覆盖 locale 默认的一周起始日 |
pageBehavior |
'visible' | 'single' |
'visible' |
翻页按可见时长还是单个单位前进 |
selectionAlignment |
'start' | 'center' | 'end' |
'center' |
初始渲染时可见范围与选择的对齐方式 |
isDisabled |
boolean |
false |
禁用交互与选择 |
isReadOnly |
boolean |
false |
内容可读但不可更改选择 |
isInvalid |
boolean |
false |
标记日历为无效以显示验证 UI |
visibleDuration |
{months?: number; weeks?: number; days?: number} |
{months: 1} |
可见时间范围。月视图用 { months: n },周视图用 { weeks: n },日视图用 { days: n } |
defaultYearPickerOpen |
boolean |
false |
内部年份选择器初始打开状态 |
isYearPickerOpen |
boolean |
- | 受控年份选择器打开状态 |
@yearPickerOpenChange |
(isOpen: boolean) => void |
- | 年份选择器打开状态变化时调用 |
Composition Parts
| Component | Description |
|---|---|
CalendarHeader |
导航与标题的头部容器 |
CalendarHeading |
可见范围的格式化标题。支持 offset(多月布局)与 format(月/年/日选项) |
CalendarNavButton |
上/下月导航控件(slot="previous" 或 slot="next") |
CalendarGrid |
单个月份的日期网格(多月布局支持 offset) |
CalendarGridHeader |
星期标题容器 |
CalendarGridBody |
日期单元格 body 容器 |
CalendarHeaderCell |
星期标签单元格 |
CalendarCell |
单个日期单元格 |
CalendarCellIndicator |
自定义元数据的可选指示元素 |
CalendarYearPickerTrigger |
切换年份选择器模式的触发器 |
CalendarYearPickerTriggerHeading |
年份选择器触发器内的本地化标题内容 |
CalendarYearPickerTriggerIndicator |
年份选择器触发器内的切换图标 |
CalendarYearPickerGrid |
覆盖式年份选择网格容器 |
CalendarYearPickerGridBody |
年份网格单元格的 body 渲染器 |
CalendarYearPickerCell |
单个年份选项单元格 |
Year Picker Parts
年份选择器支持日期格式与窗口选项。独立部件直接从包根导入,不需要 React 上下文。
| Component | Prop | 类型 | 默认值 | 描述 |
|---|---|---|---|---|
CalendarYearPickerTriggerHeading |
format |
Intl.DateTimeFormatOptions |
- | 自定义月/年标签(如 {month: 'short'}) |
CalendarYearPickerTriggerHeading |
offset |
{months?: number} |
- | 相对聚焦日期偏移标题(多月布局) |
CalendarYearPickerGrid |
format |
Intl.DateTimeFormatOptions |
{year: 'numeric'} |
自定义年份单元格标签(纪元、历法等) |
CalendarYearPickerGrid |
visibleYears |
number |
min–max 完整范围 | 以焦点年为中心的滑动窗口年份数;缺省显示 min–max 完整范围(未设约束时为 1900–2099)。上游文档写“min–max 跨度或 20”,本实现没有 20 的回退分支 |
CalendarCell 作用域插槽
CalendarCell 的默认作用域插槽提供以下状态:
| Prop | 类型 | 描述 |
|---|---|---|
formattedDate |
string |
单元格的本地化日期标签 |
isSelected |
boolean |
日期是否选中 |
isUnavailable |
boolean |
日期是否不可用 |
isDisabled |
boolean |
单元格是否禁用 |
isOutsideMonth |
boolean |
日期是否属于相邻月份 |
有关支持的历法系统及其标识符的完整列表,请参阅:
Related packages
@internationalized/date— 所有日期组件使用的日期类型(CalendarDate、CalendarDateTime、ZonedDateTime)与工具- 本库
ConfigProvider— 覆盖子树的 locale 与布局方向 - 本库
useLocale()— 读取当前ConfigProvider的locale/dir(未包裹时回退en-US/ltr,与组件继承链一致),供startOfWeek、isWeekend、DateFormatter等本地化计算使用
Vue 模型、DOM 与当前差异
- 单选使用
v-model(DateValue | null),多选设置selection-mode="multiple"并使用日期数组。value为受控别名;显式modelValue优先,defaultValue只捕获初始值。@change与update:modelValue提供复制后的提案,父拒绝不会改变选中显示。 - 焦点使用
v-model:focusedValue,年份面板使用v-model:isYearPickerOpen。年份选择只改变聚焦年份,不改变已选日期。defaultFocusedValue固定非受控初始视图;跨时区 SSR 建议传入明确日期。 locale、dir优先于 ConfigProvider;timeZone用于“今天”标记。显示历法与公开模型历法分离;默认上下界按公历 1900-01-01 至 2099-12-31 转换到显示历法,与上游部分历法的近似年份偏移算法有差异。- 根为
role="group",网格为语义 table/grid,CalendarCell 输出 td/gridcell + 原生 button。class、ARIA、DOM 事件落在真正可交互按钮;取消 Cell 的键盘或点击事件可阻止默认处理,祖先需使用捕获事件取消。isDateDisabled完全禁用日期;isDateUnavailable保留焦点但阻止选择。 - 左右/上下键移动日期,Home/End 到周边界,PageUp/PageDown 移动月份,Shift+PageUp/PageDown 移动年份;Enter/Space 选择。年份层为三列,方向键与 Home/End 移动,Escape 关闭并恢复触发器。
- Calendar 本身不产生原生命名表单输入,也不消费 Label/FieldError 的字段协议;表单字段由 DateField 体系承载,DatePicker、DateRangePicker 复用同一套 DateField 部件。RangeCalendar、TimeField、DatePicker、DateRangePicker 均有独立公共组件。
weeksInMonth是行数下限,不裁掉真实月份日期。visibleYears是以聚焦年份为中心的窗口;默认显示完整 min/max 年份范围。复杂 era 可通过年份插槽中的date传给 CalendarYearPickerCell,避免仅凭数字年份混淆。- 键盘适配层针对安装版 Reka 缺少 week/day view、年份层及原生取消缺口使用本地日期视图算法;不运行 React,也不承诺完整 React Aria 类型或全部读屏行为等价。
相关组件
RangeCalendar 范围日历 · DateField 日期字段 · DatePicker 日期选择器
Root 默认插槽还提供 value、isDisabled、isReadOnly、isInvalid、focusedValue、visibleRange 与 isYearPickerOpen。Cell 插槽还提供 date、isOutsideVisibleRange、isToday、isHovered、isPressed、isFocused、isFocusVisible。使用 defaultFocusedValue、locale、dir、timeZone、isDateDisabled 可显式设置初始视图、国际化和禁用规则。