forked from kouchao/vue-layui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate.vue
More file actions
141 lines (139 loc) · 2.89 KB
/
rate.vue
File metadata and controls
141 lines (139 loc) · 2.89 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<template>
<div class="layui-inline">
<ul
class="layui-rate"
:readonly="disabled"
@mouseleave="handleMouseLeave()"
>
<li
v-for="(item, index) in rates"
:key="item"
class="layui-inline"
>
<i
class="layui-icon"
:class="[
{
'layui-icon-rate-solid': item == 1,
'layui-icon-rate-half': item == 0.5,
'layui-icon-rate': item == 0
},
'layui-co-' + theme
]"
:style="color ? 'color: ' + color : ''"
@mousemove="handleMouseMove(index, $event)"
@click="handleClick()"
/>
</li>
</ul>
<span
v-if="showText || showScore"
class="layui-inline"
>
<span v-if="showScore">
<slot :rate="value" />
</span>
<span v-if="showText && !showScore && texts">{{
texts[value] || ""
}}</span>
</span>
</div>
</template>
<script>
export default {
name: 'LayRate',
props: {
max: {
type: Number,
default: () => 5
},
disabled: {
type: Boolean,
default: () => false
},
allowHalf: {
type: Boolean,
default: () => false
},
value: {
type: Number,
default: () => 0
},
// 是否显示当前分数,show-score 和 show-text 不能同时为真 show-score优先展示
showScore: {
type: Boolean,
default: () => false
},
// 是否显示文字,show-score 和 show-text 不能同时为真 show-score优先展示,必须定义texts
showText: {
type: Boolean,
default: () => false
},
texts: {
type: Object,
default: () => []
},
theme: {
type: String,
default: ''
},
color: {
type: String,
default: ''
}
},
data () {
return {
rates: [],
rate: 0
};
},
watch: {
value () {
this.rate = this.value;
this.setRates();
}
},
mounted () {
this.rate = this.value;
this.setRates();
},
methods: {
setRates () {
const { rate, max, allowHalf } = this;
const rates = [];
for (let i = 0; i < max; i++) {
if (rate - i > 0) {
rate - i < 1 && allowHalf ? rates.push(0.5) : rates.push(1);
} else {
rates.push(0);
}
}
this.rates = rates;
},
handleMouseMove (key, e) {
if (this.disabled) {
return false;
}
const offset = e.offsetX > 10 || !this.allowHalf ? 1 : 0.5;
this.rate = key + offset;
this.setRates();
},
handleMouseLeave () {
if (this.disabled) {
return false;
}
this.rate = this.value;
this.setRates();
},
handleClick () {
if (this.disabled) {
return false;
}
this.$emit('input', this.rate);
this.$emit('change', this.rate);
}
}
};
</script>
<style></style>