Calendar
Container for displaying data in calendar form.
When To Use#
When data is in the form of dates, such as schedules, timetables, prices calendar, lunar calendar. This component also supports Year/Month switch.
Examples
2022
Jul
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 01 | 02 |
03 | 04 | 05 | 06 | 07 | 08 | 09 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 01 | 02 | 03 | 04 | 05 | 06 |
TypeScript
JavaScript
import { Calendar } from 'infrad';
import type { CalendarMode } from 'infrad/lib/calendar/generateCalendar';
import type { Moment } from 'moment';
import React from 'react';
const App: React.FC = () => {
const onPanelChange = (value: Moment, mode: CalendarMode) => {
console.log(value.format('YYYY-MM-DD'), mode);
};
return <Calendar onPanelChange={onPanelChange} />;
};
export default App;
2022
Jul
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 01 | 02 |
03 | 04 | 05 | 06 | 07 | 08
| 09 |
10
| 11 | 12 | 13 | 14 | 15
| 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 01 | 02 | 03 | 04 | 05 | 06 |
TypeScript
JavaScript
import type { BadgeProps } from 'infrad';
import { Badge, Calendar } from 'infrad';
import type { Moment } from 'moment';
import React from 'react';
const getListData = (value: Moment) => {
let listData;
switch (value.date()) {
case 8:
listData = [
{ type: 'warning', content: 'This is warning event.' },
{ type: 'success', content: 'This is usual event.' },
];
break;
case 10:
listData = [
{ type: 'warning', content: 'This is warning event.' },
{ type: 'success', content: 'This is usual event.' },
{ type: 'error', content: 'This is error event.' },
];
break;
case 15:
listData = [
{ type: 'warning', content: 'This is warning event' },
{ type: 'success', content: 'This is very long usual event。。....' },
{ type: 'error', content: 'This is error event 1.' },
{ type: 'error', content: 'This is error event 2.' },
{ type: 'error', content: 'This is error event 3.' },
{ type: 'error', content: 'This is error event 4.' },
];
break;
default:
}
return listData || [];
};
const getMonthData = (value: Moment) => {
if (value.month() === 8) {
return 1394;
}
};
const App: React.FC = () => {
const monthCellRender = (value: Moment) => {
const num = getMonthData(value);
return num ? (
<div className="notes-month">
<section>{num}</section>
<span>Backlog number</span>
</div>
) : null;
};
const dateCellRender = (value: Moment) => {
const listData = getListData(value);
return (
<ul className="events">
{listData.map(item => (
<li key={item.content}>
<Badge status={item.type as BadgeProps['status']} text={item.content} />
</li>
))}
</ul>
);
};
return <Calendar dateCellRender={dateCellRender} monthCellRender={monthCellRender} />;
};
export default App;
.events {
margin: 0;
padding: 0;
list-style: none;
}
.events .ant-badge-status {
width: 100%;
overflow: hidden;
font-size: 12px;
white-space: nowrap;
text-overflow: ellipsis;
}
.notes-month {
font-size: 28px;
text-align: center;
}
.notes-month section {
font-size: 28px;
}
2022
Jul
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 01 | 02 |
03 | 04 | 05 | 06 | 07 | 08 | 09 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 01 | 02 | 03 | 04 | 05 | 06 |
TypeScript
JavaScript
import { Calendar } from 'infrad';
import type { CalendarMode } from 'infrad/lib/calendar/generateCalendar';
import type { Moment } from 'moment';
import React from 'react';
const App: React.FC = () => {
const onPanelChange = (value: Moment, mode: CalendarMode) => {
console.log(value.format('YYYY-MM-DD'), mode);
};
return (
<div className="site-calendar-demo-card">
<Calendar fullscreen={false} onPanelChange={onPanelChange} />
</div>
);
};
export default App;
.site-calendar-demo-card {
width: 300px;
border: 1px solid #f0f0f0;
border-radius: 2px;
}
2017
Jan
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
01 | 02 | 03 | 04 | 05 | 06 | 07 |
08 | 09 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 | 01 | 02 | 03 | 04 |
05 | 06 | 07 | 08 | 09 | 10 | 11 |
TypeScript
JavaScript
import { Alert, Calendar } from 'infrad';
import type { Moment } from 'moment';
import moment from 'moment';
import React, { useState } from 'react';
const App: React.FC = () => {
const [value, setValue] = useState(moment('2017-01-25'));
const [selectedValue, setSelectedValue] = useState(moment('2017-01-25'));
const onSelect = (newValue: Moment) => {
setValue(newValue);
setSelectedValue(newValue);
};
const onPanelChange = (newValue: Moment) => {
setValue(newValue);
};
return (
<>
<Alert message={`You selected date: ${selectedValue?.format('YYYY-MM-DD')}`} />
<Calendar value={value} onSelect={onSelect} onPanelChange={onPanelChange} />
</>
);
};
export default App;
Custom header
2022
7月
Su | Mo | Tu | We | Th | Fr | Sa |
---|---|---|---|---|---|---|
26 | 27 | 28 | 29 | 30 | 01 | 02 |
03 | 04 | 05 | 06 | 07 | 08 | 09 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 01 | 02 | 03 | 04 | 05 | 06 |
TypeScript
JavaScript
import { Calendar, Col, Radio, Row, Select, Typography } from 'infrad';
import type { CalendarMode } from 'infrad/lib/calendar/generateCalendar';
import type { Moment } from 'moment';
import React from 'react';
const App: React.FC = () => {
const onPanelChange = (value: Moment, mode: CalendarMode) => {
console.log(value.format('YYYY-MM-DD'), mode);
};
return (
<div className="site-calendar-customize-header-wrapper">
<Calendar
fullscreen={false}
headerRender={({ value, type, onChange, onTypeChange }) => {
const start = 0;
const end = 12;
const monthOptions = [];
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));
}
for (let i = start; i < end; i++) {
monthOptions.push(
<Select.Option key={i} value={i} className="month-item">
{months[i]}
</Select.Option>,
);
}
const year = value.year();
const month = value.month();
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"
value={year}
onChange={newYear => {
const now = value.clone().year(newYear);
onChange(now);
}}
>
{options}
</Select>
</Col>
<Col>
<Select
size="small"
dropdownMatchSelectWidth={false}
value={month}
onChange={newMonth => {
const now = value.clone().month(newMonth);
onChange(now);
}}
>
{monthOptions}
</Select>
</Col>
</Row>
</div>
);
}}
onPanelChange={onPanelChange}
/>
</div>
);
};
export default App;
.site-calendar-customize-header-wrapper {
width: 300px;
border: 1px solid #f0f0f0;
border-radius: 2px;
}
API#
<Calendar
dateCellRender={dateCellRender}
monthCellRender={monthCellRender}
onPanelChange={onPanelChange}
onSelect={onSelect}
/>
Property | Description | Type | Default | Version |
---|---|---|---|---|
dateCellRender | Customize the display of the date cell, the returned content will be appended to the cell | function(date: Moment): ReactNode | - | |
dateFullCellRender | Customize the display of the date cell, the returned content will override the cell | function(date: Moment): ReactNode | - | |
defaultValue | The date selected by default | moment | - | |
disabledDate | Function that specifies the dates that cannot be selected, currentDate is same moment object as value prop which you shouldn't mutate it](https://github.com/ant-design/ant-design/issues/30987) | (currentDate: Moment) => boolean | - | |
fullscreen | Whether to display in full-screen | boolean | true | |
headerRender | Render custom header in panel | function(object:{value: Moment, type: string, onChange: f(), onTypeChange: f()}) | - | |
locale | The calendar's locale | object | (default) | |
mode | The display mode of the calendar | month | year | month | |
monthCellRender | Customize the display of the month cell, the returned content will be appended to the cell | function(date: Moment): ReactNode | - | |
monthFullCellRender | Customize the display of the month cell, the returned content will override the cell | function(date: Moment): ReactNode | - | |
validRange | To set valid range | [moment, moment] | - | |
value | The current selected date | moment | - | |
onChange | Callback for when date changes | function(date: Moment) | - | |
onPanelChange | Callback for when panel changes | function(date: Moment, mode: string) | - | |
onSelect | Callback for when a date is selected | function(date: Moment) | - |
FAQ#
How to use Calendar with customize date library like dayjs?#
See How to use Calendar with customize date library like dayjs
How to set locale for date-related components?#
See How to set locale for date-related components