Calendar
显示日期和星期几,方便与日期相关的互动。
<script setup lang="ts">
import type { CalendarRootProps } from 'reka-ui'
import { Icon } from '@iconify/vue'
import { CalendarDate } from '@internationalized/date'
import { CalendarCell, CalendarCellTrigger, CalendarGrid, CalendarGridBody, CalendarGridHead, CalendarGridRow, CalendarHeadCell, CalendarHeader, CalendarHeading, CalendarNext, CalendarPrev, CalendarRoot } from 'reka-ui'
const date = new CalendarDate(2024, 10, 3)
const isDateUnavailable: CalendarRootProps['isDateUnavailable'] = (date) => {
return date.day === 17 || date.day === 18
}
</script>
<template>
<CalendarRoot
v-slot="{ weekDays, grid }"
:is-date-unavailable="isDateUnavailable"
:default-value="date"
class="mt-6 rounded-xl bg-white p-4 shadow-sm border"
fixed-weeks
>
<CalendarHeader class="flex items-center justify-between">
<CalendarPrev
class="inline-flex items-center cursor-pointer text-black justify-center rounded-md bg-transparent w-7 h-7 hover:bg-stone-50 active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-left"
class="w-4 h-4"
/>
</CalendarPrev>
<CalendarHeading class="text-sm text-black font-medium" />
<CalendarNext
class="inline-flex items-center cursor-pointer justify-center text-black rounded-md bg-transparent w-7 h-7 hover:bg-stone-50 active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-right"
class="w-4 h-4"
/>
</CalendarNext>
</CalendarHeader>
<div
class="flex flex-col space-y-4 pt-4 sm:flex-row sm:space-x-4 sm:space-y-0"
>
<CalendarGrid
v-for="month in grid"
:key="month.value.toString()"
class="w-full border-collapse select-none space-y-1"
>
<CalendarGridHead>
<CalendarGridRow class="mb-1 grid w-full grid-cols-7">
<CalendarHeadCell
v-for="day in weekDays"
:key="day"
class="rounded-md text-xs text-green-500"
>
{{ day }}
</CalendarHeadCell>
</CalendarGridRow>
</CalendarGridHead>
<CalendarGridBody class="grid">
<CalendarGridRow
v-for="(weekDates, index) in month.rows"
:key="`weekDate-${index}`"
class="grid grid-cols-7"
>
<CalendarCell
v-for="weekDate in weekDates"
:key="weekDate.toString()"
:date="weekDate"
class="relative text-center text-sm"
>
<CalendarCellTrigger
:day="weekDate"
:month="month.value"
class="relative flex items-center justify-center rounded-full whitespace-nowrap text-sm font-normal text-black w-8 h-8 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black data-[outside-view]:text-black/30 data-[selected]:!bg-green-900 data-[selected]:text-white hover:bg-green-500 data-[highlighted]:bg-green-500 data-[unavailable]:pointer-events-none data-[unavailable]:text-black/30 data-[unavailable]:line-through before:absolute before:top-[5px] before:hidden before:rounded-full before:w-1 before:h-1 before:bg-white data-[today]:before:block data-[today]:before:bg-green-900 "
/>
</CalendarCell>
</CalendarGridRow>
</CalendarGridBody>
</CalendarGrid>
</div>
</CalendarRoot>
</template>
功能特性 (Features)
- 完整的键盘导航。
- 可控或非控。
- 完全管理焦点。
- 支持本地化。
- 高度可组合。
前言 (Preface)
该组件依赖于 @internationalized/date 包,该包解决了 JavaScript 中处理日期和时间带来的许多问题。
我们强烈建议您阅读该包的文档,以充分了解其工作原理,并且您需要在项目中安装它才能使用日期相关的组件。
安装日期包 (Install the date package)
$ npm add @internationalized/date
$ pnpm add @internationalized/date
$ yarn add @internationalized/date
$ bun add @internationalized/date
安装组件 (Install the component)
$ npm add reka-ui
$ pnpm add reka-ui
$ yarn add reka-ui
$ bun add reka-ui
结构 (Anatomy)
导入所有部分并将其组合在一起。
<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 参考 (API Reference)
根 (Root)
包含日历的所有部分。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'div' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 | |
calendarLabel | string | 日历的可访问性标签。 | |
defaultPlaceholder | DateValue | 默认占位符日期。 | |
defaultValue | DateValue | 日历的默认值。 | |
dir | 'ltr' | 'rtl' | 日历的阅读方向(如果适用)。如果省略,则全局继承自 ConfigProvider 或假定为 LTR(从左到右)阅读模式。 | |
disabled | false | boolean | 日历是否禁用。 |
disableDaysOutsideCurrentView | false | boolean | 是否禁用当前视图之外的日期。 |
fixedWeeks | false | boolean | 日历是否始终显示 6 周。 |
initialFocus | false | boolean | 如果为 true,日历挂载时将根据可见情况聚焦选定的日期、今天或月份的第一天。 |
isDateDisabled | Matcher | 返回日期是否被禁用的函数。 | |
isDateUnavailable | Matcher | 返回日期是否不可用的函数。 | |
locale | string | 用于格式化日期的区域设置。 | |
maxValue | DateValue | 可选择的最大日期。 | |
minValue | DateValue | 可选择的最小日期。 | |
modelValue | DateValue | DateValue[] | 日历的受控选中状态。 | |
multiple | false | boolean | 是否可以选择多个日期。 |
nextPage | ((placeholder: DateValue) => DateValue) | 返回日历下一页的函数。它在组件内部接收当前占位符作为参数。 | |
numberOfMonths | 1 | number | 一次显示的月份数量。 |
pagedNavigation | false | boolean | 此属性使“上一页”和“下一页”按钮根据一次显示的月份数量而不是一个月进行导航。 |
placeholder | DateValue | 占位符日期,用于在未选择日期时确定要显示的月份。 | |
preventDeselect | false | boolean | 是否阻止用户在未选择另一个日期的情况下取消选择日期。 |
prevPage | ((placeholder: DateValue) => DateValue) | 返回日历上一页的函数。它在组件内部接收当前占位符作为参数。 | |
readonly | false | boolean | 日历是否只读。 |
weekdayFormat | 'narrow' | 'narrow' | 'short' | 'long' | 用于通过 weekdays 插槽提供的星期几字符串的格式。 |
weekStartsOn | 0 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 日历开始的星期几。 |
发送事件
| 事件 | Payload | 描述 |
|---|---|---|
update:modelValue | [date: DateValue] | modelValue 更改时调用的事件处理程序。 |
update:placeholder | [date: DateValue] | placeholder 值更改时调用的事件处理程序。 |
默认插槽
| 插槽参数 | 插槽参数类型 | 描述 |
|---|---|---|
date | DateValue | 占位符的当前日期。 |
grid | Grid<DateValue>[] | 日期网格。 |
weekDays | string[] | 星期几。 |
weekStartsOn | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 周的开始。 |
locale | string | 日历区域设置。 |
fixedWeeks | boolean | 日历是否始终显示 6 周。 |
modelValue | DateValue | DateValue[] | undefined | 日历的当前日期。 |
方法 (Methods)
| 方法名称 | 类型 | 描述 |
|---|---|---|
isDateDisabled | Matcher | 返回日期是否被禁用的函数。 |
isDateUnavailable | Matcher | 返回日期是否不可用的函数。 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-readonly] | 只读时存在 |
[data-disabled] | 禁用时存在 |
[data-invalid] | 无效时存在 |
头部 (Header)
包含导航按钮和标题段。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'div' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
上一页按钮 (Prev Button)
日历导航按钮。它根据当前日历视图将日历向前导航一个月/年/十年。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'button' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 | |
prevPage | ((placeholder: DateValue) => DateValue) | 用于上一页的函数。覆盖 CalendarRoot 上设置的 prevPage 函数。 |
默认插槽
| 插槽参数 | 插槽参数类型 | 描述 |
|---|---|---|
disabled | boolean | 当前禁用状态 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-disabled] | 禁用时存在 |
下一页按钮 (Next Button)
日历导航按钮。它根据当前日历视图将日历向后导航一个月/年/十年。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'button' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 | |
nextPage | ((placeholder: DateValue) => DateValue) | 用于下一页的函数。覆盖 CalendarRoot 上设置的 nextPage 函数。 |
默认插槽
| 插槽参数 | 插槽参数类型 | 描述 |
|---|---|---|
disabled | boolean | 当前禁用状态 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-disabled] | 禁用时存在 |
标题 (Heading)
显示当前月份和年份的标题。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'div' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
默认插槽
| 插槽参数 | 插槽参数类型 | 描述 |
|---|---|---|
headingValue | string | 当前月份和年份。 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-disabled] | 禁用时存在 |
网格 (Grid)
用于包裹日历网格的容器。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'table' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-readonly] | 只读时存在 |
[data-disabled] | 禁用时存在 |
网格头部 (Grid Head)
用于包裹网格头部的容器。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'thead' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
网格体 (Grid Body)
用于包裹网格主体的容器。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'tbody' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
网格行 (Grid Row)
用于包裹网格行的容器。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'tr' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
头部单元格 (Head Cell)
用于包裹头部单元格的容器。用于显示星期几。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'th' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 |
单元格 (Cell)
用于包裹日历单元格的容器。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'td' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 | |
date* | DateValue | 单元格的日期值。 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-disabled] | 禁用时存在 |
单元格触发器 (Cell Trigger)
用于显示单元格日期的可交互容器。点击它会选择日期。
| 属性 | 默认值 | 类型 | 描述 |
|---|---|---|---|
as | 'div' | AsTag | Component | 此组件应渲染为的元素或组件。可通过 asChild 覆盖。 |
asChild | boolean | 将默认渲染的元素替换为作为子元素传递的元素,合并它们的 props 和行为。阅读我们的组合指南了解更多详情。 | |
day* | DateValue | 提供给单元格触发器的日期值。 | |
month* | DateValue | 渲染单元格的月份。 |
默认插槽
| 插槽参数 | 插槽参数类型 | 描述 |
|---|---|---|
dayValue | string | 当前日期。 |
disabled | boolean | 当前禁用状态。 |
selected | boolean | 当前选中状态。 |
today | boolean | 当前是否为今天。 |
outsideView | boolean | 当前是否在当前月份视图之外。 |
outsideVisibleView | boolean | 当前是否在日历可见月份之外。 |
unavailable | boolean | 当前是否不可用。 |
数据属性 (Data Attribute)
| 值 | 描述 |
|---|---|
[data-selected] | 选中时存在 |
[data-value] | 日期的 ISO 字符串值。 |
[data-disabled] | 禁用时存在 |
[data-unavailable] | 不可用时存在 |
[data-today] | 今天时存在 |
[data-outside-view] | 日期在当前月份视图之外时存在。 |
[data-outside-visible-view] | 日期在日历可见月份之外时存在。 |
[data-focused] | 聚焦时存在 |
示例 (Examples)
带年份递增的日历 (Calendar with Year Incrementation)
此示例展示了一个允许递增年份的日历。
<script setup lang="ts">
import type { DateValue } from '@internationalized/date'
import type { CalendarRootProps } from 'reka-ui'
import { Icon } from '@iconify/vue'
import { CalendarCell, CalendarCellTrigger, CalendarGrid, CalendarGridBody, CalendarGridHead, CalendarGridRow, CalendarHeadCell, CalendarHeader, CalendarHeading, CalendarNext, CalendarPrev, CalendarRoot } from 'reka-ui'
const isDateUnavailable: CalendarRootProps['isDateUnavailable'] = (date) => {
return date.day === 17 || date.day === 18
}
function pagingFunc(date: DateValue, sign: -1 | 1) {
if (sign === -1)
return date.subtract({ years: 1 })
return date.add({ years: 1 })
}
</script>
<template>
<CalendarRoot
v-slot="{ weekDays, grid }"
:is-date-unavailable="isDateUnavailable"
class="mt-6 rounded-xl bg-white p-4 shadow-md"
fixed-weeks
>
<CalendarHeader class="flex items-center justify-between">
<CalendarPrev
class="inline-flex items-center cursor-pointer text-black justify-center rounded-[9px] bg-transparent w-8 h-8 hover:bg-black hover:text-white active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
:prev-page="(date: DateValue) => pagingFunc(date, -1)"
>
<Icon
icon="radix-icons:double-arrow-left"
class="w-4 h-4"
/>
</CalendarPrev>
<CalendarPrev
class="inline-flex items-center cursor-pointer text-black justify-center rounded-[9px] bg-transparent w-8 h-8 hover:bg-black hover:text-white active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-left"
class="w-4 h-4"
/>
</CalendarPrev>
<CalendarHeading class="text-sm text-black font-medium" />
<CalendarNext
class="inline-flex items-center cursor-pointer justify-center text-black rounded-[9px] bg-transparent w-8 h-8 hover:bg-black hover:text-white active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-right"
class="w-4 h-4"
/>
</CalendarNext>
<CalendarNext
class="inline-flex items-center cursor-pointer justify-center text-black rounded-[9px] bg-transparent w-8 h-8 hover:bg-black hover:text-white active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
:next-page="(date: DateValue) => pagingFunc(date, 1)"
>
<Icon
icon="radix-icons:double-arrow-right"
class="w-4 h-4"
/>
</CalendarNext>
</CalendarHeader>
<div
class="flex flex-col space-y-4 pt-4 sm:flex-row sm:space-x-4 sm:space-y-0"
>
<CalendarGrid
v-for="month in grid"
:key="month.value.toString()"
class="w-full border-collapse select-none space-y-1"
>
<CalendarGridHead>
<CalendarGridRow class="mb-1 grid w-full grid-cols-7">
<CalendarHeadCell
v-for="day in weekDays"
:key="day"
class="rounded-md text-xs text-green-800"
>
{{ day }}
</CalendarHeadCell>
</CalendarGridRow>
</CalendarGridHead>
<CalendarGridBody class="grid">
<CalendarGridRow
v-for="(weekDates, index) in month.rows"
:key="`weekDate-${index}`"
class="grid grid-cols-7"
>
<CalendarCell
v-for="weekDate in weekDates"
:key="weekDate.toString()"
:date="weekDate"
class="relative text-center text-sm"
>
<CalendarCellTrigger
:day="weekDate"
:month="month.value"
class="relative flex items-center justify-center rounded-full whitespace-nowrap text-sm font-normal text-black w-8 h-8 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black data-[outside-view]:text-black/30 data-[selected]:!bg-green-900 data-[selected]:text-white hover:bg-green-500 data-[highlighted]:bg-green-500 data-[unavailable]:pointer-events-none data-[unavailable]:text-black/30 data-[unavailable]:line-through before:absolute before:top-[5px] before:hidden before:rounded-full before:w-1 before:h-1 before:bg-white data-[today]:before:block data-[today]:before:bg-green9 "
/>
</CalendarCell>
</CalendarGridRow>
</CalendarGridBody>
</CalendarGrid>
</div>
</CalendarRoot>
</template>
带有区域设置和日历系统选择的日历 (Calendar with Locale and Calendar System Selection)
此示例展示了一些可用的区域设置以及日历系统的显示方式。
<script setup lang="ts">
import { Icon } from '@iconify/vue'
import { createCalendar, getLocalTimeZone, toCalendar, today } from '@internationalized/date'
import { CalendarCell, CalendarCellTrigger, CalendarGrid, CalendarGridBody, CalendarGridHead, CalendarGridRow, CalendarHeadCell, CalendarHeader, CalendarHeading, CalendarNext, CalendarPrev, CalendarRoot, Label, SelectContent, SelectGroup, SelectItem, SelectItemIndicator, SelectItemText, SelectLabel, SelectPortal, SelectRoot, SelectScrollDownButton, SelectScrollUpButton, SelectSeparator, SelectTrigger, SelectValue, SelectViewport } from 'reka-ui'
import { computed, ref } from 'vue'
const preferences = [
{ locale: 'en-US', label: 'Default', ordering: 'gregory' },
{ label: 'Arabic (Algeria)', locale: 'ar-DZ', territories: 'DJ DZ EH ER IQ JO KM LB LY MA MR OM PS SD SY TD TN YE', ordering: 'gregory islamic islamic-civil islamic-tbla' },
{ label: 'Arabic (United Arab Emirates)', locale: 'ar-AE', territories: 'AE BH KW QA', ordering: 'gregory islamic-umalqura islamic islamic-civil islamic-tbla' },
{ label: 'Arabic (Egypt)', locale: 'AR-EG', territories: 'EG', ordering: 'gregory coptic islamic islamic-civil islamic-tbla' },
{ label: 'Arabic (Saudi Arabia)', locale: 'ar-SA', territories: 'SA', ordering: 'islamic-umalqura gregory islamic islamic-rgsa' },
{ label: 'Farsi (Iran)', locale: 'fa-IR', territories: 'IR', ordering: 'persian gregory islamic islamic-civil islamic-tbla' },
{ label: 'Farsi (Afghanistan)', locale: 'fa-AF', territories: 'AF IR', ordering: 'persian gregory islamic islamic-civil islamic-tbla' },
{ label: 'Amharic (Ethiopia)', locale: 'am-ET', territories: 'ET', ordering: 'gregory ethiopic ethioaa' },
{ label: 'Hebrew (Israel)', locale: 'he-IL', territories: 'IL', ordering: 'gregory hebrew islamic islamic-civil islamic-tbla' },
{ label: 'Hindi (India)', locale: 'hi-IN', territories: 'IN', ordering: 'gregory indian' },
{ label: 'Japanese (Japan)', locale: 'ja-JP', territories: 'JP', ordering: 'gregory japanese' },
{ label: 'Thai (Thailand)', locale: 'th-TH', territories: 'TH', ordering: 'buddhist gregory' },
{ label: 'Chinese (Taiwan)', locale: 'zh-TW', territories: 'TW', ordering: 'gregory roc chinese' },
]
const calendars = [
{ key: 'gregory', name: 'Gregorian' },
{ key: 'japanese', name: 'Japanese' },
{ key: 'buddhist', name: 'Buddhist' },
{ key: 'roc', name: 'Taiwan' },
{ key: 'persian', name: 'Persian' },
{ key: 'indian', name: 'Indian' },
{ key: 'islamic-umalqura', name: 'Islamic (Umm al-Qura)' },
{ key: 'islamic-civil', name: 'Islamic Civil' },
{ key: 'islamic-tbla', name: 'Islamic Tabular' },
{ key: 'hebrew', name: 'Hebrew' },
{ key: 'coptic', name: 'Coptic' },
{ key: 'ethiopic', name: 'Ethiopic' },
{ key: 'ethioaa', name: 'Ethiopic (Amete Alem)' },
]
const locale = ref(preferences[0].locale)
const calendar = ref(calendars[0].key)
const pref = computed(() => preferences.find(p => p.locale === locale.value))
const preferredCalendars = computed(() => pref.value ? pref.value.ordering.split(' ').map(p => calendars.find(c => c.key === p)).filter(Boolean) : [calendars[0]])
const otherCalendars = computed(() => calendars.filter(c => !preferredCalendars.value.some(p => p!.key === c.key)))
function updateLocale(newLocale: string) {
locale.value = newLocale
calendar.value = pref.value!.ordering.split(' ')[0]
}
const value = computed(() => toCalendar(today(getLocalTimeZone()), createCalendar(calendar.value)))
</script>
<template>
<div class="flex flex-col gap-4">
<Label class="text-white">Locale</Label>
<SelectRoot
v-model="locale"
@update:model-value="updateLocale"
>
<SelectTrigger
class="inline-flex min-w-[160px] items-center justify-between rounded-md px-[15px] text-xs leading-none h-[35px] gap-[5px] bg-white text-grass11 shadow-[0_2px_10px] shadow-black/10 hover:bg-mauve3 focus:shadow-[0_0_0_2px] focus:shadow-black data-[placeholder]:text-green-900 outline-none"
aria-label="Select a locale"
>
<SelectValue placeholder="Please select a locale">
{{ pref!.label }}
</SelectValue>
<Icon
icon="radix-icons:chevron-down"
class="h-3.5 w-3.5"
/>
</SelectTrigger>
<SelectPortal>
<SelectContent
class="min-w-[160px] bg-white rounded-md shadow-[0px_10px_38px_-10px_rgba(22,_23,_24,_0.35),_0px_10px_20px_-15px_rgba(22,_23,_24,_0.2)] will-change-[opacity,transform] data-[side=top]:animate-slideDownAndFade data-[side=right]:animate-slideLeftAndFade data-[side=bottom]:animate-slideUpAndFade data-[side=left]:animate-slideRightAndFade z-[100]"
:side-offset="5"
>
<SelectScrollUpButton class="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
<Icon icon="radix-icons:chevron-up" />
</SelectScrollUpButton>
<SelectViewport class="p-[5px]">
<SelectItem
v-for="(option, index) in preferences"
:key="index"
class="text-xs leading-none text-grass11 rounded-[3px] flex items-center h-[25px] pr-[35px] pl-[25px] relative select-none data-[disabled]:text-mauve8 data-[disabled]:pointer-events-none data-[highlighted]:outline-none data-[highlighted]:bg-green-900 data-[highlighted]:text-green-100"
:value="option.locale"
>
<SelectItemIndicator class="absolute left-0 w-[25px] inline-flex items-center justify-center">
<Icon icon="radix-icons:check" />
</SelectItemIndicator>
<SelectItemText>
{{ option.label }}
</SelectItemText>
</SelectItem>
</SelectViewport>
<SelectScrollDownButton class="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
<Icon icon="radix-icons:chevron-down" />
</SelectScrollDownButton>
</SelectContent>
</SelectPortal>
</SelectRoot>
<Label class="text-white">Calendar</Label>
<SelectRoot v-model="calendar">
<SelectTrigger
class="inline-flex min-w-[160px] items-center justify-between rounded-md px-[15px] text-xs leading-none h-[35px] gap-[5px] bg-white text-grass11 shadow-[0_2px_10px] shadow-black/10 hover:bg-mauve3 focus:shadow-[0_0_0_2px] focus:shadow-black data-[placeholder]:text-green-900 outline-none"
aria-label="Select a calendar"
>
<SelectValue placeholder="Please select a calendar">
{{ calendars.find(c => c.key === calendar)?.name }}
</SelectValue>
<Icon
icon="radix-icons:chevron-down"
class="h-3.5 w-3.5"
/>
</SelectTrigger>
<SelectPortal>
<SelectContent
class="min-w-[160px] bg-white rounded-md shadow-[0px_10px_38px_-10px_rgba(22,_23,_24,_0.35),_0px_10px_20px_-15px_rgba(22,_23,_24,_0.2)] will-change-[opacity,transform] data-[side=top]:animate-slideDownAndFade data-[side=right]:animate-slideLeftAndFade data-[side=bottom]:animate-slideUpAndFade data-[side=left]:animate-slideRightAndFade z-[100]"
:side-offset="5"
>
<SelectScrollUpButton class="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
<Icon icon="radix-icons:chevron-up" />
</SelectScrollUpButton>
<SelectViewport class="p-[5px]">
<SelectLabel class="px-[25px] text-xs leading-[25px] text-mauve11">
Preferred
</SelectLabel>
<SelectGroup>
<SelectItem
v-for="(option, index) in preferredCalendars"
:key="index"
class="text-xs leading-none text-grass-900 rounded-[3px] flex items-center h-[25px] pr-[35px] pl-[25px] relative select-none data-[disabled]:text-mauve8 data-[disabled]:pointer-events-none data-[highlighted]:outline-none data-[highlighted]:bg-green-900 data-[highlighted]:text-green1"
:value="option!.key"
>
<SelectItemIndicator class="absolute left-0 w-[25px] inline-flex items-center justify-center">
<Icon icon="radix-icons:check" />
</SelectItemIndicator>
<SelectItemText>
{{ option!.name }}
</SelectItemText>
</SelectItem>
</SelectGroup>
<SelectSeparator class="h-[1px] bg-green6 m-[5px]" />
<SelectLabel class="px-[25px] text-xs leading-[25px] text-mauve11">
Other
</SelectLabel>
<SelectGroup>
<SelectItem
v-for="(option, index) in otherCalendars"
:key="index"
class="text-xs leading-none text-grass11 rounded-[3px] flex items-center h-[25px] pr-[35px] pl-[25px] relative select-none data-[disabled]:text-mauve8 data-[disabled]:pointer-events-none data-[highlighted]:outline-none data-[highlighted]:bg-green-900 data-[highlighted]:text-green-100"
:value="option.key"
>
<SelectItemIndicator class="absolute left-0 w-[25px] inline-flex items-center justify-center">
<Icon icon="radix-icons:check" />
</SelectItemIndicator>
<SelectItemText>
{{ option.name }}
</SelectItemText>
</SelectItem>
</SelectGroup>
</SelectViewport>
<SelectScrollDownButton class="flex items-center justify-center h-[25px] bg-white text-violet11 cursor-default">
<Icon icon="radix-icons:chevron-down" />
</SelectScrollDownButton>
</SelectContent>
</SelectPortal>
</SelectRoot>
<CalendarRoot
v-slot="{ weekDays, grid }"
:model-value="value"
:locale="locale"
class="mt-6 rounded-xl bg-white p-4 shadow-md"
fixed-weeks
>
<CalendarHeader class="flex items-center justify-between">
<CalendarPrev
class="inline-flex items-center cursor-pointer text-black justify-center rounded-[9px] bg-transparent w-8 h-8 hover:bg-black hover:text-white active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-left"
class="w-4 h-4"
/>
</CalendarPrev>
<CalendarHeading class="text-sm text-black font-medium" />
<CalendarNext
class="inline-flex items-center cursor-pointer justify-center text-black rounded-[9px] bg-transparent w-8 h-8 hover:bg-black hover:text-white active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-right"
class="w-4 h-4"
/>
</CalendarNext>
</CalendarHeader>
<div
class="flex flex-col space-y-4 pt-4 sm:flex-row sm:space-x-4 sm:space-y-0"
>
<CalendarGrid
v-for="month in grid"
:key="month.value.toString()"
class="w-full border-collapse select-none space-y-1"
>
<CalendarGridHead>
<CalendarGridRow class="mb-1 grid w-full grid-cols-7">
<CalendarHeadCell
v-for="day in weekDays"
:key="day"
class="rounded-md text-xs text-green8"
>
{{ day }}
</CalendarHeadCell>
</CalendarGridRow>
</CalendarGridHead>
<CalendarGridBody class="grid">
<CalendarGridRow
v-for="(weekDates, index) in month.rows"
:key="`weekDate-${index}`"
class="grid grid-cols-7"
>
<CalendarCell
v-for="weekDate in weekDates"
:key="weekDate.toString()"
:date="weekDate"
class="relative text-center text-sm"
>
<CalendarCellTrigger
:day="weekDate"
:month="month.value"
class="relative flex items-center justify-center rounded-full whitespace-nowrap text-sm font-normal text-black w-8 h-8 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black data-[outside-view]:text-black/30 data-[selected]:!bg-green10 data-[selected]:text-white hover:bg-green-500 data-[highlighted]:bg-green-500 data-[unavailable]:pointer-events-none data-[unavailable]:text-black/30 data-[unavailable]:line-through before:absolute before:top-[5px] before:hidden before:rounded-full before:w-1 before:h-1 before:bg-white data-[today]:before:block data-[today]:before:bg-green-900 "
/>
</CalendarCell>
</CalendarGridRow>
</CalendarGridBody>
</CalendarGrid>
</div>
</CalendarRoot>
</div>
</template>
日历滑动手势导航 (Calendar swipe gesture navigation)
此组件演示了使用基于触摸的滑动手势进行直观的日历导航,这是一种用户友好的月份浏览方式。
<script setup lang="ts">
import { Icon } from '@iconify/vue'
import { CalendarDate } from '@internationalized/date'
import { usePointerSwipe } from '@vueuse/core'
import { CalendarCell, CalendarCellTrigger, CalendarGrid, CalendarGridBody, CalendarGridHead, CalendarGridRow, CalendarHeadCell, CalendarHeader, CalendarHeading, CalendarNext, CalendarPrev, CalendarRoot } from 'reka-ui'
import { onMounted, ref, useTemplateRef } from 'vue'
const calendarRef = useTemplateRef('calendarRef')
const date = ref(new CalendarDate(2023, 1, 1))
function nextPage() {
date.value = date.value.add({ months: 1 }).copy()
}
function prevPage() {
date.value = date.value.subtract({ months: 1 }).copy()
}
onMounted(() => {
if (calendarRef.value) {
usePointerSwipe(calendarRef.value.$el, {
onSwipeEnd(_e, direction) {
if (direction === 'none') {
// eslint-disable-next-line no-useless-return
return
}
else if (['down', 'right'].includes(direction)) {
prevPage()
}
else {
nextPage()
}
},
})
}
})
</script>
<template>
<CalendarRoot
ref="calendarRef"
v-slot="{ weekDays, grid }"
v-model:placeholder="date"
class="mt-6 rounded-xl bg-white p-4 shadow-sm border"
fixed-weeks
>
<CalendarHeader class="flex items-center justify-between">
<CalendarPrev
class="inline-flex items-center cursor-pointer text-black justify-center rounded-md bg-transparent w-7 h-7 hover:bg-stone-50 active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-left"
class="w-4 h-4"
/>
</CalendarPrev>
<CalendarHeading class="text-sm text-black font-medium" />
<CalendarNext
class="inline-flex items-center cursor-pointer justify-center text-black rounded-md bg-transparent w-7 h-7 hover:bg-stone-50 active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-right"
class="w-4 h-4"
/>
</CalendarNext>
</CalendarHeader>
<div
class="flex flex-col space-y-4 pt-4 sm:flex-row sm:space-x-4 sm:space-y-0"
>
<CalendarGrid
v-for="month in grid"
:key="month.value.toString()"
class="w-full border-collapse select-none space-y-1"
>
<CalendarGridHead>
<CalendarGridRow class="mb-1 grid w-full grid-cols-7">
<CalendarHeadCell
v-for="day in weekDays"
:key="day"
class="rounded-md text-xs text-green-800"
>
{{ day }}
</CalendarHeadCell>
</CalendarGridRow>
</CalendarGridHead>
<CalendarGridBody class="grid">
<CalendarGridRow
v-for="(weekDates, index) in month.rows"
:key="`weekDate-${index}`"
class="grid grid-cols-7"
>
<CalendarCell
v-for="weekDate in weekDates"
:key="weekDate.toString()"
:date="weekDate"
class="relative text-center text-sm"
>
<CalendarCellTrigger
:day="weekDate"
:month="month.value"
class="relative flex items-center justify-center rounded-full whitespace-nowrap text-sm font-normal text-black w-8 h-8 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black data-[outside-view]:text-black/30 data-[selected]:!bg-green-900 data-[selected]:text-white hover:bg-green-500 data-[highlighted]:bg-green-500 data-[unavailable]:pointer-events-none data-[unavailable]:text-black/30 data-[unavailable]:line-through before:absolute before:top-[5px] before:hidden before:rounded-full before:w-1 before:h-1 before:bg-white data-[today]:before:block data-[today]:before:bg-green-900 "
/>
</CalendarCell>
</CalendarGridRow>
</CalendarGridBody>
</CalendarGrid>
</div>
</CalendarRoot>
</template>
日历周数
此示例展示了如何使用 CalendarWeek 组件来显示每周的周数。
<script setup lang="ts">
import { Icon } from '@iconify/vue'
import { CalendarCell, CalendarCellTrigger, CalendarGrid, CalendarGridBody, CalendarGridHead, CalendarGridRow, CalendarHeadCell, CalendarHeader, CalendarHeading, CalendarNext, CalendarPrev, CalendarRoot } from 'reka-ui'
import { getWeekNumber } from 'reka-ui/date'
</script>
<template>
<CalendarRoot
v-slot="{ weekDays, grid }"
class="mt-6 rounded-xl bg-white p-4 shadow-sm border"
fixed-weeks
>
<CalendarHeader class="flex items-center justify-between">
<CalendarPrev
class="inline-flex items-center cursor-pointer text-black justify-center rounded-md bg-transparent w-7 h-7 hover:bg-stone-50 active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-left"
class="w-4 h-4"
/>
</CalendarPrev>
<CalendarHeading class="text-sm text-black font-medium" />
<CalendarNext
class="inline-flex items-center cursor-pointer justify-center text-black rounded-md bg-transparent w-7 h-7 hover:bg-stone-50 active:scale-98 active:transition-all focus:shadow-[0_0_0_2px] focus:shadow-black"
>
<Icon
icon="radix-icons:chevron-right"
class="w-4 h-4"
/>
</CalendarNext>
</CalendarHeader>
<div
class="flex flex-col space-y-4 pt-4 sm:flex-row sm:space-x-4 sm:space-y-0"
>
<CalendarGrid
v-for="month in grid"
:key="month.value.toString()"
class="w-full border-collapse select-none space-y-1"
>
<CalendarGridHead>
<CalendarGridRow class="mb-1 grid w-full grid-cols-8">
<CalendarHeadCell
class="rounded-md text-xs text-green-800"
>
Wk
</CalendarHeadCell>
<CalendarHeadCell
v-for="day in weekDays"
:key="day"
class="rounded-md text-xs text-green-800"
>
{{ day }}
</CalendarHeadCell>
</CalendarGridRow>
</CalendarGridHead>
<CalendarGridBody class="grid">
<CalendarGridRow
v-for="(weekDates, index) in month.rows"
:key="`weekDate-${index}`"
class="grid grid-cols-8"
>
<div
class="text-sm flex justify-center items-center"
>
{{ getWeekNumber(weekDates[0]) }}
</div>
<CalendarCell
v-for="weekDate in weekDates"
:key="weekDate.toString()"
:date="weekDate"
class="relative text-center text-sm"
>
<CalendarCellTrigger
:day="weekDate"
:month="month.value"
class="relative flex items-center justify-center rounded-full whitespace-nowrap text-sm font-normal text-black w-8 h-8 outline-none focus:shadow-[0_0_0_2px] focus:shadow-black data-[outside-view]:text-black/30 data-[selected]:!bg-green-900 data-[selected]:text-white hover:bg-green-500 data-[highlighted]:bg-green-500 data-[unavailable]:pointer-events-none data-[unavailable]:text-black/30 data-[unavailable]:line-through before:absolute before:top-[5px] before:hidden before:rounded-full before:w-1 before:h-1 before:bg-white data-[today]:before:block data-[today]:before:bg-green-900 "
/>
</CalendarCell>
</CalendarGridRow>
</CalendarGridBody>
</CalendarGrid>
</div>
</CalendarRoot>
</template>
可访问性 (Accessibility)
键盘交互 (Keyboard Interactions)
| 按键 | 描述 |
|---|---|
Tab | 当焦点移动到日历上时,聚焦第一个导航按钮。 |
Space | 当焦点位于 CalendarNext 或 CalendarPrev 上时,导航日历。否则,选择日期。 |
Enter | 当焦点位于 CalendarNext 或 CalendarPrev 上时,导航日历。否则,选择日期。 |
ArrowLeft ArrowRight ArrowUp ArrowDown | 当焦点位于 CalendarCellTrigger 上时,导航日期,必要时更改月份/年份/十年。 |