- 手风琴 (Accordion)
- 警示框 (Alert)
- 对话框 (Alert Dialog)
- 宽高比 (Aspect Ratio)
- 头像 (Avatar)
- 徽章 (Badge)
- 面包屑 (Breadcrumb)
- 按钮 (Button)
- 按钮组 (Button Group)
- 日历 (Calendar)
- 卡片 (Card)
- 轮播 (Carousel)
- 图表 (Chart)
- 复选框 (Checkbox)
- 折叠面板 (Collapsible)
- 组合框 (Combobox)
- 命令栏 (Command)
- 上下文菜单 (Context Menu)
- 数据表格 (Data Table)
- 日期选择器 (Date Picker)
- 对话框 (Dialog)
- 方向 (Direction)
- 抽屉 (Drawer)
- 下拉菜单 (Dropdown Menu)
- 空状态 (Empty)
- 字段 (Field)
- 悬浮卡片 (Hover Card)
- 输入框 (Input)
- 输入框组 (Input Group)
- OTP 输入框 (Input OTP)
- 项目 (Item)
- 快捷键 (Kbd)
- 标签 (Label)
- 菜单栏 (Menubar)
- 原生选择框 (Native Select)
- 导航菜单 (Navigation Menu)
- 分页 (Pagination)
- 气泡卡片 (Popover)
- 进度条 (Progress)
- 单选组 (Radio Group)
- 可调节大小 (Resizable)
- 滚动区域 (Scroll Area)
- 选择框 (Select)
- 分隔线 (Separator)
- 侧边栏 (Sheet)
- 导航侧边栏 (Sidebar)
- 骨架屏 (Skeleton)
- 滑块 (Slider)
- Sonner (吐司通知)
- 加载器 (Spinner)
- 开关 (Switch)
- 表格 (Table)
- 标签页 (Tabs)
- 文本域 (Textarea)
- 吐司 (Toast)
- 切换按钮 (Toggle)
- 切换组 (Toggle Group)
- 文字提示 (Tooltip)
- 排版 (Typography)
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"安装
pnpm dlx shadcn@latest add calendar
用法
import { Calendar } from "@/components/ui/calendar"const [date, setDate] = React.useState<Date | undefined>(new Date())
return (
<Calendar
mode="single"
selected={date}
onSelect={setDate}
className="rounded-lg border"
/>
)欲了解更多信息,请参阅 React DayPicker 文档。
关于
Calendar 组件构建于 React DayPicker 之上。
日期选择器
你可以使用 <Calendar> 组件来构建日期选择器。更多信息请参阅 日期选择器 (Date Picker) 页面。
波斯历 / 回历 / Jalali 日历
若要使用波斯历,请编辑 components/ui/calendar.tsx,将 react-day-picker 替换为 react-day-picker/persian。
- import { DayPicker } from "react-day-picker"
+ import { DayPicker } from "react-day-picker/persian"| 周六 | 周日 | 周一 | 周二 | 周三 | 周四 | 周五 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"所选日期(含时区)
Calendar 组件接受 timeZone 属性,以确保日期在用户的本地时区中显示和选择。
export function CalendarWithTimezone() {
const [date, setDate] = React.useState<Date | undefined>(undefined)
const [timeZone, setTimeZone] = React.useState<string | undefined>(undefined)
React.useEffect(() => {
setTimeZone(Intl.DateTimeFormat().resolvedOptions().timeZone)
}, [])
return (
<Calendar
mode="single"
selected={date}
onSelect={setDate}
timeZone={timeZone}
/>
)
}注意: 如果您发现所选日期存在偏移(例如,选择 20 号却高亮了 19 号),请确保 timeZone 属性已设置为用户的本地时区。
为何采用客户端检测? 时区是通过 Intl.DateTimeFormat().resolvedOptions().timeZone 在 useEffect 中检测到的,以确保与服务端渲染 (SSR) 的兼容性。在渲染期间检测时区会导致水合 (hydration) 不匹配,因为服务器和客户端可能处于不同的时区。
示例
基础用法
一个基础日历组件。我们使用了 className="rounded-lg border" 来设置日历样式。
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import { Calendar } from "@/components/ui/calendar"范围日历 (Range Calendar)
使用 mode="range" 属性来启用范围选择。
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"月份和年份选择器
使用 captionLayout="dropdown" 来显示月份和年份的下拉菜单。
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import { Calendar } from "@/components/ui/calendar"预设
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"日期与时间选择器
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"已预订日期
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"自定义单元格大小
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"您可以使用 --cell-size CSS 变量自定义日历单元格的大小。您也可以通过使用针对断点的值使其具备响应式特性。
<Calendar
mode="single"
selected={date}
onSelect={setDate}
className="rounded-lg border [--cell-size:--spacing(11)] md:[--cell-size:--spacing(12)]"
/>或者使用固定值
<Calendar
mode="single"
selected={date}
onSelect={setDate}
className="rounded-lg border [--cell-size:2.75rem] md:[--cell-size:3rem]"
/>周数
使用 showWeekNumber 来显示周数。
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 | |
|---|---|---|---|---|---|---|---|
06 | |||||||
07 | |||||||
08 | |||||||
09 |
"use client"
import * as React from "react"RTL (从右至左)
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
另请参阅 回历指南 以了解如何启用波斯历/回历/Jalali 日历。
| 周日 | 周一 | 周二 | 周三 | 周四 | 周五 | 周六 |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"当使用 RTL(从右向左)布局时,请从 react-day-picker/locale 导入 locale,并将 locale 和 dir 属性传递给 Calendar 组件。
import { arSA } from "react-day-picker/locale"
;<Calendar
mode="single"
selected={date}
onSelect={setDate}
locale={arSA}
dir="rtl"
/>API 参考
请参阅 React DayPicker 文档,获取关于 Calendar 组件的更多信息。
更新日志
RTL 支持
如果您是从 Calendar 组件的旧版本升级而来,您需要执行以下更新以支持区域设置 (locale):
导入 Locale 类型。
将 Locale 添加到您从 react-day-picker 的导入中。
import {
DayPicker,
getDefaultClassNames,
type DayButton,
+ type Locale,
} from "react-day-picker"为 Calendar 组件添加 locale 属性。
将 locale 属性添加到组件的 props 中。
function Calendar({
className,
classNames,
showOutsideDays = true,
captionLayout = "label",
buttonVariant = "ghost",
+ locale,
formatters,
components,
...props
}: React.ComponentProps<typeof DayPicker> & {
buttonVariant?: React.ComponentProps<typeof Button>["variant"]
}) {将 locale 传递给 DayPicker。
将 locale 属性传递给 DayPicker 组件。
<DayPicker
showOutsideDays={showOutsideDays}
className={cn(...)}
captionLayout={captionLayout}
+ locale={locale}
formatters={{
formatMonthDropdown: (date) =>
- date.toLocaleString("default", { month: "short" }),
+ date.toLocaleString(locale?.code, { month: "short" }),
...formatters,
}}更新 CalendarDayButton 以接受 locale。
更新 CalendarDayButton 组件签名并传递 locale。
function CalendarDayButton({
className,
day,
modifiers,
+ locale,
...props
- }: React.ComponentProps<typeof DayButton>) {
+ }: React.ComponentProps<typeof DayButton> & { locale?: Partial<Locale> }) {更新 CalendarDayButton 中的日期格式化方式。
在日期格式化中使用 locale?.code。
<Button
variant="ghost"
size="icon"
- data-day={day.date.toLocaleDateString()}
+ data-day={day.date.toLocaleDateString(locale?.code)}
...
/>将 locale 传递给 DayButton 组件。
更新 DayButton 组件的调用,以传递 locale 属性。
components={{
...
- DayButton: CalendarDayButton,
+ DayButton: ({ ...props }) => (
+ <CalendarDayButton locale={locale} {...props} />
+ ),
...
}}更新 RTL 感知 CSS 类。
用逻辑属性替换方向性 CSS 类,以获得更好的 RTL 支持。
// In the day classNames:
- [&:last-child[data-selected=true]_button]:rounded-r-(--cell-radius)
+ [&:last-child[data-selected=true]_button]:rounded-e-(--cell-radius)
- [&:nth-child(2)[data-selected=true]_button]:rounded-l-(--cell-radius)
+ [&:nth-child(2)[data-selected=true]_button]:rounded-s-(--cell-radius)
- [&:first-child[data-selected=true]_button]:rounded-l-(--cell-radius)
+ [&:first-child[data-selected=true]_button]:rounded-s-(--cell-radius)
// In range_start classNames:
- rounded-l-(--cell-radius) ... after:right-0
+ rounded-s-(--cell-radius) ... after:end-0
// In range_end classNames:
- rounded-r-(--cell-radius) ... after:left-0
+ rounded-e-(--cell-radius) ... after:start-0
// In CalendarDayButton className:
- data-[range-end=true]:rounded-r-(--cell-radius)
+ data-[range-end=true]:rounded-e-(--cell-radius)
- data-[range-start=true]:rounded-l-(--cell-radius)
+ data-[range-start=true]:rounded-s-(--cell-radius)应用这些更改后,您可以使用 locale 属性来提供特定于区域设置的格式。
import { enUS } from "react-day-picker/locale"
;<Calendar mode="single" selected={date} onSelect={setDate} locale={enUS} />