date-table.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <script>
  2. import fecha from 'element-ui/src/utils/date';
  3. import { range as rangeArr, getFirstDayOfMonth, getPrevMonthLastDays, getMonthDays, getI18nSettings, validateRangeInOneMonth } from 'element-ui/src/utils/date-util';
  4. export default {
  5. props: {
  6. selectedDay: String, // formated date yyyy-MM-dd
  7. range: {
  8. type: Array,
  9. validator(val) {
  10. if (!(val && val.length)) return true;
  11. const [start, end] = val;
  12. return validateRangeInOneMonth(start, end);
  13. }
  14. },
  15. date: Date,
  16. hideHeader: Boolean,
  17. firstDayOfWeek: Number
  18. },
  19. inject: ['elCalendar'],
  20. data() {
  21. return {
  22. WEEK_DAYS: getI18nSettings().dayNames
  23. };
  24. },
  25. methods: {
  26. toNestedArr(days) {
  27. return rangeArr(days.length / 7).map((_, index) => {
  28. const start = index * 7;
  29. return days.slice(start, start + 7);
  30. });
  31. },
  32. getFormateDate(day, type) {
  33. if (!day || ['prev', 'current', 'next'].indexOf(type) === -1) {
  34. throw new Error('invalid day or type');
  35. }
  36. let prefix = this.curMonthDatePrefix;
  37. if (type === 'prev') {
  38. prefix = this.prevMonthDatePrefix;
  39. } else if (type === 'next') {
  40. prefix = this.nextMonthDatePrefix;
  41. }
  42. day = `00${day}`.slice(-2);
  43. return `${prefix}-${day}`;
  44. },
  45. getCellClass({ text, type}) {
  46. const classes = [type];
  47. if (type === 'current') {
  48. const date = this.getFormateDate(text, type);
  49. if (date === this.selectedDay) {
  50. classes.push('is-selected');
  51. }
  52. if (date === this.formatedToday) {
  53. classes.push('is-today');
  54. }
  55. }
  56. return classes;
  57. },
  58. pickDay({ text, type }) {
  59. const date = this.getFormateDate(text, type);
  60. this.$emit('pick', date);
  61. },
  62. cellRenderProxy({ text, type }) {
  63. let render = this.elCalendar.$scopedSlots.dateCell;
  64. if (!render) return <span>{ text }</span>;
  65. const day = this.getFormateDate(text, type);
  66. const date = new Date(day);
  67. const data = {
  68. isSelected: this.selectedDay === day,
  69. type: `${type}-month`,
  70. day
  71. };
  72. return render({ date, data });
  73. }
  74. },
  75. computed: {
  76. prevMonthDatePrefix() {
  77. const temp = new Date(this.date.getTime());
  78. temp.setDate(0);
  79. return fecha.format(temp, 'yyyy-MM');
  80. },
  81. curMonthDatePrefix() {
  82. return fecha.format(this.date, 'yyyy-MM');
  83. },
  84. nextMonthDatePrefix() {
  85. const temp = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 1);
  86. return fecha.format(temp, 'yyyy-MM');
  87. },
  88. formatedToday() {
  89. return this.elCalendar.formatedToday;
  90. },
  91. isInRange() {
  92. return this.range && this.range.length;
  93. },
  94. rows() {
  95. let days = [];
  96. // if range exists, should render days in range.
  97. if (this.isInRange) {
  98. const [start, end] = this.range;
  99. const currentMonthRange = rangeArr(end.getDate() - start.getDate() + 1).map((_, index) => ({
  100. text: start.getDate() + index,
  101. type: 'current'
  102. }));
  103. let remaining = currentMonthRange.length % 7;
  104. remaining = remaining === 0 ? 0 : 7 - remaining;
  105. const nextMonthRange = rangeArr(remaining).map((_, index) => ({
  106. text: index + 1,
  107. type: 'next'
  108. }));
  109. days = currentMonthRange.concat(nextMonthRange);
  110. } else {
  111. const date = this.date;
  112. let firstDay = getFirstDayOfMonth(date);
  113. firstDay = firstDay === 0 ? 7 : firstDay;
  114. const firstDayOfWeek = typeof this.firstDayOfWeek === 'number' ? this.firstDayOfWeek : 1;
  115. const prevMonthDays = getPrevMonthLastDays(date, firstDay - firstDayOfWeek).map(day => ({
  116. text: day,
  117. type: 'prev'
  118. }));
  119. const currentMonthDays = getMonthDays(date).map(day => ({
  120. text: day,
  121. type: 'current'
  122. }));
  123. days = [...prevMonthDays, ...currentMonthDays];
  124. const nextMonthDays = rangeArr(42 - days.length).map((_, index) => ({
  125. text: index + 1,
  126. type: 'next'
  127. }));
  128. days = days.concat(nextMonthDays);
  129. }
  130. return this.toNestedArr(days);
  131. },
  132. weekDays() {
  133. const start = this.firstDayOfWeek;
  134. const { WEEK_DAYS } = this;
  135. if (typeof start !== 'number' || start === 0) {
  136. return WEEK_DAYS.slice();
  137. } else {
  138. return WEEK_DAYS.slice(start).concat(WEEK_DAYS.slice(0, start));
  139. }
  140. }
  141. },
  142. render() {
  143. const thead = this.hideHeader ? null : (<thead>
  144. {
  145. this.weekDays.map(day => <th key={day}>{ day }</th>)
  146. }
  147. </thead>);
  148. return (
  149. <table
  150. class={{
  151. 'el-calendar-table': true,
  152. 'is-range': this.isInRange
  153. }}
  154. cellspacing="0"
  155. cellpadding="0">
  156. {
  157. thead
  158. }
  159. <tbody>
  160. {
  161. this.rows.map((row, index) => <tr
  162. class={{
  163. 'el-calendar-table__row': true,
  164. 'el-calendar-table__row--hide-border': index === 0 && this.hideHeader
  165. }}
  166. key={index}>
  167. {
  168. row.map((cell, key) => <td key={key}
  169. class={ this.getCellClass(cell) }
  170. onClick={this.pickDay.bind(this, cell)}>
  171. <div class="el-calendar-day">
  172. {
  173. this.cellRenderProxy(cell)
  174. }
  175. </div>
  176. </td>)
  177. }
  178. </tr>)
  179. }
  180. </tbody>
  181. </table>);
  182. }
  183. };
  184. </script>