2019-05-26 13:09:06 +08:00
|
|
|
---
|
|
|
|
order: 4
|
|
|
|
title:
|
|
|
|
zh-CN: 自定义头部
|
|
|
|
en-US: Customize Header
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
自定义日历头部内容。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Customize Calendar header content.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2022-05-23 14:37:16 +08:00
|
|
|
import { Calendar, Col, Radio, Row, Select, Typography } from 'antd';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { CalendarMode } from 'antd/lib/calendar/generateCalendar';
|
|
|
|
import type { Moment } from 'moment';
|
2022-05-23 14:37:16 +08:00
|
|
|
import React from 'react';
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const onPanelChange = (value: Moment, mode: CalendarMode) => {
|
|
|
|
console.log(value.format('YYYY-MM-DD'), mode);
|
|
|
|
};
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<div className="site-calendar-customize-header-wrapper">
|
|
|
|
<Calendar
|
|
|
|
fullscreen={false}
|
|
|
|
headerRender={({ value, type, onChange, onTypeChange }) => {
|
|
|
|
const start = 0;
|
|
|
|
const end = 12;
|
|
|
|
const monthOptions = [];
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const current = value.clone();
|
|
|
|
const localeData = value.localeData();
|
|
|
|
const months = [];
|
|
|
|
for (let i = 0; i < 12; i++) {
|
|
|
|
current.month(i);
|
|
|
|
months.push(localeData.monthsShort(current));
|
|
|
|
}
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
for (let index = start; index < end; index++) {
|
|
|
|
monthOptions.push(
|
|
|
|
<Select.Option className="month-item" key={`${index}`}>
|
|
|
|
{months[index]}
|
|
|
|
</Select.Option>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const month = value.month();
|
2019-05-26 13:09:06 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const year = value.year();
|
|
|
|
const options = [];
|
|
|
|
for (let i = year - 10; i < year + 10; i += 1) {
|
|
|
|
options.push(
|
|
|
|
<Select.Option key={i} value={i} className="year-item">
|
|
|
|
{i}
|
|
|
|
</Select.Option>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div style={{ padding: 8 }}>
|
|
|
|
<Typography.Title level={4}>Custom header</Typography.Title>
|
|
|
|
<Row gutter={8}>
|
|
|
|
<Col>
|
|
|
|
<Radio.Group
|
|
|
|
size="small"
|
|
|
|
onChange={e => onTypeChange(e.target.value)}
|
|
|
|
value={type}
|
|
|
|
>
|
|
|
|
<Radio.Button value="month">Month</Radio.Button>
|
|
|
|
<Radio.Button value="year">Year</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Col>
|
|
|
|
<Col>
|
|
|
|
<Select
|
|
|
|
size="small"
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
className="my-year-select"
|
|
|
|
onChange={newYear => {
|
|
|
|
const now = value.clone().year(Number(newYear));
|
|
|
|
onChange(now);
|
|
|
|
}}
|
|
|
|
value={String(year)}
|
|
|
|
>
|
|
|
|
{options}
|
|
|
|
</Select>
|
|
|
|
</Col>
|
|
|
|
<Col>
|
|
|
|
<Select
|
|
|
|
size="small"
|
|
|
|
dropdownMatchSelectWidth={false}
|
|
|
|
value={String(month)}
|
|
|
|
onChange={selectedMonth => {
|
|
|
|
const newValue = value.clone();
|
|
|
|
newValue.month(parseInt(selectedMonth, 10));
|
|
|
|
onChange(newValue);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{monthOptions}
|
|
|
|
</Select>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
</div>
|
2019-05-26 13:09:06 +08:00
|
|
|
);
|
2022-05-19 09:46:26 +08:00
|
|
|
}}
|
|
|
|
onPanelChange={onPanelChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|
2019-05-26 13:09:06 +08:00
|
|
|
```
|
2019-12-25 15:04:41 +08:00
|
|
|
|
|
|
|
```css
|
|
|
|
.site-calendar-customize-header-wrapper {
|
|
|
|
width: 300px;
|
2020-03-27 20:02:14 +08:00
|
|
|
border: 1px solid #f0f0f0;
|
|
|
|
border-radius: 2px;
|
2019-12-25 15:04:41 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
<style>
|
|
|
|
[data-theme="dark"] .site-calendar-customize-header-wrapper {
|
|
|
|
border: 1px solid #303030;
|
|
|
|
}
|
|
|
|
</style>
|