-
Notifications
You must be signed in to change notification settings - Fork 0
/
uni-rate.vue
150 lines (146 loc) · 3.13 KB
/
uni-rate.vue
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
142
143
144
145
146
147
148
149
150
<template>
<view class="uni-rate">
<view :key="index" :style="{ marginLeft: margin + 'px' }" @click="_onClick(index)" class="uni-rate__icon" v-for="(star, index) in stars">
<uni-icons :color="color" :size="size" :type="isFill ? 'star-filled' : 'star'" />
<!-- #ifdef APP-NVUE -->
<view :style="{ width: star.activeWitch.replace('%','')*size/100+'px'}" class="uni-rate__icon-on">
<uni-icons style="text-align: left;" :color="activeColor" :size="size" type="star-filled" />
</view>
<!-- #endif -->
<!-- #ifndef APP-NVUE -->
<view :style="{ width: star.activeWitch,top:-size/2+'px' }" class="uni-rate__icon-on">
<uni-icons :color="activeColor" :size="size" type="star-filled" />
</view>
<!-- #endif -->
</view>
</view>
</template>
<script>
import uniIcons from "../uni-icons/uni-icons.vue";
export default {
name: "UniRate",
components: {
uniIcons
},
props: {
isFill: {
// 星星的类型,是否镂空
type: [Boolean, String],
default: true
},
color: {
// 星星的颜色
type: String,
default: "#ececec"
},
activeColor: {
// 星星选中状态颜色
type: String,
default: "#ffca3e"
},
size: {
// 星星的大小
type: [Number, String],
default: 24
},
value: {
// 当前评分
type: [Number, String],
default: 0
},
max: {
// 最大评分
type: [Number, String],
default: 5
},
margin: {
// 星星的间距
type: [Number, String],
default: 0
},
disabled: {
// 是否可点击
type: [Boolean, String],
default: false
}
},
data() {
return {
valueSync: ""
};
},
computed: {
stars() {
const value = this.valueSync ? this.valueSync : 0;
const starList = [];
const floorValue = Math.floor(value);
const ceilValue = Math.ceil(value);
// console.log("ceilValue: " + ceilValue);
// console.log("floorValue: " + floorValue);
for (let i = 0; i < this.max; i++) {
if (floorValue > i) {
starList.push({
activeWitch: "100%"
});
} else if (ceilValue - 1 === i) {
starList.push({
activeWitch: (value - floorValue) * 100 + "%"
});
} else {
starList.push({
activeWitch: "0"
});
}
}
console.log("starList[4]: " + starList[4].activeWitch);
return starList;
}
},
created() {
this.valueSync = Number(this.value);
},
methods: {
_onClick(index) {
if (this.disabled) {
return;
}
//this.valueSync = index + 1;
this.valueSync?this.valueSync = index :this.valueSync = index +1
this.$emit("change", {
value: this.valueSync
});
}
},
//
watch:{
value:function(now,old,){
if(now != old){
this.valueSync = Number(now);
}
}
}
};
</script>
<style lang="scss" scoped>
.uni-rate {
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
line-height: 0;
font-size: 0;
flex-direction: row;
}
.uni-rate__icon {
position: relative;
line-height: 0;
font-size: 0;
}
.uni-rate__icon-on {
overflow: hidden;
position: absolute;
top: 0;
left: 0;
line-height: 1;
text-align: left;
}
</style>