Blame view

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