日历
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
29 | 30 | 1 | 2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 | 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
功能特性
- 完整的键盘导航
- 支持受控与非受控模式
- 焦点管理完善
- 本地化支持
- 高度可组合
前言
该组件依赖于 @internationalized/date 包,该包解决了在 JavaScript 中处理日期和时间时的诸多问题。
我们强烈建议阅读该包的文档以深入了解其工作原理,并且您需要在项目中安装它才能使用与日期相关的组件。
安装
安装日期包。
$ npm add @internationalized/date从命令行安装该组件。
$ npm add reka-ui结构剖析
导入所有部件并将它们组合在一起。
<script setup>
import {
CalendarCell,
CalendarCellTrigger,
CalendarGrid,
CalendarGridBody,
CalendarGridHead,
CalendarGridRow,
CalendarHeadCell,
CalendarHeader,
CalendarHeading,
CalendarNext,
CalendarPrev,
CalendarRoot,
} from 'reka-ui'
</script>
<template>
<CalendarRoot>
<CalendarHeader>
<CalendarPrev />
<CalendarHeading />
<CalendarNext />
</CalendarHeader>
<CalendarGrid>
<CalendarGridHead>
<CalendarGridRow>
<CalendarHeadCell />
</CalendarGridRow>
</CalendarGridHead>
<CalendarGridBody>
<CalendarGridRow>
<CalendarCell>
<CalendarCellTrigger />
</CalendarCell>
</CalendarGridRow>
</CalendarGridBody>
</CalendarGrid>
</CalendarRoot>
</template>API 参考
Root
包含日历的所有部分
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
calendarLabel | stringThe accessible label for the calendar | |
defaultPlaceholder | DateValueThe default placeholder date | |
defaultValue | DateValueThe default value for the calendar | |
dir | 'ltr' | 'rtl'The reading direction of the calendar when applicable. | |
disabled | false | booleanWhether the calendar is disabled |
disableDaysOutsideCurrentView | false | booleanWhether or not to disable days outside the current view. |
fixedWeeks | false | booleanWhether or not to always display 6 weeks in the calendar |
initialFocus | false | booleanIf true, the calendar will focus the selected day, today, or the first day of the month depending on what is visible when the calendar is mounted |
isDateDisabled | MatcherA function that returns whether or not a date is disabled | |
isDateUnavailable | MatcherA function that returns whether or not a date is unavailable | |
locale | stringThe locale to use for formatting dates | |
maxValue | DateValueThe maximum date that can be selected | |
minValue | DateValueThe minimum date that can be selected | |
modelValue | DateValue | DateValue[]The controlled checked state of the calendar | |
multiple | false | booleanWhether multiple dates can be selected |
nextPage | ((placeholder: DateValue) => DateValue)A function that returns the next page of the calendar. It receives the current placeholder as an argument inside the component. | |
numberOfMonths | 1 | numberThe number of months to display at once |
pagedNavigation | false | booleanThis property causes the previous and next buttons to navigate by the number of months displayed at once, rather than one month |
placeholder | DateValueThe placeholder date, which is used to determine what month to display when no date is selected | |
preventDeselect | false | booleanWhether or not to prevent the user from deselecting a date without selecting another date first |
prevPage | ((placeholder: DateValue) => DateValue)A function that returns the previous page of the calendar. It receives the current placeholder as an argument inside the component. | |
readonly | false | booleanWhether the calendar is readonly |
weekdayFormat | 'narrow' | 'narrow' | 'short' | 'long'The format to use for the weekday strings provided via the weekdays slot prop |
weekStartsOn | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6The day of the week to start the calendar on |
| Emit | Payload |
|---|---|
update:modelValue | [date: DateValue]Event handler called whenever the model value changes |
update:placeholder | [date: DateValue]Event handler called whenever the placeholder value changes |
| Slots (default) | Payload |
|---|---|
date | DateValueThe current date of the placeholder |
grid | Grid<DateValue>[]The grid of dates |
weekDays | string[]The days of the week |
weekStartsOn | 0 | 1 | 2 | 3 | 4 | 5 | 6The start of the week |
locale | stringThe calendar locale |
fixedWeeks | booleanWhether or not to always display 6 weeks in the calendar |
modelValue | DateValue | DateValue[] | undefinedThe current date of the calendar |
| Methods | Type |
|---|---|
isDateDisabled | MatcherA function that returns whether or not a date is disabled |
isDateUnavailable | MatcherA function that returns whether or not a date is unavailable |
| Data Attribute | Value |
|---|---|
[data-readonly] | 只读时存在 |
[data-disabled] | 禁用时存在 |
[data-invalid] | 无效时存在 |
Header
包含导航按钮和标题部分。
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Prev Button
日历导航按钮。根据当前日历视图,将日历向前导航一个月/一年/十年。
| Prop | Default | Type |
|---|---|---|
as | 'button' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
prevPage | ((placeholder: DateValue) => DateValue)The function to be used for the prev page. Overwrites the |
| Slots (default) | Payload |
|---|---|
disabled | booleanCurrent disable state |
| Data Attribute | Value |
|---|---|
[data-disabled] | 禁用时存在 |
Next Button
日历导航按钮。根据当前日历视图,将日历向后导航一个月/一年/十年。
| Prop | Default | Type |
|---|---|---|
as | 'button' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
nextPage | ((placeholder: DateValue) => DateValue)The function to be used for the next page. Overwrites the |
| Slots (default) | Payload |
|---|---|
disabled | booleanCurrent disable state |
| Data Attribute | Value |
|---|---|
[data-disabled] | 禁用时存在 |
Heading
用于显示当前月份和年份的标题
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Slots (default) | Payload |
|---|---|
headingValue | stringCurrent month and year |
| Data Attribute | Value |
|---|---|
[data-disabled] | 禁用时存在 |
Grid
用于包裹日历网格的容器。
| Prop | Default | Type |
|---|---|---|
as | 'table' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
| Data Attribute | Value |
|---|---|
[data-readonly] | 只读时存在 |
[data-disabled] | 禁用时存在 |
Grid Head
用于包裹网格头部的容器。
| Prop | Default | Type |
|---|---|---|
as | 'thead' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Grid Body
用于包裹网格主体的容器。
| Prop | Default | Type |
|---|---|---|
as | 'tbody' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Grid Row
用于包裹网格行的容器。
| Prop | Default | Type |
|---|---|---|
as | 'tr' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Head Cell
用于包裹头部单元格的容器。用于显示星期几。
| Prop | Default | Type |
|---|---|---|
as | 'th' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. |
Cell
用于包裹日历单元格的容器。
| Prop | Default | Type |
|---|---|---|
as | 'td' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
date* | DateValueThe date value for the cell |
| Data Attribute | Value |
|---|---|
[data-disabled] | 禁用时存在 |
Cell Trigger
用于显示单元格日期的可交互容器。点击它可选择日期。
| Prop | Default | Type |
|---|---|---|
as | 'div' | AsTag | ComponentThe element or component this component should render as. Can be overwritten by |
asChild | booleanChange the default rendered element for the one passed as a child, merging their props and behavior. Read our Composition guide for more details. | |
day* | DateValueThe date value provided to the cell trigger | |
month* | DateValueThe month in which the cell is rendered |
| Slots (default) | Payload |
|---|---|
dayValue | stringCurrent day |
disabled | booleanCurrent disable state |
selected | booleanCurrent selected state |
today | booleanCurrent today state |
outsideView | booleanCurrent outside view state |
outsideVisibleView | booleanCurrent outside visible view state |
unavailable | booleanCurrent unavailable state |
| Data Attribute | Value |
|---|---|
[data-selected] | 选中时存在 |
[data-value] | 日期的 ISO 字符串值。 |
[data-disabled] | 禁用时存在 |
[data-unavailable] | 不可用时存在 |
[data-today] | 为今天日期时存在 |
[data-outside-view] | 当日期位于其显示的当前月份之外时存在。 |
[data-outside-visible-view] | 当日期位于日历上可见的月份之外时存在。 |
[data-focused] | 获得焦点时存在 |
示例
支持年份递增的日历
此示例展示了一个允许递增年份的日历。
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 31 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 1 | 2 | 3 | 4 | 5 | 6 |
支持区域设置和日历系统选择的日历
此示例展示了一些可用的区域设置以及日历系统的显示方式。
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 31 | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 1 | 2 | 3 | 4 | 5 | 6 |
支持滑动手势导航的日历
此组件演示了使用基于触摸的滑动手势进行直观的日历导航,提供了一种用户友好的方式来浏览月份。
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 | 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 |
显示周数的日历
此示例展示了用于显示周数的 CalendarWeek 组件的用法。
| Wk | S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 31 | 1 | |
2 | 3 | 4 | 5 | 6 | 7 | 8 | |
9 | 10 | 11 | 12 | 13 | 14 | 15 | |
16 | 17 | 18 | 19 | 20 | 21 | 22 | |
23 | 24 | 25 | 26 | 27 | 28 | 29 | |
30 | 1 | 2 | 3 | 4 | 5 | 6 |
无障碍访问
键盘交互
| Key | Description |
|---|---|
Tab | 当焦点移动到日历上时,聚焦第一个导航按钮。 |
Space |
当焦点位于 CalendarNext 或 CalendarPrev 上时,它将导航日历。否则,它将选择日期。
|
Enter |
当焦点位于 CalendarNext 或 CalendarPrev 上时,它将导航日历。否则,它将选择日期。
|
ArrowLeftArrowRightArrowUpArrowDown |
当焦点位于 CalendarCellTrigger 上时,它将导航日期,并在必要时更改月份/年份/十年。
|
