Blame view

src/components/date-picker/base/year-table.vue 2.45 KB
17e1fcf1   梁灏   init DatePicker
1
  <template>
c46f385a   梁灏   update DatePicker
2
3
4
      <div :class="classes" @click="handleClick">
          <span :class="getCellCls(cell)" v-for="cell in cells"><em :index="$index">{{ cell.text }}</em></span>
      </div>
17e1fcf1   梁灏   init DatePicker
5
6
  </template>
  <script>
c46f385a   梁灏   update DatePicker
7
8
9
      import { deepCopy } from '../../../utils/assist';
      const prefixCls = 'ivu-date-picker-cells';
  
17e1fcf1   梁灏   init DatePicker
10
      export default {
c46f385a   梁灏   update DatePicker
11
12
13
          props: {
              date: {},
              year: {},
13be4434   梁灏   update DatePicker
14
15
16
17
              disabledDate: {},
              selectionMode: {
                  default: 'year'
              }
17e1fcf1   梁灏   init DatePicker
18
          },
c46f385a   梁灏   update DatePicker
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      `${prefixCls}-year`
                  ]
              },
              startYear() {
                  return Math.floor(this.year / 10) * 10;
              },
              cells () {
                  let cells = [];
                  const cell_tmpl = {
                      text: '',
                      selected: false,
                      disabled: false
                  };
  
                  for (let i = 0; i < 10; i++) {
                      const cell = deepCopy(cell_tmpl);
                      cell.text = this.startYear + i;
  
                      const date = new Date(this.date);
                      date.setFullYear(cell.text);
13be4434   梁灏   update DatePicker
43
                      cell.disabled = typeof this.disabledDate === 'function' && this.disabledDate(date) && this.selectionMode === 'year';
c46f385a   梁灏   update DatePicker
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  
                      cell.selected = Number(this.year) === cell.text;
                      cells.push(cell);
                  }
  
                  return cells;
              }
          },
          methods: {
              getCellCls (cell) {
                  return [
                      `${prefixCls}-cell`,
                      {
                          [`${prefixCls}-cell-selected`]: cell.selected,
                          [`${prefixCls}-cell-disabled`]: cell.disabled
                      }
                  ]
              },
              nextTenYear() {
                  this.$emit('on-pick', Number(this.year) + 10, false);
              },
              prevTenYear() {
                  this.$emit('on-pick', Number(this.year) - 10, false);
              },
              handleClick (event) {
                  const target = event.target;
                  if (target.tagName === 'EM') {
                      const cell = this.cells[parseInt(event.target.getAttribute('index'))];
                      if (cell.disabled) return;
344131a7   梁灏   update DatePicker
73
  
c46f385a   梁灏   update DatePicker
74
75
76
77
                      this.$emit('on-pick', cell.text);
                  }
              }
          }
17e1fcf1   梁灏   init DatePicker
78
79
      }
  </script>