Calendar 日历

用于选择日期区间。

规则

  • 显示日历以用来选择日期或日期区间。

代码演示

基本

  1. import { List, Switch, Calendar } from 'antd-mobile';
  2. import enUS from 'antd-mobile/lib/calendar/locale/en_US';
  3. import zhCN from 'antd-mobile/lib/calendar/locale/zh_CN';
  4. const extra = {
  5. '2017/07/15': { info: 'Disable', disable: true },
  6. };
  7. const now = new Date();
  8. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 5)] = { info: 'Disable', disable: true };
  9. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 6)] = { info: 'Disable', disable: true };
  10. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7)] = { info: 'Disable', disable: true };
  11. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 8)] = { info: 'Disable', disable: true };
  12. Object.keys(extra).forEach((key) => {
  13. const info = extra[key];
  14. const date = new Date(key);
  15. if (!Number.isNaN(+date) && !extra[+date]) {
  16. extra[+date] = info;
  17. }
  18. });
  19. class Test extends React.Component {
  20. originbodyScrollY = document.getElementsByTagName('body')[0].style.overflowY;
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. en: false,
  25. show: false,
  26. config: {},
  27. };
  28. }
  29. renderBtn(zh, en, config = {}) {
  30. config.locale = this.state.en ? enUS : zhCN;
  31. return (
  32. <List.Item arrow="horizontal"
  33. onClick={() => {
  34. document.getElementsByTagName('body')[0].style.overflowY = 'hidden';
  35. this.setState({
  36. show: true,
  37. config,
  38. });
  39. }}
  40. >
  41. {this.state.en ? en : zh}
  42. </List.Item>
  43. );
  44. }
  45. changeLanguage = () => {
  46. this.setState({
  47. en: !this.state.en,
  48. });
  49. }
  50. onSelectHasDisableDate = (dates) => {
  51. console.warn('onSelectHasDisableDate', dates);
  52. }
  53. onConfirm = (startTime, endTime) => {
  54. document.getElementsByTagName('body')[0].style.overflowY = this.originbodyScrollY;
  55. this.setState({
  56. show: false,
  57. startTime,
  58. endTime,
  59. });
  60. }
  61. onCancel = () => {
  62. document.getElementsByTagName('body')[0].style.overflowY = this.originbodyScrollY;
  63. this.setState({
  64. show: false,
  65. startTime: undefined,
  66. endTime: undefined,
  67. });
  68. }
  69. getDateExtra = date => extra[+date];
  70. render() {
  71. return (
  72. <div>
  73. <List className="calendar-list" style={{ backgroundColor: 'white' }}>
  74. <List.Item className="item" extra={<Switch className="right" checked={!this.state.en} onChange={this.changeLanguage} />}>
  75. {this.state.en ? 'Chinese' : '中文'}
  76. </List.Item>
  77. {this.renderBtn('选择日期区间', 'Select Date Range')}
  78. {this.renderBtn('选择日期时间区间', 'Select DateTime Range', { pickTime: true })}
  79. {this.renderBtn('选择日期', 'Select Date', { type: 'one' })}
  80. {this.renderBtn('选择日期时间', 'Select DateTime', { type: 'one', pickTime: true })}
  81. {this.renderBtn('选择日期区间(快捷)', 'Select Date Range (Shortcut)', { showShortcut: true })}
  82. {this.renderBtn('选择日期时间区间(快捷)', 'Select DateTime Range (Shortcut)', { pickTime: true, showShortcut: true })}
  83. {this.renderBtn('大行距', 'XL row size', { rowSize: 'xl' })}
  84. {this.renderBtn('不无限滚动', 'infinite: false', { infinite: false })}
  85. {this.renderBtn('水平进入', 'Horizontal enter', { enterDirection: 'horizontal' })}
  86. {this.renderBtn('默认选择范围', 'Selected Date Range', { defaultValue: [new Date(+now - 86400000), new Date(+now - 345600000)] })}
  87. {this.renderBtn('onSelect API', 'onSelect API', {
  88. onSelect: (date, state) => {
  89. console.log('onSelect', date, state);
  90. return [date, new Date(+now - 604800000)];
  91. },
  92. })}
  93. {
  94. this.state.startTime &&
  95. <List.Item>Time1: {this.state.startTime.toLocaleString()}</List.Item>
  96. }
  97. {
  98. this.state.endTime &&
  99. <List.Item>Time2: {this.state.endTime.toLocaleString()}</List.Item>
  100. }
  101. </List>
  102. <Calendar
  103. {...this.state.config}
  104. visible={this.state.show}
  105. onCancel={this.onCancel}
  106. onConfirm={this.onConfirm}
  107. onSelectHasDisableDate={this.onSelectHasDisableDate}
  108. getDateExtra={this.getDateExtra}
  109. defaultDate={now}
  110. minDate={new Date(+now - 5184000000)}
  111. maxDate={new Date(+now + 31536000000)}
  112. />
  113. </div>
  114. );
  115. }
  116. }
  117. ReactDOM.render(<Test />, mountNode);
  1. .am-list-item .am-list-line .am-list-content {
  2. display: flex;
  3. }
  4. .calendar-list .right {
  5. float: right;
  6. }

Calendar日历 - 图1

API

属性说明类型默认值必选
enterDirection入场方向 vertical: 垂直 horizontal: 水平'horizontal' | 'vertical'verticalfalse
locale本地化Models.Localefalse
onCancel关闭时回调() => voidfalse
onConfirm确认时回调(startDateTime?: Date, endDateTime?: Date) => voidfalse
pickTime是否选择时间booleanfalsefalse
prefixCls样式前缀stringrmc-calendarfalse
renderShortcut替换快捷选择栏 需要设置showShortcut: true(select: (startDate?: Date, endDate?: Date) => void) => React.ReactNodefalse
renderHeader替换标题栏() => React.ReactNodefalse
showShortcut快捷日期选择booleanfalsefalse
titleheader titlestring{locale.title}false
type选择类型 one: 单日 range: 日期区间'one' | 'range'rangefalse
visible是否显示booleanfalsefalse
defaultDate显示开始日期Datetodayfalse
getDateExtra日期扩展数据(date: Date) => Models.ExtraDatafalse
infiniteOpt无限滚动优化(大范围选择)booleanfalsefalse
initalMonths初始化月个数number6false
maxDate最大日期Datefalse
minDate最小日期Datefalse
onSelect选择区间回调(date: Date, state?: [Dateundefined, Dateundefined]) => [Date, Date]|[Date]| voidfalse
onSelectHasDisableDate选择区间包含不可用日期(date: Date[]) => voidfalse
rowSize行大小'normal' | 'xl'false
defaultValue默认日历选择范围[Date, Date]|[Date]false
defaultTimeValue默认时间选择值Datefalse