145a208687
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
335 lines
22 KiB
Markdown
335 lines
22 KiB
Markdown
---
|
||
title: RangeCalendar 范围日历
|
||
description: 可组合日期范围选择器,包含月份网格、导航与年份选择支持。
|
||
component: range-calendar
|
||
---
|
||
|
||
## 用法
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import { RangeCalendar } from "@heroui-vue/components";
|
||
</script>
|
||
```
|
||
|
||
<ComponentPreview name="range-calendar-basic" />
|
||
|
||
## 组件结构
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
RangeCalendar,
|
||
RangeCalendarHeader,
|
||
RangeCalendarHeading,
|
||
RangeCalendarNavButton,
|
||
RangeCalendarGrid,
|
||
RangeCalendarGridHeader,
|
||
RangeCalendarGridBody,
|
||
RangeCalendarHeaderCell,
|
||
RangeCalendarCell,
|
||
} from "@heroui-vue/components";
|
||
</script>
|
||
<template>
|
||
<RangeCalendar aria-label="行程日期" first-day-of-week="mon">
|
||
<RangeCalendarHeader>
|
||
<RangeCalendarHeading />
|
||
<RangeCalendarNavButton slot="previous" />
|
||
<RangeCalendarNavButton slot="next" />
|
||
</RangeCalendarHeader>
|
||
<RangeCalendarGrid>
|
||
<RangeCalendarGridHeader v-slot="{ day }">
|
||
<RangeCalendarHeaderCell>{{ day }}</RangeCalendarHeaderCell>
|
||
</RangeCalendarGridHeader>
|
||
<RangeCalendarGridBody v-slot="{ date }">
|
||
<RangeCalendarCell :date="date" />
|
||
</RangeCalendarGridBody>
|
||
</RangeCalendarGrid>
|
||
</RangeCalendar>
|
||
</template>
|
||
```
|
||
|
||
## 示例
|
||
|
||
### 禁用
|
||
|
||
<ComponentPreview name="range-calendar-disabled" />
|
||
|
||
### 年份选择
|
||
|
||
`CalendarYearPickerTrigger`、`CalendarYearPickerGrid` 及其 body/cell 部件复用同一日历 owner,提供年份导航。
|
||
|
||
<ComponentPreview name="range-calendar-year-picker" />
|
||
|
||
### 默认值
|
||
|
||
<ComponentPreview name="range-calendar-default-value" />
|
||
|
||
### 受控组件
|
||
|
||
<ComponentPreview name="range-calendar-controlled" />
|
||
|
||
### 日期范围限制
|
||
|
||
<ComponentPreview name="range-calendar-min-max-dates" />
|
||
|
||
### 不可用日期
|
||
|
||
使用 `isDateUnavailable` 屏蔽周末、节假日或已预订等不可用日期。
|
||
|
||
<ComponentPreview name="range-calendar-unavailable-dates" />
|
||
|
||
### 基于锚点的不可用日期
|
||
|
||
选择范围时,`isDateUnavailable` 会收到第二个参数 `anchorDate`(首个选中日期)。可用它限制哪些结束日期有效(例如起始日期后 7 天内)。
|
||
|
||
<ComponentPreview name="range-calendar-anchor-unavailable-dates" />
|
||
|
||
### 月份周数
|
||
|
||
将 `weeksInMonth` 设为固定值(例如 `6`),在月份切换时保持网格高度稳定。
|
||
|
||
<ComponentPreview name="range-calendar-weeks-in-month" />
|
||
|
||
### 周视图
|
||
|
||
设置 `:visible-duration="{ weeks: n }"` 一次显示一个或多个周。导航按可见周范围前进。显示多周时,使用 `pageBehavior="single"` 每次移动一周。
|
||
|
||
<ComponentPreview name="range-calendar-week-view" />
|
||
|
||
### 日视图
|
||
|
||
设置 `:visible-duration="{ days: n }"` 显示连续日期的滚动窗口。导航按可见日范围前进。显示多天时,使用 `pageBehavior="single"` 每次移动一天。
|
||
|
||
<ComponentPreview name="range-calendar-day-view" />
|
||
|
||
### 非连续范围
|
||
|
||
启用 `allowsNonContiguousRanges` 以允许跨越不可用日期进行选择。
|
||
|
||
<ComponentPreview name="range-calendar-allows-non-contiguous-ranges" />
|
||
|
||
### 只读
|
||
|
||
<ComponentPreview name="range-calendar-read-only" />
|
||
|
||
### 无效状态
|
||
|
||
<ComponentPreview name="range-calendar-invalid" />
|
||
|
||
### 聚焦值
|
||
|
||
<ComponentPreview name="range-calendar-focused-value" />
|
||
|
||
### 单元格标记
|
||
|
||
可自定义 `RangeCalendarCell` 的子内容,并使用 `RangeCalendarCellIndicator` 展示事件等元数据。
|
||
|
||
<ComponentPreview name="range-calendar-with-indicators" />
|
||
|
||
### 典型场景
|
||
|
||
<ComponentPreview name="range-calendar-booking-calendar" />
|
||
|
||
### 多月份展示
|
||
|
||
结合 `visibleDuration` 与 `offset` 渲染多个网格,适用于预订与规划场景。
|
||
|
||
<ComponentPreview name="range-calendar-multiple-months" />
|
||
|
||
### 国际化日历
|
||
|
||
默认情况下,RangeCalendar 使用用户 locale 的历法系统显示日期。可用 `ConfigProvider` 包裹组件,并设置 [Unicode 历法 locale 扩展](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar#adding_a_calendar_in_the_locale_string) 覆盖。
|
||
|
||
以下示例展示印度历法系统:
|
||
|
||
<ComponentPreview name="range-calendar-international-calendar" />
|
||
|
||
使用 `v-model` 绑定范围,`v-model:focusedValue` 绑定焦点,`v-model:isYearPickerOpen` 绑定年份面板。
|
||
|
||
**注意:** 无论显示 locale 如何,`@change` 事件始终返回与 `value` 或 `defaultValue` 相同历法系统的日期(未提供 value 时为公历)。
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="range-calendar-custom-styles" />
|
||
|
||
### 全局 CSS
|
||
|
||
若要自定义组件类,可使用 `@layer components` 指令。[了解更多](https://tailwindcss.com/docs/adding-custom-styles#adding-component-classes)。
|
||
|
||
```css
|
||
@layer components {
|
||
.hv-range-calendar {
|
||
@apply w-80 rounded-2xl border border-border bg-surface p-3 shadow-sm;
|
||
}
|
||
|
||
.hv-range-calendar__heading {
|
||
@apply text-sm font-semibold text-default;
|
||
}
|
||
|
||
.hv-range-calendar__cell[data-selected="true"]
|
||
.hv-range-calendar__cell-button {
|
||
@apply bg-accent text-accent-foreground;
|
||
}
|
||
}
|
||
```
|
||
|
||
## 样式参考
|
||
|
||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
||
|
||
### CSS 类
|
||
|
||
RangeCalendar 在 `packages/styles/components/range-calendar.css` 与 `packages/styles/components/calendar-year-picker.css` 中使用以下类:
|
||
|
||
- `.hv-range-calendar` - 根容器。
|
||
- `.hv-range-calendar__header` - 含导航按钮与标题的头部行。
|
||
- `.hv-range-calendar__heading` - 当前月份标签。
|
||
- `.hv-range-calendar__nav-button` - 上一月/下一月导航控件。
|
||
- `.hv-range-calendar__grid` - 主体日期网格。
|
||
- `.hv-range-calendar__grid-header` - 星期标题行外层。
|
||
- `.hv-range-calendar__grid-body` - 日期行外层。
|
||
- `.hv-range-calendar__header-cell` - 星期标题单元格。
|
||
- `.hv-range-calendar__cell` - 可交互日期单元格外层。
|
||
- `.hv-range-calendar__cell-button` - 单元格内的可交互日期按钮。
|
||
- `.hv-range-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` - 单个年份选项。
|
||
|
||
### 交互状态
|
||
|
||
RangeCalendar 同时支持伪类与适配后的 data 属性:
|
||
|
||
- **已选中**:`[data-selected="true"]`
|
||
- **范围起点**:`[data-selection-start="true"]`
|
||
- **范围终点**:`[data-selection-end="true"]`
|
||
- **范围内**:`[data-selection-in-range="true"]`
|
||
- **今天**:`[data-today="true"]`
|
||
- **不可用**:`[data-unavailable="true"]`
|
||
- **跨月**:`[data-outside-month="true"]`
|
||
- **悬停**:`:hover` 或 `[data-hovered="true"]`
|
||
- **按下**:`:active` 或 `[data-pressed="true"]`
|
||
- **焦点可见**:`:focus-visible` 或 `[data-focus-visible="true"]`
|
||
- **禁用**:`:disabled` 或 `[data-disabled="true"]`
|
||
|
||
## API 参考
|
||
|
||
### RangeCalendar
|
||
|
||
RangeCalendar 保留固定基线的主要属性,通过 Vue 模型、具名部件与作用域插槽表达;React 专属类型与上下文不移植。继承属性的实际支持边界见本页末尾。
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| --------------------------- | ---------------------------------------------------------------- | ----------------------- | ---------------------------------------------------------------------------------------------- |
|
||
| `modelValue` / `value` | `CalendarDateRange \| null` | - | 受控的选中范围。 |
|
||
| `defaultValue` | `CalendarDateRange \| null` | - | 初始选中范围(非受控)。 |
|
||
| `@change` | `(value: CalendarDateRange \| null) => void` | - | 选中变化时调用。 |
|
||
| `focusedValue` | `DateValue` | - | 受控的焦点日期。 |
|
||
| `@focusChange` | `(value: DateValue) => void` | - | 焦点移动到其它日期时调用。 |
|
||
| `minValue` | `DateValue` | 历法感知的 `1900-01-01` | 可选的最早日期。 |
|
||
| `maxValue` | `DateValue` | 历法感知的 `2099-12-31` | 可选的最晚日期。 |
|
||
| `weeksInMonth` | `number` | - | 一个月的周数。该值会覆盖区域设置的默认值。 |
|
||
| `isDateUnavailable` | `(date: DateValue, anchorDate: CalendarDate \| null) => boolean` | - | 将日期标记为不可用。`anchorDate` 为当前范围选择中的首个日期。 |
|
||
| `firstDayOfWeek` | `'sun' \| 'mon' \| 'tue' \| 'wed' \| 'thu' \| 'fri' \| 'sat'` | - | 覆盖区域设置的一周起始日。 |
|
||
| `pageBehavior` | `'visible' \| 'single'` | `'visible'` | 翻页按可见范围或单步前进。 |
|
||
| `selectionAlignment` | `'start' \| 'center' \| 'end'` | `'center'` | 初始渲染时按选中项对齐可见范围。 |
|
||
| `allowsNonContiguousRanges` | `boolean` | `false` | 允许范围跨越不可用日期。 |
|
||
| `isDisabled` | `boolean` | `false` | 禁用交互与选择。 |
|
||
| `isReadOnly` | `boolean` | `false` | 内容只读,不可更改选中。 |
|
||
| `isInvalid` | `boolean` | `false` | 标记为无效以配合校验样式。 |
|
||
| `visibleDuration` | `{months?: number; weeks?: number; days?: number}` | `{months: 1}` | 可见时间范围。使用 `{ months: n }` 为月视图,`{ weeks: n }` 为周视图,`{ days: n }` 为日视图。 |
|
||
| `defaultYearPickerOpen` | `boolean` | `false` | 内置年份选择器的初始展开状态。 |
|
||
| `isYearPickerOpen` | `boolean` | - | 受控的年份选择器展开状态。 |
|
||
| `@yearPickerOpenChange` | `(isOpen: boolean) => void` | - | 年份选择器展开状态变化时调用。 |
|
||
|
||
### 组合部件
|
||
|
||
| 组件 | 描述 |
|
||
| ------------------------------------ | ---------------------------------------------------------------------------------- |
|
||
| `RangeCalendarHeader` | 导航与标题的头部容器。 |
|
||
| `RangeCalendarHeading` | 可见范围的格式化标题。支持 `offset`(多月份布局)与 `format`(月/年/日格式选项)。 |
|
||
| `RangeCalendarNavButton` | 上一页/下一页导航(`slot="previous"` 或 `slot="next"`)。 |
|
||
| `RangeCalendarGrid` | 单个月的日期网格(多月份布局支持 `offset`)。 |
|
||
| `RangeCalendarGridHeader` | 星期标题容器。 |
|
||
| `RangeCalendarGridBody` | 日期单元格主体容器。 |
|
||
| `RangeCalendarHeaderCell` | 星期标签单元格。 |
|
||
| `RangeCalendarCell` | 单个日期单元格。 |
|
||
| `RangeCalendarCellIndicator` | 用于自定义元数据的可选指示元素。 |
|
||
| `CalendarYearPickerTrigger` | 切换年份选择模式的触发器。 |
|
||
| `CalendarYearPickerTriggerHeading` | 年份选择触发器内的本地化标题内容。 |
|
||
| `CalendarYearPickerTriggerIndicator` | 年份选择触发器内的切换图标。 |
|
||
| `CalendarYearPickerGrid` | 年份选择覆盖网格容器。 |
|
||
| `CalendarYearPickerGridBody` | 年份网格单元格的 body 渲染器。 |
|
||
| `CalendarYearPickerCell` | 单个年份选项单元格。 |
|
||
|
||
### 年份选择器子组件
|
||
|
||
年份选择器复用公共 CalendarYearPicker 部件,支持以下格式化属性。
|
||
|
||
| 组件 | 属性 | 类型 | 默认值 | 描述 |
|
||
| ---------------------------------- | -------------- | ---------------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||
| `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),传入数字时以焦点年为中心。上游文档写“或 `20`”,本实现没有该回退分支 |
|
||
|
||
### RangeCalendarCell 作用域插槽
|
||
|
||
`RangeCalendarCell` 的默认作用域插槽可使用以下状态:
|
||
|
||
| Prop | 类型 | 描述 |
|
||
| ------------------ | --------- | ------------------------ |
|
||
| `formattedDate` | `string` | 单元格日期的本地化文案。 |
|
||
| `isSelected` | `boolean` | 该日期是否已选中。 |
|
||
| `isSelectionStart` | `boolean` | 是否为选中范围的起点。 |
|
||
| `isSelectionEnd` | `boolean` | 是否为选中范围的终点。 |
|
||
| `isUnavailable` | `boolean` | 该日期是否不可用。 |
|
||
| `isDisabled` | `boolean` | 单元格是否禁用。 |
|
||
| `isOutsideMonth` | `boolean` | 是否属于相邻月份。 |
|
||
|
||
支持的历法系统及其标识符完整列表见:
|
||
|
||
- [React Aria Calendar Implementations](https://react-aria.adobe.com/internationalized/date/Calendar#implementations)
|
||
- [React Aria International Calendars](https://react-aria.adobe.com/Calendar#international-calendars)
|
||
|
||
### Related packages
|
||
|
||
- [`@internationalized/date`](https://react-aria.adobe.com/internationalized/date/) — 各日期组件共用的日期类型(`CalendarDate`、`CalendarDateTime`、`ZonedDateTime`)与工具函数
|
||
- 本库 `ConfigProvider` — 为子树覆盖语言环境与书写方向
|
||
|
||
## 相关组件
|
||
|
||
[Calendar 日历](./calendar) · [DateField 日期字段](./date-field) · [DatePicker 日期选择器](./date-picker)
|
||
|
||
## Vue 公共契约与差异
|
||
|
||
- `RangeCalendar` 与 `RangeCalendarRoot` 是同一根组件;其他部件使用本页列出的具名导出。年份六部件直接使用 `CalendarYearPicker*`,没有 React 对象式成员或私有上下文导出。`rangeCalendarVariants()` 提供基线 slots,无额外视觉变体。
|
||
- `modelValue`(包括显式 null/undefined)优先于 `value`;`defaultValue` 只捕获初始值。未完成锚点与悬停预览不触发值变更。完成选择时 `update:modelValue` 与 `change` 提供分别复制的提案,受控父组件拒绝后可重新提交相同范围。倒置初值保留并标记无效,用户反向选择则按日期先后排序。
|
||
- 复用现有 Calendar 视图/焦点/年份 owner,日期交互采用局部 Vue 适配。`CalendarDateRange<T extends DateValue>` 表示完整的两个端点;主模型为 `CalendarRangeModel`(`CalendarDateRange | null`)。选择保留端点各自的日期类、历法、时间与时区;显示 locale 只改变视图。未提供初值的选择返回公历 CalendarDate。
|
||
- 默认上下界以公历 1900-01-01 至 2099-12-31 转换到显示历法,区别于上游对部分历法的近似年份偏移。`weeksInMonth` 是月份行数下限,不裁掉实际日期。年份默认覆盖有效年界,`visibleYears` 为以焦点年为中心的窗口。
|
||
- `isDateDisabled` 是额外支持的完全禁用规则;`isDateUnavailable` 保留焦点而阻止选择。`allowsNonContiguousRanges` 可跨不可用日,不能跨完全禁用日;锚点只在选取范围期间传入不可用规则。触摸支持两次点击选择并保留页面滚动;触屏长按拖选暂未实现。鼠标支持拖选和拖动已有端点。
|
||
- 根输出 `role="group"`,网格为 table/grid,Cell 为 td/gridcell 内原生 button,内层 span 保留基线 cell-button 样式。Cell 的 class、style、ARIA、DOM 事件和公开 element/focus 属于实际 button;Header/HeaderCell/Indicator 的 `as` 可替换标签。没有 React `render`、`className`、PressEvent 或函数式 style,改用 `as`、class/style、原生事件与作用域插槽。用户直接取消 Cell 的可取消事件可阻止默认选择/导航;祖先取消使用捕获事件。
|
||
- 左右/上下键移动焦点,Home/End 到周边界,PageUp/PageDown 按月、Shift+PageUp/PageDown 按年;Enter/Space 开始或确认范围,Escape 放弃草稿。年份层方向键/Home/End 移动,选择年份后聚焦对应日期,Escape 关闭后恢复触发器焦点。
|
||
- 独立日历不生成原生命名输入,不提供字段的 required/校验提交/reset;字段及 Picker 另行组合。React Aria 的全部继承 props 不作等价支持声明,未列出的框架专属事件、验证详情与上下文未公开。
|
||
|
||
### 补充属性与实例方法
|
||
|
||
| 属性 | 类型 | 默认值 | 描述 |
|
||
| --------------------- | ------------------------------ | ------------------------- | ------------------- |
|
||
| `defaultFocusedValue` | `DateValue` | 初值 start 或今天 | 非受控初始焦点 |
|
||
| `autoFocus` | `boolean` | `false` | 挂载后聚焦日期 |
|
||
| `locale` | `string` | ConfigProvider 或 `en-US` | 显示历法与格式 |
|
||
| `dir` | `'ltr' \| 'rtl'` | ConfigProvider 或 `ltr` | 排列与方向键 |
|
||
| `timeZone` | `string` | 本地时区 | 今天标记 |
|
||
| `isDateDisabled` | `(date: DateValue) => boolean` | - | 完全禁用日期 |
|
||
| `disabled` | `boolean` | `false` | isDisabled 原生别名 |
|
||
| `as` | `string` | `div` | 根标签 |
|
||
|
||
根实例公开 `element` 与 `focus()`,后者聚焦当前可用日期;Cell 实例公开实际 button 和 `focus()`。Root 默认插槽提供 `value`、`isDisabled`、`isReadOnly`、`isInvalid`、`focusedValue`、`visibleRange`、`isYearPickerOpen`;Vue 扩展 `anchorDate` 与 `highlightedRange` 是复制后的草稿快照,不可用作已提交值。
|
||
|
||
GridHeader 插槽提供 `{ day, index }`;GridBody 提供 `{ date }`。Cell 插槽除表中状态,还提供 `date`、`isOutsideVisibleRange`、`isToday`、`isHovered`、`isPressed`、`isFocused`、`isFocusVisible`、`isInvalid`;可在消费端组合额外展示而不覆盖内核状态。
|