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"> Time1: {{ this.state.startDate.toLocaleString() }} </ListItem>
  64. <ListItem *ngIf="this.state.endDate"> Time2: {{ this.state.endDate.toLocaleString() }} </ListItem>
  65. </List>
  66. <ng-template #_switch>
  67. <Switch className="right" [checked]="!this.state.en" (onChange)="this.changeLanguage()"></Switch>
  68. </ng-template>
  69. <Calendar
  70. [(ngModel)]="this.state.date"
  71. [locale]="this.state.en ? 'enUS' : 'zhCN'"
  72. [enterDirection]="this.state.enterDirection"
  73. [visible]="this.state.show"
  74. [getDateExtra]="this.state.getDateExtra"
  75. [defaultDate]="this.state.now"
  76. [minDate]="this.state.minDate"
  77. [maxDate]="this.state.maxDate"
  78. [pickTime]="this.state.pickTime"
  79. [type]="this.state.type"
  80. [rowSize]="this.state.rowSize"
  81. [showShortcut]="this.state.showShortcut"
  82. [infinite]="this.state.infinite"
  83. [defaultValue]="this.state.defaultValue"
  84. [onSelect]="this.state.onSelect"
  85. (onCancel)="triggerCancel()"
  86. (onConfirm)="triggerConfirm($event)"
  87. (onSelectHasDisableDate)="triggerSelectHasDisableDate($event)"
  88. ></Calendar>
  89. </div>
  90. `
  91. })
  92. export class DemoCalendarBasicComponent {
  93. state: any = {
  94. en: false,
  95. date: null,
  96. show: false,
  97. pickTime: false,
  98. now: new Date(),
  99. type: 'range',
  100. enterDirection: '',
  101. rowSize: 'normal',
  102. showShortcut: false,
  103. infinite: true,
  104. defaultValue: undefined,
  105. minDate: new Date(+now - 5184000000),
  106. maxDate: new Date(+now + 31536000000),
  107. onSelect: undefined,
  108. getDateExtra: date => {
  109. return extra[+date];
  110. }
  111. };
  112. constructor() {}
  113. initPara() {
  114. this.state = {
  115. ...this.state,
  116. ...{
  117. show: false,
  118. date: null,
  119. pickTime: false,
  120. now: new Date(),
  121. type: 'range',
  122. rowSize: 'normal',
  123. infinite: true,
  124. enterDirection: '',
  125. onSelect: undefined,
  126. showShortcut: false,
  127. defaultValue: undefined,
  128. minDate: new Date(+now - 5184000000),
  129. maxDate: new Date(+now + 31536000000),
  130. getDateExtra: date => {
  131. return extra[+date];
  132. }
  133. }
  134. };
  135. }
  136. changeLanguage() {
  137. this.state.en = !this.state.en;
  138. }
  139. onClick_0() {
  140. this.initPara();
  141. this.state.show = true;
  142. this.state.type = 'one';
  143. this.state.date = new Date();
  144. }
  145. onClick_1() {
  146. this.initPara();
  147. this.state.show = true;
  148. }
  149. onClick_2() {
  150. this.initPara();
  151. this.state.show = true;
  152. this.state.pickTime = true;
  153. }
  154. onClick_3() {
  155. this.initPara();
  156. this.state.show = true;
  157. this.state.type = 'one';
  158. }
  159. onClick_4() {
  160. this.initPara();
  161. this.state.show = true;
  162. this.state.pickTime = true;
  163. this.state.type = 'one';
  164. }
  165. onClick_5() {
  166. this.initPara();
  167. this.state.show = true;
  168. this.state.showShortcut = true;
  169. }
  170. onClick_6() {
  171. this.initPara();
  172. this.state.show = true;
  173. this.state.pickTime = true;
  174. this.state.showShortcut = true;
  175. }
  176. onClick_7() {
  177. this.initPara();
  178. this.state.show = true;
  179. this.state.rowSize = 'xl';
  180. }
  181. onClick_8() {
  182. this.initPara();
  183. this.state.show = true;
  184. this.state.infinite = false;
  185. }
  186. onClick_9() {
  187. this.initPara();
  188. this.state.show = true;
  189. this.state.enterDirection = 'horizontal';
  190. }
  191. onClick_10() {
  192. this.initPara();
  193. this.state.show = true;
  194. this.state.defaultValue = [new Date(+now - 86400000), new Date(+now - 345600000)];
  195. }
  196. onClick_11() {
  197. this.initPara();
  198. this.state.show = true;
  199. this.state.onSelect = (date, state) => {
  200. console.log('onSelect', date, state);
  201. return [date, new Date(+now - 604800000)];
  202. };
  203. }
  204. triggerCancel() {
  205. this.state.show = false;
  206. }
  207. triggerConfirm(value) {
  208. const { startDate, endDate } = value;
  209. this.state = {
  210. ...this.state,
  211. ...{ show: false, startDate, endDate }
  212. };
  213. this.triggerCancel();
  214. console.log('onConfirm', startDate, endDate);
  215. }
  216. triggerSelectHasDisableDate(dates) {
  217. console.warn('onSelectHasDisableDate', dates);
  218. }
  219. }

Calendar日历 - 图1

API

参数说明类型默认值
[enterDirection]入场方向‘horizontal’ | ‘vertical’‘vertical’
[locale]本地化DateModels.Locale-
[pickTime]是否选择时间booleanfalse
[prefixCls]样式前缀stringrmc-calendar
[showShortcut]快捷日期选择booleanfalse
[type]选择类型 one: 单日 range: 日期区间‘one’ | ‘range’‘range’
[visible]是否显示booleanfalse
[defaultDate]显示开始日期Datenew Date()
[getDateExtra]日期扩展数据(date: Date) => DateModels.ExtraData-
[initalMonths]初始化月个数number6
[maxDate]最大日期Date-
[minDate]最小日期Date-
[rowSize]行大小‘normal’ | ‘xl’-
[defaultValue]默认日历选择范围[Date, Date] | [Date]-
[defaultTimeValue]默认时间选择值Date-
[onSelect]选择区间回调(date: Date, state?: [Date | undefined, Date | undefined]) => [Date, Date] | [Date] | void-
[(ngModel)]ngModelArray<Date> | DateDate
(onCancel)关闭时回调EventEmitter<void>-
(onConfirm)确认时回调EventEmitter<{startDateTime?: Date, endDateTime?: Date}>-
(onSelectHasDisableDate)选择区间包含不可用日期(date: Date[]) => void-