Calendar日历

    当数据是日期或按照日期划分时,例如日程、课表、价格日历等,农历等。目前支持年/月切换。

    基本

    一个通用的日历面板,支持年/月切换。

    Calendar日历 - 图2

    一个复杂的应用示例,用 和 monthCellRender 函数来自定义需要渲染的数据。

    1. import { Calendar, Badge } from 'antd';
    2. function getListData(value) {
    3. let listData;
    4. switch (value.date()) {
    5. case 8:
    6. listData = [
    7. { type: 'warning', content: 'This is warning event.' },
    8. { type: 'success', content: 'This is usual event.' },
    9. ];
    10. break;
    11. case 10:
    12. listData = [
    13. { type: 'warning', content: 'This is warning event.' },
    14. { type: 'success', content: 'This is usual event.' },
    15. { type: 'error', content: 'This is error event.' },
    16. ];
    17. break;
    18. case 15:
    19. listData = [
    20. { type: 'warning', content: 'This is warning event' },
    21. { type: 'success', content: 'This is very long usual event。。....' },
    22. { type: 'error', content: 'This is error event 1.' },
    23. { type: 'error', content: 'This is error event 2.' },
    24. { type: 'error', content: 'This is error event 3.' },
    25. { type: 'error', content: 'This is error event 4.' },
    26. ];
    27. break;
    28. default:
    29. }
    30. return listData || [];
    31. }
    32. function dateCellRender(value) {
    33. const listData = getListData(value);
    34. return (
    35. <ul className="events">
    36. {listData.map(item => (
    37. <li key={item.content}>
    38. <Badge status={item.type} text={item.content} />
    39. </li>
    40. ))}
    41. </ul>
    42. );
    43. }
    44. function getMonthData(value) {
    45. if (value.month() === 8) {
    46. return 1394;
    47. }
    48. }
    49. function monthCellRender(value) {
    50. return num ? (
    51. <section>{num}</section>
    52. <span>Backlog number</span>
    53. </div>
    54. ) : null;
    55. }
    56. ReactDOM.render(
    57. <Calendar dateCellRender={dateCellRender} monthCellRender={monthCellRender} />,
    58. mountNode,
    59. );

    用于嵌套在空间有限的容器中。

    1. import { Calendar } from 'antd';
    2. function onPanelChange(value, mode) {
    3. console.log(value, mode);
    4. }
    5. ReactDOM.render(
    6. <div style={{ width: 300, border: '1px solid #d9d9d9', borderRadius: 4 }}>
    7. <Calendar fullscreen={false} onPanelChange={onPanelChange} />
    8. </div>,
    9. mountNode,
    10. );

    Calendar日历 - 图4

    一个通用的日历面板,支持年/月切换。

    自定义头部

    自定义日历头部内容。

    1. import { Calendar, Select, Radio, Col, Row } from 'antd';
    2. const { Group, Button } = Radio;
    3. function onPanelChange(value, mode) {
    4. console.log(value, mode);
    5. }
    6. ReactDOM.render(
    7. <div style={{ width: 300, border: '1px solid #d9d9d9', borderRadius: 4 }}>
    8. <Calendar
    9. fullscreen={false}
    10. headerRender={({ value, type, onChange, onTypeChange }) => {
    11. const start = 0;
    12. const end = 12;
    13. const monthOptions = [];
    14. const current = value.clone();
    15. const localeData = value.localeData();
    16. const months = [];
    17. for (let i = 0; i < 12; i++) {
    18. current.month(i);
    19. months.push(localeData.monthsShort(current));
    20. }
    21. for (let index = start; index < end; index++) {
    22. monthOptions.push(
    23. <Select.Option className="month-item" key={`${index}`}>
    24. {months[index]}
    25. </Select.Option>,
    26. );
    27. }
    28. const options = [];
    29. for (let i = year - 10; i < year + 10; i += 1) {
    30. options.push(
    31. <Select.Option key={i} value={i} className="year-item">
    32. {i}
    33. </Select.Option>,
    34. );
    35. }
    36. return (
    37. <div style={{ padding: 10 }}>
    38. <div style={{ marginBottom: '10px' }}>Custom header </div>
    39. <Row type="flex" justify="space-between">
    40. <Col>
    41. <Group size="small" onChange={e => onTypeChange(e.target.value)} value={type}>
    42. <Button value="month">Month</Button>
    43. <Button value="year">Year</Button>
    44. </Group>
    45. </Col>
    46. <Col>
    47. <Select
    48. size="small"
    49. dropdownMatchSelectWidth={false}
    50. className="my-year-select"
    51. onChange={newYear => {
    52. const now = value.clone().year(newYear);
    53. onChange(now);
    54. }}
    55. value={String(year)}
    56. >
    57. {options}
    58. </Select>
    59. </Col>
    60. <Col>
    61. <Select
    62. size="small"
    63. dropdownMatchSelectWidth={false}
    64. value={String(month)}
    65. onChange={selectedMonth => {
    66. const newValue = value.clone();
    67. newValue.month(parseInt(selectedMonth, 10));
    68. onChange(newValue);
    69. }}
    70. >
    71. {monthOptions}
    72. </Select>
    73. </Col>
    74. </Row>
    75. </div>
    76. );
    77. }}
    78. onPanelChange={onPanelChange}
    79. />
    80. </div>,
    81. mountNode,

    注意:Calendar 部分 locale 是从 value 中读取,所以请先正确设置 moment 的 locale。