blob: 5e18a58d8959d51c9e9b74fc34f6378942fd0341 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view>
3 <view
4 ref="uni-rate"
5 class="uni-rate"
6 >
7 <view
8 class="uni-rate__icon"
9 :style="{ 'margin-right': margin + 'px' }"
10 v-for="(star, index) in stars"
11 :key="index"
12 @touchstart.stop="touchstart"
13 @touchmove.stop="touchmove"
14 >
15 <uni-icons
16 :color="color"
17 :size="size"
18 :type="isFill ? 'star-filled' : 'star'"
19 />
20 <!-- #ifdef APP-NVUE -->
21 <view
22 :style="{ width: star.activeWitch.replace('%','')*size/100+'px'}"
23 class="uni-rate__icon-on"
24 >
25 <uni-icons
26 style="text-align: left;"
27 :color="disabled?'#ccc':activeColor"
28 :size="size"
29 type="star-filled"
30 />
31 </view>
32 <!-- #endif -->
33 <!-- #ifndef APP-NVUE -->
34 <view
35 :style="{ width: star.activeWitch}"
36 class="uni-rate__icon-on"
37 >
38 <uni-icons
39 :color="disabled?disabledColor:activeColor"
40 :size="size"
41 type="star-filled"
42 />
43 </view>
44 <!-- #endif -->
45 </view>
46 </view>
47 </view>
48</template>
49
50<script>
51 // #ifdef APP-NVUE
52 const dom = uni.requireNativePlugin('dom');
53 // #endif
54 import uniIcons from "../uni-icons/uni-icons.vue";
55 /**
56 * Rate 评分
57 * @description 评分组件
58 * @tutorial https://ext.dcloud.net.cn/plugin?id=33
59 * @property {Boolean} isFill = [true|false] 星星的类型,是否为实心类型, 默认为实心
60 * @property {String} color 未选中状态的星星颜色,默认为 "#ececec"
61 * @property {String} activeColor 选中状态的星星颜色,默认为 "#ffca3e"
62 * @property {String} disabledColor 禁用状态的星星颜色,默认为 "#c0c0c0"
63 * @property {Number} size 星星的大小
64 * @property {Number} value/v-model 当前评分
65 * @property {Number} max 最大评分评分数量,目前一分一颗星
66 * @property {Number} margin 星星的间距,单位 px
67 * @property {Boolean} disabled = [true|false] 是否为禁用状态,默认为 false
68 * @property {Boolean} readonly = [true|false] 是否为只读状态,默认为 false
69 * @property {Boolean} allowHalf = [true|false] 是否实现半星,默认为 false
70 * @property {Boolean} touchable = [true|false] 是否支持滑动手势,默认为 true
71 * @event {Function} change uniRate 的 value 改变时触发事件,e={value:Number}
72 */
73
74 export default {
75 components: {
76 uniIcons
77 },
78 name: "UniRate",
79 props: {
80 isFill: {
81 // 星星的类型,是否镂空
82 type: [Boolean, String],
83 default: true
84 },
85 color: {
86 // 星星未选中的颜色
87 type: String,
88 default: "#ececec"
89 },
90 activeColor: {
91 // 星星选中状态颜色
92 type: String,
93 default: "#ffca3e"
94 },
95 disabledColor: {
96 // 星星禁用状态颜色
97 type: String,
98 default: "#c0c0c0"
99 },
100 size: {
101 // 星星的大小
102 type: [Number, String],
103 default: 24
104 },
105 value: {
106 // 当前评分
107 type: [Number, String],
108 default: 1
109 },
110 max: {
111 // 最大评分
112 type: [Number, String],
113 default: 5
114 },
115 margin: {
116 // 星星的间距
117 type: [Number, String],
118 default: 0
119 },
120 disabled: {
121 // 是否可点击
122 type: [Boolean, String],
123 default: false
124 },
125 readonly: {
126 // 是否只读
127 type: [Boolean, String],
128 default: false
129 },
130 allowHalf: {
131 // 是否显示半星
132 type: [Boolean, String],
133 default: false
134 },
135 touchable: {
136 // 是否支持滑动手势
137 type: [Boolean, String],
138 default: true
139 }
140 },
141 data() {
142 return {
143 valueSync: ""
144 };
145 },
146 watch: {
147 value(newVal) {
148 this.valueSync = Number(newVal);
149 }
150 },
151 computed: {
152 stars() {
153 const value = this.valueSync ? this.valueSync : 0;
154 const starList = [];
155 const floorValue = Math.floor(value);
156 const ceilValue = Math.ceil(value);
157 for (let i = 0; i < this.max; i++) {
158 if (floorValue > i) {
159 starList.push({
160 activeWitch: "100%"
161 });
162 } else if (ceilValue - 1 === i) {
163 starList.push({
164 activeWitch: (value - floorValue) * 100 + "%"
165 });
166 } else {
167 starList.push({
168 activeWitch: "0"
169 });
170 }
171 }
172 return starList;
173 }
174 },
175 created() {
176 this.valueSync = Number(this.value);
177 this._rateBoxLeft = 0
178 this._oldValue = null
179 },
180 mounted() {
181 setTimeout(() => {
182 this._getSize()
183 }, 100)
184 },
185 methods: {
186 touchstart(e) {
187 if (this.readonly || this.disabled) return
188 const {
189 clientX,
190 screenX
191 } = e.changedTouches[0]
192 // TODO 做一下兼容,只有 Nvue 下才有 screenX,其他平台式 clientX
193 this._getRateCount(clientX || screenX)
194 },
195 touchmove(e) {
196 if (this.readonly || this.disabled || !this.touchable) return
197 const {
198 clientX,
199 screenX
200 } = e.changedTouches[0]
201 this._getRateCount(clientX || screenX)
202 },
203 /**
204 * 获取星星个数
205 */
206 _getRateCount(clientX) {
207 const rateMoveRange = clientX - this._rateBoxLeft
208 const index = parseInt(rateMoveRange / (this.size + this.margin))
209 const range = parseInt(rateMoveRange - ((this.size + this.margin) * index))
210 let value = 0
211
212 if (this.allowHalf) {
213 if (range > (this.size / 2)) {
214 value = index + 1
215 } else {
216 value = index + 0.5
217 }
218 } else {
219 value = index + 1
220 }
221
222 value = Math.max(0.5, Math.min(value, this.max))
223 if (this.valueSync !== value) {
224 this.valueSync = value
225 this._onChange()
226 }
227 // const rateCount = parseInt(rateMoveRange / (this.size / 2)) + 1
228 },
229
230 /**
231 * 触发动态修改
232 */
233 _onChange() {
234
235 this.$emit("input", this.valueSync);
236 this.$emit("change", {
237 value: this.valueSync
238 });
239 },
240 /**
241 * 获取星星距离屏幕左侧距离
242 */
243 _getSize() {
244 // #ifndef APP-NVUE
245 uni.createSelectorQuery()
246 .in(this)
247 .select('.uni-rate')
248 .boundingClientRect()
249 .exec(ret => {
250 if (ret) {
251 this._rateBoxLeft = ret[0].left
252 }
253 })
254 // #endif
255 // #ifdef APP-NVUE
256 dom.getComponentRect(this.$refs['uni-rate'], (ret) => {
257 const size = ret.size
258 if (size) {
259 this._rateBoxLeft = size.left
260 }
261 })
262 // #endif
263 }
264 }
265 };
266</script>
267
268<style
269 lang="scss"
270 scoped
271>
272 .uni-rate {
273 /* #ifndef APP-NVUE */
274 display: flex;
275 /* #endif */
276 line-height: 1;
277 font-size: 0;
278 flex-direction: row;
279 }
280
281 .uni-rate__icon {
282 position: relative;
283 line-height: 1;
284 font-size: 0;
285 }
286
287 .uni-rate__icon-on {
288 overflow: hidden;
289 position: absolute;
290 top: 0;
291 left: 0;
292 line-height: 1;
293 text-align: left;
294 }
295</style>