index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { VantComponent } from '../../../common/component';
  2. import { getMonthEndDay, compareDay, getPrevDay, getNextDay } from '../../utils';
  3. VantComponent({
  4. props: {
  5. date: {
  6. type: null,
  7. observer: 'setDays'
  8. },
  9. type: {
  10. type: String,
  11. observer: 'setDays'
  12. },
  13. color: String,
  14. minDate: {
  15. type: null,
  16. observer: 'setDays'
  17. },
  18. maxDate: {
  19. type: null,
  20. observer: 'setDays'
  21. },
  22. showMark: Boolean,
  23. rowHeight: [Number, String],
  24. formatter: {
  25. type: null,
  26. observer: 'setDays'
  27. },
  28. currentDate: {
  29. type: [null, Array],
  30. observer: 'setDays'
  31. },
  32. allowSameDay: Boolean,
  33. showSubtitle: Boolean,
  34. showMonthTitle: Boolean
  35. },
  36. data: {
  37. visible: true,
  38. days: []
  39. },
  40. methods: {
  41. onClick(event) {
  42. const { index } = event.currentTarget.dataset;
  43. const item = this.data.days[index];
  44. if (item.type !== 'disabled') {
  45. this.$emit('click', item);
  46. }
  47. },
  48. setDays() {
  49. const days = [];
  50. const startDate = new Date(this.data.date);
  51. const year = startDate.getFullYear();
  52. const month = startDate.getMonth();
  53. const totalDay = getMonthEndDay(startDate.getFullYear(), startDate.getMonth() + 1);
  54. for (let day = 1; day <= totalDay; day++) {
  55. const date = new Date(year, month, day);
  56. const type = this.getDayType(date);
  57. let config = {
  58. date,
  59. type,
  60. text: day,
  61. bottomInfo: this.getBottomInfo(type)
  62. };
  63. if (this.data.formatter) {
  64. config = this.data.formatter(config);
  65. }
  66. days.push(config);
  67. }
  68. this.setData({ days });
  69. },
  70. getMultipleDayType(day) {
  71. const { currentDate } = this.data;
  72. if (!Array.isArray(currentDate)) {
  73. return '';
  74. }
  75. const isSelected = date => currentDate.some(item => compareDay(item, date) === 0);
  76. if (isSelected(day)) {
  77. const prevDay = getPrevDay(day);
  78. const nextDay = getNextDay(day);
  79. const prevSelected = isSelected(prevDay);
  80. const nextSelected = isSelected(nextDay);
  81. if (prevSelected && nextSelected) {
  82. return 'multiple-middle';
  83. }
  84. if (prevSelected) {
  85. return 'end';
  86. }
  87. return nextSelected ? 'start' : 'multiple-selected';
  88. }
  89. return '';
  90. },
  91. getRangeDayType(day) {
  92. const { currentDate, allowSameDay } = this.data;
  93. if (!Array.isArray(currentDate)) {
  94. return;
  95. }
  96. const [startDay, endDay] = currentDate;
  97. if (!startDay) {
  98. return;
  99. }
  100. const compareToStart = compareDay(day, startDay);
  101. if (!endDay) {
  102. return compareToStart === 0 ? 'start' : '';
  103. }
  104. const compareToEnd = compareDay(day, endDay);
  105. if (compareToStart === 0 && compareToEnd === 0 && allowSameDay) {
  106. return 'start-end';
  107. }
  108. if (compareToStart === 0) {
  109. return 'start';
  110. }
  111. if (compareToEnd === 0) {
  112. return 'end';
  113. }
  114. if (compareToStart > 0 && compareToEnd < 0) {
  115. return 'middle';
  116. }
  117. },
  118. getDayType(day) {
  119. const { type, minDate, maxDate, currentDate } = this.data;
  120. if (compareDay(day, minDate) < 0 || compareDay(day, maxDate) > 0) {
  121. return 'disabled';
  122. }
  123. if (type === 'single') {
  124. return compareDay(day, currentDate) === 0 ? 'selected' : '';
  125. }
  126. if (type === 'multiple') {
  127. return this.getMultipleDayType(day);
  128. }
  129. /* istanbul ignore else */
  130. if (type === 'range') {
  131. return this.getRangeDayType(day);
  132. }
  133. },
  134. getBottomInfo(type) {
  135. if (this.data.type === 'range') {
  136. if (type === 'start') {
  137. return '开始';
  138. }
  139. if (type === 'end') {
  140. return '结束';
  141. }
  142. if (type === 'start-end') {
  143. return '开始/结束';
  144. }
  145. }
  146. }
  147. }
  148. });