km-year.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div class="year-picker">
  3. <input
  4. v-model="value"
  5. placeholder="请选择年份"
  6. :class="[
  7. 'year-picker__input',
  8. 'year-picker__input--real',
  9. showYearContent ? 'year-picker__input--focus' : '',
  10. ]"
  11. type="text"
  12. @focus="inputFocus"
  13. />
  14. <!--<a-icon type="calendar" class="year-picker__icon"/>-->
  15. <div v-if="showYearContent" class="year-picker__year-box">
  16. <div class="year-picker__input">{{ value }}</div>
  17. <div class="year-picker__year-title">
  18. <span class="to-left" @click="toLeft"> -- </span>
  19. <strong>{{ yearStart }}--{{ yearEnd - 1 }}</strong>
  20. <span class="to-right" @click="toRight">>></span>
  21. </div>
  22. <div class="year-picker__year-content">
  23. <span
  24. v-for="(item, index) in yearList"
  25. :key="index"
  26. :class="{ 'year-text': 1, 'year-disable': yearDis(item) }"
  27. @click="chooseYear(item)"
  28. >{{ item }}
  29. </span>
  30. </div>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. import components from './_import-components/km-year-import'
  36. export default {
  37. name: 'KmYear',
  38. components,
  39. props: {
  40. yearDisable: {
  41. type: String,
  42. default: 'no',
  43. },
  44. value: {
  45. type: Number,
  46. default: null,
  47. },
  48. },
  49. metaInfo: {
  50. title: 'OaYear',
  51. },
  52. data() {
  53. return {
  54. yearLists: [],
  55. year: '',
  56. showYearContent: false,
  57. yearStart: 2010,
  58. yearEnd: 2030,
  59. }
  60. },
  61. computed: {
  62. yearDis() {
  63. return function(y) {
  64. switch (this.yearDisable) {
  65. case 'before':
  66. return y < new Date().getFullYear()
  67. case 'no':
  68. return false
  69. case 'after': {
  70. return y > new Date().getFullYear()
  71. }
  72. }
  73. }
  74. },
  75. yearList() {
  76. const arr = []
  77. for (let i = this.yearStart; i < this.yearEnd; i++) {
  78. arr.push(i)
  79. }
  80. return arr
  81. },
  82. },
  83. methods: {
  84. inputFocus() {
  85. this.showYearContent = true
  86. },
  87. inputBlur() {
  88. this.showYearContent = false
  89. },
  90. chooseYear(year) {
  91. if (year > this.yearDis) return
  92. this.showYearContent = false
  93. this.year = year + '年'
  94. this.$emit('input', year)
  95. },
  96. toLeft() {
  97. this.yearStart -= 20
  98. this.yearEnd -= 20
  99. },
  100. toRight() {
  101. this.yearStart += 20
  102. this.yearEnd += 20
  103. },
  104. },
  105. }
  106. </script>
  107. <style module lang="scss">
  108. @use '@/common/design' as *;
  109. </style>