Blame view

src/components/rate/rate.vue 3.78 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
          </div>
6bfdf8f8   梁灏   release 0.9.16
10
          <div :class="[prefixCls + '-text']" v-if="showText" v-show="value > 0">
e5ac7925   梁灏   update Rate
11
12
              <slot>{{ value }} <template v-if="value <= 1">{{ t('i.rate.star') }}</template><template v-else>{{ t('i.rate.stars') }}</template></slot>
          </div>
49d380cf   梁灏   init Rate component
13
14
15
      </div>
  </template>
  <script>
e5ac7925   梁灏   update Rate
16
17
      import Locale from '../../mixins/locale';
  
49d380cf   梁灏   init Rate component
18
19
20
      const prefixCls = 'ivu-rate';
  
      export default {
e5ac7925   梁灏   update Rate
21
          mixins: [ Locale ],
49d380cf   梁灏   init Rate component
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
          props: {
              count: {
                  type: Number,
                  default: 5
              },
              value: {
                  type: Number,
                  default: 0
              },
              allowHalf: {
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
e5ac7925   梁灏   update Rate
38
39
40
41
              },
              showText: {
                  type: Boolean,
                  default: false
49d380cf   梁灏   init Rate component
42
43
44
45
46
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
962c40bd   梁灏   update Rate
47
48
49
                  hoverIndex: -1,
                  isHover: false,
                  isHalf: false
49d380cf   梁灏   init Rate component
50
51
52
53
54
55
56
57
58
              };
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`,
                      {
                          [`${prefixCls}-disabled`]: this.disabled
                      }
fa0241a5   梁灏   fixed #212
59
                  ];
49d380cf   梁灏   init Rate component
60
61
              }
          },
962c40bd   梁灏   update Rate
62
63
64
65
66
67
68
69
          watch: {
              value: {
                  immediate: true,
                  handler (val) {
                      this.setHalf(val);
                  }
              }
          },
49d380cf   梁灏   init Rate component
70
71
72
          methods: {
              starCls (value) {
                  const hoverIndex = this.hoverIndex;
962c40bd   梁灏   update Rate
73
74
                  const currentIndex = this.isHover ? hoverIndex : this.value;
  
49d380cf   梁灏   init Rate component
75
                  let full = false;
962c40bd   梁灏   update Rate
76
77
78
                  let isLast = false;
  
                  if (currentIndex > value) full = true;
49d380cf   梁灏   init Rate component
79
  
962c40bd   梁灏   update Rate
80
81
82
83
                  if (this.isHover) {
                      isLast = currentIndex === value + 1;
                  } else {
                      isLast = Math.ceil(this.value) === value + 1;
49d380cf   梁灏   init Rate component
84
85
86
87
88
                  }
  
                  return [
                      `${prefixCls}-star`,
                      {
962c40bd   梁灏   update Rate
89
90
                          [`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
                          [`${prefixCls}-star-half`]: isLast && this.isHalf,
49d380cf   梁灏   init Rate component
91
92
                          [`${prefixCls}-star-zero`]: !full
                      }
fa0241a5   梁灏   fixed #212
93
                  ];
49d380cf   梁灏   init Rate component
94
              },
962c40bd   梁灏   update Rate
95
              handleMousemove(value, event) {
49d380cf   梁灏   init Rate component
96
97
                  if (this.disabled) return;
  
962c40bd   梁灏   update Rate
98
                  this.isHover = true;
49d380cf   梁灏   init Rate component
99
                  if (this.allowHalf) {
962c40bd   梁灏   update Rate
100
101
                      const type = event.target.getAttribute('type') || false;
                      this.isHalf = type === 'half';
49d380cf   梁灏   init Rate component
102
                  } else {
962c40bd   梁灏   update Rate
103
                      this.isHalf = false;
49d380cf   梁灏   init Rate component
104
                  }
962c40bd   梁灏   update Rate
105
                  this.hoverIndex = value + 1;
49d380cf   梁灏   init Rate component
106
107
              },
              handleMouseleave () {
962c40bd   梁灏   update Rate
108
109
110
111
                  if (this.disabled) return;
  
                  this.isHover = false;
                  this.setHalf(this.value);
49d380cf   梁灏   init Rate component
112
113
                  this.hoverIndex = -1;
              },
962c40bd   梁灏   update Rate
114
115
116
117
118
119
120
121
122
123
              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
124
125
126
127
              }
          }
      };
  </script>