index.js 3.9 KB

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