3eafb66da40ce8eaa35039ede08418fc1057b960949fcf38eaa6b789e661d6e7b2fcb28d6d5d645084125485ffb38483131d2841acc6a484c84a3078d6ec34 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <table @click="handleYearTableClick" class="el-year-table">
  3. <tbody>
  4. <tr>
  5. <td class="available" :class="getCellStyle(startYear + 0)">
  6. <a class="cell">{{ startYear }}</a>
  7. </td>
  8. <td class="available" :class="getCellStyle(startYear + 1)">
  9. <a class="cell">{{ startYear + 1 }}</a>
  10. </td>
  11. <td class="available" :class="getCellStyle(startYear + 2)">
  12. <a class="cell">{{ startYear + 2 }}</a>
  13. </td>
  14. <td class="available" :class="getCellStyle(startYear + 3)">
  15. <a class="cell">{{ startYear + 3 }}</a>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td class="available" :class="getCellStyle(startYear + 4)">
  20. <a class="cell">{{ startYear + 4 }}</a>
  21. </td>
  22. <td class="available" :class="getCellStyle(startYear + 5)">
  23. <a class="cell">{{ startYear + 5 }}</a>
  24. </td>
  25. <td class="available" :class="getCellStyle(startYear + 6)">
  26. <a class="cell">{{ startYear + 6 }}</a>
  27. </td>
  28. <td class="available" :class="getCellStyle(startYear + 7)">
  29. <a class="cell">{{ startYear + 7 }}</a>
  30. </td>
  31. </tr>
  32. <tr>
  33. <td class="available" :class="getCellStyle(startYear + 8)">
  34. <a class="cell">{{ startYear + 8 }}</a>
  35. </td>
  36. <td class="available" :class="getCellStyle(startYear + 9)">
  37. <a class="cell">{{ startYear + 9 }}</a>
  38. </td>
  39. <td></td>
  40. <td></td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. </template>
  45. <script type="text/babel">
  46. import { hasClass } from 'element-ui/src/utils/dom';
  47. import { isDate, range, nextDate, getDayCountOfYear } from 'element-ui/src/utils/date-util';
  48. import { arrayFindIndex, coerceTruthyValueToArray } from 'element-ui/src/utils/util';
  49. const datesInYear = year => {
  50. const numOfDays = getDayCountOfYear(year);
  51. const firstDay = new Date(year, 0, 1);
  52. return range(numOfDays).map(n => nextDate(firstDay, n));
  53. };
  54. export default {
  55. props: {
  56. disabledDate: {},
  57. value: {},
  58. defaultValue: {
  59. validator(val) {
  60. // null or valid Date Object
  61. return val === null || (val instanceof Date && isDate(val));
  62. }
  63. },
  64. date: {},
  65. selectionMode: {}
  66. },
  67. computed: {
  68. startYear() {
  69. return Math.floor(this.date.getFullYear() / 10) * 10;
  70. }
  71. },
  72. methods: {
  73. getCellStyle(year) {
  74. const style = {};
  75. const today = new Date();
  76. style.disabled = typeof this.disabledDate === 'function'
  77. ? datesInYear(year).every(this.disabledDate)
  78. : false;
  79. style.current = arrayFindIndex(coerceTruthyValueToArray(this.value), date => date.getFullYear() === year) >= 0;
  80. style.today = today.getFullYear() === year;
  81. style.default = this.defaultValue && this.defaultValue.getFullYear() === year;
  82. return style;
  83. },
  84. handleYearTableClick(event) {
  85. const target = event.target;
  86. if (target.tagName === 'A') {
  87. if (hasClass(target.parentNode, 'disabled')) return;
  88. const year = target.textContent || target.innerText;
  89. if (this.selectionMode === 'years') {
  90. const value = this.value || [];
  91. const idx = arrayFindIndex(value, date => date.getFullYear() === Number(year));
  92. const newValue = idx > -1
  93. ? [...value.slice(0, idx), ...value.slice(idx + 1)]
  94. : [...value, new Date(year)];
  95. this.$emit('pick', newValue);
  96. } else {
  97. this.$emit('pick', Number(year));
  98. }
  99. }
  100. }
  101. }
  102. };
  103. </script>