index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { getDateRect, isSameDate, getMonthDateRect, isValidDate, getDate } from '../date';
  2. export default class TCalendar {
  3. constructor(options = {}) {
  4. this.type = 'single';
  5. Object.assign(this, options);
  6. if (!this.minDate)
  7. this.minDate = getDate();
  8. if (!this.maxDate)
  9. this.maxDate = getDate(6);
  10. }
  11. getTrimValue() {
  12. const { value, type } = this;
  13. const format = (val) => {
  14. if (val instanceof Date)
  15. return val;
  16. if (typeof val === 'number')
  17. return new Date(val);
  18. return new Date();
  19. };
  20. if (type === 'single' && isValidDate(value))
  21. return format(value);
  22. if (type === 'multiple' || type === 'range') {
  23. if (Array.isArray(value)) {
  24. const isValid = value.every((item) => isValidDate(item));
  25. return isValid ? value.map((item) => format(item)) : [];
  26. }
  27. return [];
  28. }
  29. }
  30. getDays(weekdays) {
  31. const ans = [];
  32. let i = this.firstDayOfWeek % 7;
  33. while (ans.length < 7) {
  34. ans.push(weekdays[i]);
  35. i = (i + 1) % 7;
  36. }
  37. return ans;
  38. }
  39. getMonths() {
  40. const ans = [];
  41. const selectedDate = this.getTrimValue();
  42. const { minDate, maxDate, type, format } = this;
  43. let { year: minYear, month: minMonth, time: minTime } = getDateRect(minDate);
  44. const { year: maxYear, month: maxMonth, time: maxTime } = getDateRect(maxDate);
  45. const calcType = (year, month, date) => {
  46. const curDate = new Date(year, month, date, 23, 59, 59);
  47. if (type === 'single' && selectedDate) {
  48. if (isSameDate({ year, month, date }, selectedDate))
  49. return 'selected';
  50. }
  51. if (type === 'multiple' && selectedDate) {
  52. const hit = selectedDate.some((item) => isSameDate({ year, month, date }, item));
  53. if (hit) {
  54. return 'selected';
  55. }
  56. }
  57. if (type === 'range' && selectedDate) {
  58. if (Array.isArray(selectedDate)) {
  59. const [startDate, endDate] = selectedDate;
  60. if (startDate && isSameDate({ year, month, date }, startDate))
  61. return 'start';
  62. if (endDate && isSameDate({ year, month, date }, endDate))
  63. return 'end';
  64. if (startDate && endDate && curDate.getTime() > startDate.getTime() && curDate.getTime() < endDate.getTime())
  65. return 'centre';
  66. }
  67. }
  68. const minCurDate = new Date(year, month, date, 0, 0, 0);
  69. if (curDate.getTime() < minTime || minCurDate.getTime() > maxTime) {
  70. return 'disabled';
  71. }
  72. return '';
  73. };
  74. while (minYear < maxYear || (minYear === maxYear && minMonth <= maxMonth)) {
  75. const target = getMonthDateRect(new Date(minYear, minMonth, 1));
  76. const months = [];
  77. for (let i = 1; i <= 31; i++) {
  78. if (i > target.lastDate)
  79. break;
  80. const dateObj = {
  81. date: new Date(minYear, minMonth, i),
  82. day: i,
  83. type: calcType(minYear, minMonth, i),
  84. };
  85. months.push(format ? format(dateObj) : dateObj);
  86. }
  87. ans.push({
  88. year: minYear,
  89. month: minMonth,
  90. months,
  91. weekdayOfFirstDay: target.weekdayOfFirstDay,
  92. });
  93. const curDate = getDateRect(new Date(minYear, minMonth + 1, 1));
  94. minYear = curDate.year;
  95. minMonth = curDate.month;
  96. }
  97. return ans;
  98. }
  99. select({ cellType, year, month, date }) {
  100. const { type } = this;
  101. const selectedDate = this.getTrimValue();
  102. if (cellType === 'disabled')
  103. return;
  104. const selected = new Date(year, month, date);
  105. this.value = selected;
  106. if (type === 'range' && Array.isArray(selectedDate)) {
  107. if (selectedDate.length === 1 && selected > selectedDate[0]) {
  108. this.value = [selectedDate[0], selected];
  109. }
  110. else {
  111. this.value = [selected];
  112. }
  113. }
  114. else if (type === 'multiple' && Array.isArray(selectedDate)) {
  115. const newVal = [...selectedDate];
  116. const index = selectedDate.findIndex((item) => isSameDate(item, selected));
  117. if (index > -1) {
  118. newVal.splice(index, 1);
  119. }
  120. else {
  121. newVal.push(selected);
  122. }
  123. this.value = newVal;
  124. }
  125. return this.value;
  126. }
  127. }