rate.vue
3.37 KB
1
2
3
4
5
6
7
8
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
37
38
39
40
41
42
43
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
<div :class="classes" @mouseleave="handleMouseleave">
<div
v-for="item in count"
:class="starCls(item)"
@mousemove="handleMousemove(item, $event)"
@click="handleClick(item)">
<span :class="[prefixCls + '-star-content']" type="half"></span>
</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,
hoverIndex: -1,
isHover: false,
isHalf: false
};
},
computed: {
classes () {
return [
`${prefixCls}`,
{
[`${prefixCls}-disabled`]: this.disabled
}
];
}
},
watch: {
value: {
immediate: true,
handler (val) {
this.setHalf(val);
}
}
},
methods: {
starCls (value) {
const hoverIndex = this.hoverIndex;
const currentIndex = this.isHover ? hoverIndex : this.value;
let full = false;
let isLast = false;
if (currentIndex > value) full = true;
if (this.isHover) {
isLast = currentIndex === value + 1;
} else {
isLast = Math.ceil(this.value) === value + 1;
}
return [
`${prefixCls}-star`,
{
[`${prefixCls}-star-full`]: (!isLast && full) || (isLast && !this.isHalf),
[`${prefixCls}-star-half`]: isLast && this.isHalf,
[`${prefixCls}-star-zero`]: !full
}
];
},
handleMousemove(value, event) {
if (this.disabled) return;
this.isHover = true;
if (this.allowHalf) {
const type = event.target.getAttribute('type') || false;
this.isHalf = type === 'half';
} else {
this.isHalf = false;
}
this.hoverIndex = value + 1;
},
handleMouseleave () {
if (this.disabled) return;
this.isHover = false;
this.setHalf(this.value);
this.hoverIndex = -1;
},
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);
}
}
};
</script>