2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
|
|
|
|
category: Components
|
2022-11-09 12:28:04 +08:00
|
|
|
|
group: 数据展示
|
2016-09-21 11:28:38 +08:00
|
|
|
|
title: Calendar
|
2024-03-22 14:22:42 +08:00
|
|
|
|
subtitle: 日历
|
|
|
|
|
description: 按照日历形式展示数据的容器。
|
2022-11-30 20:14:41 +08:00
|
|
|
|
cover: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*nF6_To7pDSAAAAAAAAAAAAAADrJ8AQ/original
|
2023-02-09 22:17:31 +08:00
|
|
|
|
coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*-p-wQLik200AAAAAAAAAAAAADrJ8AQ/original
|
2016-03-31 14:17:09 +08:00
|
|
|
|
---
|
2015-11-11 00:15:12 +08:00
|
|
|
|
|
|
|
|
|
## 何时使用
|
|
|
|
|
|
2015-11-13 18:20:22 +08:00
|
|
|
|
当数据是日期或按照日期划分时,例如日程、课表、价格日历等,农历等。目前支持年/月切换。
|
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
|
## 代码演示
|
|
|
|
|
|
2022-11-17 17:31:26 +08:00
|
|
|
|
<!-- prettier-ignore -->
|
2023-03-21 13:00:34 +08:00
|
|
|
|
<code src="./demo/basic.tsx" clientOnly>基本</code>
|
|
|
|
|
<code src="./demo/notice-calendar.tsx" clientOnly>通知事项日历</code>
|
|
|
|
|
<code src="./demo/card.tsx" clientOnly>卡片模式</code>
|
|
|
|
|
<code src="./demo/select.tsx" clientOnly>选择功能</code>
|
2023-08-17 17:12:51 +08:00
|
|
|
|
<code src="./demo/lunar.tsx" clientOnly>农历日历</code>
|
2023-03-21 13:00:34 +08:00
|
|
|
|
<code src="./demo/customize-header.tsx" clientOnly>自定义头部</code>
|
2023-05-18 16:12:41 +08:00
|
|
|
|
<code src="./demo/component-token.tsx" debug>组件 Token</code>
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
2015-11-11 00:15:12 +08:00
|
|
|
|
## API
|
|
|
|
|
|
2023-08-08 18:27:48 +08:00
|
|
|
|
通用属性参考:[通用属性](/docs/react/common-props)
|
|
|
|
|
|
2022-11-23 11:06:20 +08:00
|
|
|
|
**注意**:Calendar 部分 locale 是从 value 中读取,所以请先正确设置 dayjs 的 locale。
|
2017-02-23 15:57:05 +08:00
|
|
|
|
|
|
|
|
|
```jsx
|
2017-09-26 23:12:47 +08:00
|
|
|
|
// 默认语言为 en-US,所以如果需要使用其他语言,推荐在入口文件全局设置 locale
|
2022-02-14 14:40:40 +08:00
|
|
|
|
// import dayjs from 'dayjs';
|
|
|
|
|
// import 'dayjs/locale/zh-cn';
|
|
|
|
|
// dayjs.locale('zh-cn');
|
2017-02-23 15:57:05 +08:00
|
|
|
|
|
2015-11-13 19:56:34 +08:00
|
|
|
|
<Calendar
|
|
|
|
|
dateCellRender={dateCellRender}
|
|
|
|
|
monthCellRender={monthCellRender}
|
2015-11-15 21:40:01 +08:00
|
|
|
|
onPanelChange={onPanelChange}
|
2017-03-03 15:26:23 +08:00
|
|
|
|
onSelect={onSelect}
|
2015-11-13 19:56:34 +08:00
|
|
|
|
/>
|
2015-11-11 00:15:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-05-26 13:09:06 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 默认值 | 版本 |
|
|
|
|
|
| --- | --- | --- | --- | --- |
|
2023-04-03 11:21:24 +08:00
|
|
|
|
| dateCellRender | 自定义渲染日期单元格,返回内容会被追加到单元格,>= 5.4.0 请用 `cellRender` | function(date: Dayjs): ReactNode | - | < 5.4.0 |
|
|
|
|
|
| cellRender | 自定义单元格的内容 | function(current: dayjs, today: dayjs, info: { originNode: React.ReactElement,today: DateType, range?: 'start' \| 'end', type: PanelMode, locale?: Locale, subType?: 'hour' \| 'minute' \| 'second' \| 'meridiem' }) => React.ReactNode | - | 5.4.0 |
|
|
|
|
|
| dateFullCellRender | 自定义渲染日期单元格,返回内容覆盖单元格,>= 5.4.0 请用 `fullCellRender` | function(date: Dayjs): ReactNode | - | < 5.4.0 |
|
|
|
|
|
| fullCellRender | 自定义单元格的内容 | function(current: dayjs, today: dayjs, info: { originNode: React.ReactElement,today: DateType, range?: 'start' \| 'end', type: PanelMode, locale?: Locale, subType?: 'hour' \| 'minute' \| 'second' \| 'meridiem' }) => React.ReactNode | - | 5.4.0 |
|
2022-06-13 14:03:43 +08:00
|
|
|
|
| defaultValue | 默认展示的日期 | [dayjs](https://day.js.org/) | - | |
|
|
|
|
|
| disabledDate | 不可选择的日期,参数为当前 `value`,注意使用时[不要直接修改](https://github.com/ant-design/ant-design/issues/30987) | (currentDate: Dayjs) => boolean | - | |
|
2019-05-26 13:09:06 +08:00
|
|
|
|
| fullscreen | 是否全屏显示 | boolean | true | |
|
2022-06-13 14:03:43 +08:00
|
|
|
|
| headerRender | 自定义头部内容 | function(object:{value: Dayjs, type: string, onChange: f(), onTypeChange: f()}) | - | |
|
2020-07-01 22:37:55 +08:00
|
|
|
|
| locale | 国际化配置 | object | [(默认配置)](https://github.com/ant-design/ant-design/blob/master/components/date-picker/locale/example.json) | |
|
|
|
|
|
| mode | 初始模式 | `month` \| `year` | `month` | |
|
2023-04-03 11:21:24 +08:00
|
|
|
|
| monthCellRender | 自定义渲染月单元格,返回内容会被追加到单元格,>= 5.4.0 请用 `cellRender` | function(date: Dayjs): ReactNode | - | < 5.4.0 |
|
|
|
|
|
| monthFullCellRender | 自定义渲染月单元格,返回内容覆盖单元格,>= 5.4.0 请用 `fullCellRender` | function(date: Dayjs): ReactNode | - | < 5.4.0 |
|
2022-06-13 14:03:43 +08:00
|
|
|
|
| validRange | 设置可以显示的日期 | \[[dayjs](https://day.js.org/), [dayjs](https://day.js.org/)] | - | |
|
|
|
|
|
| value | 展示日期 | [dayjs](https://day.js.org/) | - | |
|
|
|
|
|
| onChange | 日期变化回调 | function(date: Dayjs) | - | |
|
|
|
|
|
| onPanelChange | 日期面板变化回调 | function(date: Dayjs, mode: string) | - | |
|
2023-05-17 21:18:51 +08:00
|
|
|
|
| onSelect | 选择日期回调,包含来源信息 | function(date: Dayjs, info: { source: 'year' \| 'month' \| 'date' \| 'customize' }) | - | `info`: 5.6.0 |
|
2020-01-02 16:05:59 +08:00
|
|
|
|
|
2023-08-30 23:02:35 +08:00
|
|
|
|
## 主题变量(Design Token)
|
2023-04-11 10:25:24 +08:00
|
|
|
|
|
|
|
|
|
<ComponentTokenTable component="Calendar"></ComponentTokenTable>
|
|
|
|
|
|
2020-01-02 16:05:59 +08:00
|
|
|
|
## FAQ
|
|
|
|
|
|
2022-07-12 17:09:43 +08:00
|
|
|
|
### 如何在 Calendar 中使用自定义日期库
|
2022-07-12 13:10:20 +08:00
|
|
|
|
|
2022-12-22 14:12:26 +08:00
|
|
|
|
参考 [使用自定义日期库](/docs/react/use-custom-date-library#calendar)。
|
2022-07-12 13:10:20 +08:00
|
|
|
|
|
|
|
|
|
### 如何给日期类组件配置国际化?
|
|
|
|
|
|
2022-12-13 15:49:05 +08:00
|
|
|
|
参考 [如何给日期类组件配置国际化](/components/date-picker-cn#%E5%9B%BD%E9%99%85%E5%8C%96%E9%85%8D%E7%BD%AE)。
|
2022-07-12 13:10:20 +08:00
|
|
|
|
|
|
|
|
|
### 为什么时间类组件的国际化 locale 设置不生效?
|
|
|
|
|
|
2022-12-22 14:12:26 +08:00
|
|
|
|
参考 FAQ [为什么时间类组件的国际化 locale 设置不生效?](/docs/react/faq#为什么时间类组件的国际化-locale-设置不生效)。
|
2023-05-17 21:18:51 +08:00
|
|
|
|
|
|
|
|
|
### 如何仅获取来自面板点击的日期?
|
|
|
|
|
|
|
|
|
|
`onSelect` 事件提供额外的来源信息,你可以通过 `info.source` 来判断来源:
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
<Calendar
|
|
|
|
|
onSelect={(date, { source }) => {
|
|
|
|
|
if (source === 'date') {
|
|
|
|
|
console.log('Panel Select:', source);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
```
|