Calendar日历

用于选择日期区间。

规则

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

代码演示

基本

  1. import { Component } from '@angular/core';
  2. const extra = {
  3. '2017/07/15': { info: 'Disable', disable: true }
  4. };
  5. const now = new Date();
  6. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 5)] = { info: 'Disable', disable: true };
  7. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 6)] = { info: 'Disable', disable: true };
  8. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7)] = { info: 'Disable', disable: true };
  9. extra[+new Date(now.getFullYear(), now.getMonth(), now.getDate() + 8)] = { info: 'Disable', disable: true };
  10. for (let key in extra) {
  11. if (extra.hasOwnProperty(key)) {
  12. let info = extra[key];
  13. const date = new Date(key);
  14. if (!Number.isNaN(+date) && !extra[+date]) {
  15. extra[+date] = info;
  16. }
  17. }
  18. }
  19. @Component({
  20. selector: 'demo-calendar-basic',
  21. template: `
  22. <div class="am-demo-page">
  23. <List className="calendar-list" style="backgroundColor: 'white'">
  24. <ListItem className="item" [extra]="_switch">
  25. {{this.state.en ? 'Chinese' : '中文'}}
  26. </ListItem>
  27. <ListItem [arrow]="'horizontal'" (onClick)="onClick_0()">
  28. {{this.state.en ? 'ngModel' : 'ngModel'}}
  29. </ListItem>
  30. <ListItem [arrow]="'horizontal'" (onClick)="onClick_1()">
  31. {{this.state.en ? 'Select Date Range' : '选择日期区间'}}
  32. </ListItem>
  33. <ListItem [arrow]="'horizontal'" (onClick)="onClick_2()">
  34. {{this.state.en ? 'Select DateTime Range' : '选择日期时间区间'}}
  35. </ListItem>
  36. <ListItem [arrow]="'horizontal'" (onClick)="onClick_3()">
  37. {{this.state.en ? 'Select Date' : '选择日期'}}
  38. </ListItem>
  39. <ListItem [arrow]="'horizontal'" (onClick)="onClick_4()">
  40. {{this.state.en ? 'Select DateTime' : '选择日期时间'}}
  41. </ListItem>
  42. <ListItem [arrow]="'horizontal'" (onClick)="onClick_5()">
  43. {{this.state.en ? 'Select Date Range (Shortcut)' : '选择日期区间(快捷)'}}
  44. </ListItem>
  45. <ListItem [arrow]="'horizontal'" (onClick)="onClick_6()">
  46. {{this.state.en ? 'Select DateTime Range (Shortcut)' : '选择日期时间区间(快捷)'}}
  47. </ListItem>
  48. <ListItem [arrow]="'horizontal'" (onClick)="onClick_7()">
  49. {{this.state.en ? 'XL row size' : '大行距'}}
  50. </ListItem>
  51. <ListItem [arrow]="'horizontal'" (onClick)="onClick_8()">
  52. {{this.state.en ? 'infinite: false' : '不无限滚动'}}
  53. </ListItem>
  54. <ListItem [arrow]="'horizontal'" (onClick)="onClick_9()">
  55. {{this.state.en ? 'Horizontal enter' : '水平进入'}}
  56. </ListItem>
  57. <ListItem [arrow]="'horizontal'" (onClick)="onClick_10()">
  58. {{this.state.en ? 'Selected Date Range' : '默认选择范围'}}
  59. </ListItem>
  60. <ListItem [arrow]="'horizontal'" (onClick)="onClick_11()">
  61. {{this.state.en ? 'onSelect API' : 'onSelect API'}}
  62. </ListItem>
  63. <ListItem *ngIf="this.state.startDate">
  64. Time1: {{this.state.startDate.toLocaleString()}}
  65. </ListItem>
  66. <ListItem *ngIf="this.state.endDate">
  67. Time2: {{this.state.endDate.toLocaleString()}}
  68. </ListItem>
  69. </List>
  70. <ng-template #_switch>
  71. <Switch className="right" [checked]="!this.state.en" (onChange)="this.changeLanguage()"></Switch>
  72. </ng-template>
  73. <Calendar [(ngModel)]="this.state.date"
  74. [locale]="this.state.en ? 'enUS' : 'zhCN'"
  75. [enterDirection]="this.state.enterDirection"
  76. [visible]="this.state.show"
  77. [getDateExtra]="this.state.getDateExtra"
  78. [defaultDate]="this.state.now"
  79. [minDate]="this.state.minDate"
  80. [maxDate]="this.state.maxDate"
  81. [pickTime]="this.state.pickTime"
  82. [type]="this.state.type"
  83. [rowSize]="this.state.rowSize"
  84. [showShortcut]="this.state.showShortcut"
  85. [infinite]="this.state.infinite"
  86. [defaultValue]="this.state.defaultValue"
  87. [onSelect]="this.state.onSelect"
  88. (onCancel)="triggerCancel()"
  89. (onConfirm)="triggerConfirm($event)"
  90. (onSelectHasDisableDate)="triggerSelectHasDisableDate($event)"
  91. ></Calendar>
  92. </div>
  93. `
  94. })
  95. export class DemoCalendarBasicComponent {
  96. state: any = {
  97. en: false,
  98. date: null,
  99. show: false,
  100. pickTime: false,
  101. now: new Date(),
  102. type: 'range',
  103. enterDirection: '',
  104. rowSize: 'normal',
  105. showShortcut: false,
  106. infinite: true,
  107. defaultValue: undefined,
  108. minDate: new Date(+now - 5184000000),
  109. maxDate: new Date(+now + 31536000000),
  110. onSelect: undefined,
  111. getDateExtra: date => {
  112. return extra[+date];
  113. }
  114. };
  115. constructor() {}
  116. initPara() {
  117. this.state = {
  118. ...this.state,
  119. ...{
  120. show: false,
  121. date: null,
  122. pickTime: false,
  123. now: new Date(),
  124. type: 'range',
  125. rowSize: 'normal',
  126. infinite: true,
  127. enterDirection: '',
  128. onSelect: undefined,
  129. showShortcut: false,
  130. defaultValue: undefined,
  131. minDate: new Date(+now - 5184000000),
  132. maxDate: new Date(+now + 31536000000),
  133. getDateExtra: date => {
  134. return extra[+date];
  135. }
  136. }
  137. };
  138. }
  139. changeLanguage() {
  140. this.state.en = !this.state.en;
  141. }
  142. onClick_0() {
  143. this.initPara();
  144. this.state.show = true;
  145. this.state.type = 'one';
  146. this.state.date = new Date();
  147. }
  148. onClick_1() {
  149. this.initPara();
  150. this.state.show = true;
  151. }
  152. onClick_2() {
  153. this.initPara();
  154. this.state.show = true;
  155. this.state.pickTime = true;
  156. }
  157. onClick_3() {
  158. this.initPara();
  159. this.state.show = true;
  160. this.state.type = 'one';
  161. }
  162. onClick_4() {
  163. this.initPara();
  164. this.state.show = true;
  165. this.state.pickTime = true;
  166. this.state.type = 'one';
  167. }
  168. onClick_5() {
  169. this.initPara();
  170. this.state.show = true;
  171. this.state.showShortcut = true;
  172. }
  173. onClick_6() {
  174. this.initPara();
  175. this.state.show = true;
  176. this.state.pickTime = true;
  177. this.state.showShortcut = true;
  178. }
  179. onClick_7() {
  180. this.initPara();
  181. this.state.show = true;
  182. this.state.rowSize = 'xl';
  183. }
  184. onClick_8() {
  185. this.initPara();
  186. this.state.show = true;
  187. this.state.infinite = false;
  188. }
  189. onClick_9() {
  190. this.initPara();
  191. this.state.show = true;
  192. this.state.enterDirection = 'horizontal';
  193. }
  194. onClick_10() {
  195. this.initPara();
  196. this.state.show = true;
  197. this.state.defaultValue = [new Date(+now - 86400000), new Date(+now - 345600000)];
  198. }
  199. onClick_11() {
  200. this.initPara();
  201. this.state.show = true;
  202. this.state.onSelect = (date, state) => {
  203. console.log('onSelect', date, state);
  204. return [date, new Date(+now - 604800000)];
  205. };
  206. }
  207. triggerCancel() {
  208. this.state.show = false;
  209. }
  210. triggerConfirm(value) {
  211. const { startDate, endDate } = value;
  212. this.state = {
  213. ...this.state,
  214. ...{ show: false, startDate, endDate }
  215. };
  216. this.triggerCancel();
  217. console.log('onConfirm', startDate, endDate);
  218. }
  219. triggerSelectHasDisableDate(dates) {
  220. console.warn('onSelectHasDisableDate', dates);
  221. }
  222. }

