date-table.vue 5.5 KB

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