Blame view

src/components/rate/rate.vue 3.37 KB
49d380cf   梁灏   init Rate component
1
2
  <template>
      <div :class="classes" @mouseleave="handleMouseleave">
962c40bd   梁灏   update Rate
3
4
5
6
7
8
          <div
              v-for="item in count"
              :class="starCls(item)"
              @mousemove="handleMousemove(item, $event)"
              @click="handleClick(item)">
              <span :class="[prefixCls + '-star-content']" type="half"></span>
49d380cf   梁灏   init Rate component
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
          </div>
      </div>
  </template>
  <script>
      const prefixCls = 'ivu-rate';
  
      export default {
          props: {
              count: {
                  type: Number,
                  default: 5
              },
              value: {
                  type: Number,
                  default: 0
              },
              allowHalf: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
962c40bd   梁灏   update Rate
37
38
39
                  hoverIndex: -1,
                  isHover: false,
                  isHalf: false
49d380cf   梁灏   init Rate component
40
41
42
43
44
45
46
47
48
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled
                      }
fa0241a5   梁灏   fixed #212
49
                  ];
49d380cf   梁灏   init Rate component
50
51
              }
          },
962c40bd   梁灏   update Rate
52
53
54
55
56
57
58
59
          watch: {
              value: {
                  immediate: true,
                  handler (val) {
                      this.setHalf(val);
                  }
              }
          },
49d380cf   梁灏   init Rate component
60
61
62
          methods: {
              starCls (value) {
                  const hoverIndex = this.hoverIndex;
962c40bd   梁灏   update Rate
63
64
                  const currentIndex = this.isHover ? hoverIndex : this.value;
  
49d380cf   梁灏   init Rate component
65
                  let full = false;
962c40bd   梁灏   update Rate
66
67
68
                  let isLast = false;
  
                  if (currentIndex > value) full = true;
49d380cf   梁灏   init Rate component
69
  
962c40bd   梁灏   update Rate
70
71
72
73
                  if (this.isHover) {
                      isLast = currentIndex === value + 1;
                  } else {
                      isLast = Math.ceil(this.value) === value + 1;
49d380cf   梁灏   init Rate component
74
75
76
77
78
                  }
  
                  return [
                      `${prefixCls}-star`,
                      {
962c40bd   梁灏   update Rate
79
80
                          [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
                          [`${prefixCls}-star-half`]: isLast && this.isHalf,
49d380cf   梁灏   init Rate component
81
82
                          [`${prefixCls}-star-zero`]: !full
                      }
fa0241a5   梁灏   fixed #212
83
                  ];
49d380cf   梁灏   init Rate component
84
              },
962c40bd   梁灏   update Rate
85
              handleMousemove(value, event) {
49d380cf   梁灏   init Rate component
86
87
                  if (this.disabled) return;
  
962c40bd   梁灏   update Rate
88
                  this.isHover = true;
49d380cf   梁灏   init Rate component
89
                  if (this.allowHalf) {
962c40bd   梁灏   update Rate
90
91
                      const type = event.target.getAttribute('type') || false;
                      this.isHalf = type === 'half';
49d380cf   梁灏   init Rate component
92
                  } else {
962c40bd   梁灏   update Rate
93
                      this.isHalf = false;
49d380cf   梁灏   init Rate component
94
                  }
962c40bd   梁灏   update Rate
95
                  this.hoverIndex = value + 1;
49d380cf   梁灏   init Rate component
96
97
              },
              handleMouseleave () {
962c40bd   梁灏   update Rate
98
99
100
101
                  if (this.disabled) return;
  
                  this.isHover = false;
                  this.setHalf(this.value);
49d380cf   梁灏   init Rate component
102
103
                  this.hoverIndex = -1;
              },
962c40bd   梁灏   update Rate
104
105
106
107
108
109
110
111
112
113
              setHalf (val) {
                  this.isHalf = val.toString().indexOf('.') >= 0;
              },
              handleClick (value) {
                  if (this.disabled) return;
                  value++;
                  if (this.isHalf) value -= 0.5;
                  this.value = value;
                  this.$emit('on-change', value);
                  this.$dispatch('on-form-change', value);
49d380cf   梁灏   init Rate component
114
115
116
117
              }
          }
      };
  </script>