Calendar 日历 - 图1

API

属性说明类型默认值必选
[(ngModel)]ngModelArray\<Date> | Datefalse
enterDirection入场方向 vertical: 垂直 horizontal: 水平'horizontal' | 'vertical'verticalfalse
locale本地化DateModels.Localefalse
onCancel关闭时回调() => voidfalse
onConfirm确认时回调(startDateTime?: Date, endDateTime?: Date) => voidfalse
pickTime是否选择时间booleanfalsefalse
prefixCls样式前缀stringrmc-calendarfalse
showShortcut快捷日期选择booleanfalsefalse
type选择类型 one: 单日 range: 日期区间'one' | 'range'rangefalse
visible是否显示booleanfalsefalse
defaultDate显示开始日期Datetodayfalse
getDateExtra日期扩展数据(date: Date) => DateModels.ExtraDatafalse
initalMonths初始化月个数number6false
maxDate最大日期Datefalse
minDate最小日期Datefalse
onSelect选择区间回调(date: Date, state?: [Date | undefined, Date | undefined]) => [Date, Date] | [Date] | voidfalse
onSelectHasDisableDate选择区间包含不可用日期(date: Date[]) => voidfalse
rowSize行大小'normal' | 'xl'false
defaultValue默认日历选择范围[Date, Date] | [Date]false
defaultTimeValue默认时间选择值Datefalse