- 手风琴
- 提示
- 警告对话框
- 宽高比
- 头像
- 徽章
- 面包屑导航
- 按钮
- 按钮组
- 日历 Calendar
- 卡片
- Carousel
- 图表 Chart
- 复选框
- 折叠面板
- 组合框
- 命令
- 上下文菜单
- 数据表格 Data Table
- 日期选择器 Date Picker
- 对话框 Dialog
- 方向
- 抽屉
- 下拉菜单
- Empty
- 字段
- 悬停卡片
- 输入
- 输入组
- 输入 OTP
- 项目
- Kbd
- 标签
- 菜单栏
- 原生选择框
- 导航菜单 Navigation Menu
- 分页
- 弹出框
- 进度 Progress
- 单选框组
- 可调整大小
- 滚动区域 Scroll Area
- 选择框
- 分隔符 Separator
- 侧边栏 Sheet
- 侧边栏 Sidebar
- 骨架屏
- 滑块
- Sonner
- 加载指示器 Spinner
- 开关
- 表格
- 标签页 Tabs
- 文本域
- 吐司
- 切换按钮 Toggle
- 切换组
- 提示 Tooltip
- 排版
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"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 构建。
Date Picker
您可以使用 <Calendar> 组件构建日期选择器。更多信息请参阅 日期选择器 页面。
波斯 / 希吉拉历 / 贾拉利历
要使用波斯历,请编辑 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"选定日期(带时区)
日历组件接受一个 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 属性设置为用户的本地时区。
为什么是客户端? 时区是通过在 useEffect 中使用 Intl.DateTimeFormat().resolvedOptions().timeZone 来检测的,以确保与服务器端渲染的兼容性。在渲染期间检测时区会导致水合不匹配,因为服务器和客户端可能处于不同的时区。
示例
基础
基础日历组件。我们使用了 className="rounded-lg border" 来样式化日历。
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client"
import { Calendar } from "@/components/ui/calendar"范围日历
使用 mode="range" 属性启用范围选择。
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"月份和年份选择器
使用 captionLayout="dropdown" 显示月份和年份下拉框。
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client"
import { Calendar } from "@/components/ui/calendar"预设
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"日期和时间选择器
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"已预订日期
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"自定义单元格大小
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
"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 属性显示周数。
| Su | Mo | Tu | We | Th | Fr | Sa | |
|---|---|---|---|---|---|---|---|
06 | |||||||
07 | |||||||
08 | |||||||
09 |
"use client"
import * as React from "react"RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
请参阅 Hijri Guide 以启用波斯历 / 希吉拉历 / 贾拉利历。
| أحد | اثنين | ثلاثاء | أربعاء | خميس | جمعة | سبت |
|---|---|---|---|---|---|---|
"use client"
import * as React from "react"使用 RTL 时,从 react-day-picker/locale 导入 locale,并向 Calendar 组件传递 locale 和 dir 属性:
import { arSA } from "react-day-picker/locale"
;<Calendar
mode="single"
selected={date}
onSelect={setDate}
locale={arSA}
dir="rtl"
/>API 参考
更多关于 Calendar 组件 API 的信息,请参见 React DayPicker 文档。
变更日志
RTL 支持
如果您正在从以前版本的 Calendar 组件升级,您需要应用以下更新以添加本地化支持:
导入 Locale 类型。
将 Locale 添加到从 react-day-picker 的导入中:
import {
DayPicker,
getDefaultClassNames,
type DayButton,
+ type Locale,
} from "react-day-picker"在 Calendar 组件中添加 locale 属性。
将 locale 属性添加到组件的参数:
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 类。
使用逻辑属性替换方向性类以更好地支持 RTL:
// 在 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)
// 在 range_start 的 classNames 中:
- rounded-l-(--cell-radius) ... after:right-0
+ rounded-s-(--cell-radius) ... after:end-0
// 在 range_end 的 classNames 中:
- rounded-r-(--cell-radius) ... after:left-0
+ rounded-e-(--cell-radius) ... after:start-0
// 在 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} />