Skip to content

Commit cc56616

Browse files
committed
新增:评分分级分层、评分半星显示
1 parent 279115b commit cc56616

File tree

1 file changed

+90
-35
lines changed

1 file changed

+90
-35
lines changed

uview-ui/components/u-rate/u-rate.vue

Lines changed: 90 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
<view class="u-rate" :id="elId" @touchmove.stop.prevent="touchMove">
33
<view class="u-star-wrap" v-for="(item, index) in count" :key="index" :class="[elClass]">
44
<u-icon
5-
:name="activeIndex > index ? activeIcon : inactiveIcon"
5+
:name="activeIndex > index ? elActiveIcon : inactiveIcon"
66
@click="click(index + 1, $event)"
7-
:color="activeIndex > index ? activeColor : inactiveColor"
7+
:color="activeIndex > index ? elActiveColor : inactiveColor"
88
:custom-style="{
99
fontSize: size + 'rpx',
1010
padding: `0 ${gutter / 2 + 'rpx'}`
1111
}"
12+
:custom-prefix="customPrefix"
13+
:show-decimal-icon="showDecimalIcon(index)"
14+
:percent="decimal"
1215
></u-icon>
1316
</view>
1417
</view>
1518
</template>
1619

17-
<script>
18-
/**
20+
<script>/**
1921
* rate 评分
2022
* @description 该组件一般用于满意度调查,星型评分的场景
2123
* @tutorial https://www.uviewui.com/components/rate.html
@@ -29,10 +31,13 @@
2931
* @property {String} inactive-icon 未选中时的图标名,只能为uView的内置图标(默认star)
3032
* @property {String} gutter 星星之间的距离(默认10)
3133
* @property {String Number} min-count 最少选中星星的个数(默认0)
34+
* @property {Array} colors 颜色分级显示,可以用不同颜色区分评分层级
35+
* @property {Array} icons 图标分级显示,可以用不同类型的icon区分评分层级
3236
* @property {Boolean} allow-half 是否允许半星选择(默认false)
3337
* @event {Function} change 选中的星星发生变化时触发
3438
* @example <u-rate :count="count" :current="2"></u-rate>
3539
*/
40+
3641
export default {
3742
name: 'u-rate',
3843
props: {
@@ -97,6 +102,25 @@ export default {
97102
inactiveIcon: {
98103
type: String,
99104
default: 'star'
105+
},
106+
// 自定义扩展前缀,方便用户扩展自己的图标库
107+
customPrefix: {
108+
type: String,
109+
default: 'uicon'
110+
},
111+
// 颜色分级显示,可以用不同颜色区分评分层级
112+
colors: {
113+
type: Array,
114+
default() {
115+
return []
116+
}
117+
},
118+
// 图标分级显示,可以用不同类型的icon区分评分层级
119+
icons: {
120+
type: Array,
121+
default() {
122+
return []
123+
}
100124
}
101125
},
102126
data() {
@@ -109,94 +133,125 @@ export default {
109133
activeIndex: this.value != -1 ? this.value : this.current,
110134
starWidth: 0, // 每个星星的宽度
111135
starWidthArr: [] //每个星星最右边到组件盒子最左边的距离
112-
};
136+
}
113137
},
114138
watch: {
115139
current(val) {
116-
this.activeIndex = val;
140+
this.activeIndex = val
117141
},
118142
value(val) {
119-
this.activeIndex = val;
143+
this.activeIndex = val
144+
}
145+
},
146+
computed: {
147+
decimal() {
148+
if (this.disabled) {
149+
return this.activeIndex * 100 % 100
150+
} else if (this.allowHalf) {
151+
return 50
152+
}
153+
},
154+
elActiveIcon() {
155+
const len = this.icons.length
156+
if (len) {
157+
const step = Math.round(this.activeIndex / Math.round(this.count / len))
158+
if (step > len) return this.icons[len - 1]
159+
return this.icons[step - 1]
160+
}
161+
return this.activeIcon
162+
},
163+
elActiveColor() {
164+
const len = this.colors.length
165+
if (len) {
166+
const step = Math.round(this.activeIndex / Math.round(this.count / len))
167+
if (step > len) return this.colors[len - 1]
168+
return this.colors[step - 1]
169+
}
170+
return this.activeColor
120171
}
121172
},
122173
methods: {
123174
// 获取评分组件盒子的布局信息
124175
getElRectById() {
125176
// uView封装的获取节点的方法,详见文档
126-
this.$uGetRect('#' + this.elId).then(res => {
127-
this.starBoxLeft = res.left;
177+
this.$u.getRect('#' + this.elId).then(res => {
178+
this.starBoxLeft = res.left
128179
})
129180
},
130181
// 获取单个星星的尺寸
131182
getElRectByClass() {
132183
// uView封装的获取节点的方法,详见文档
133-
this.$uGetRect('.' + this.elClass).then(res => {
134-
this.starWidth = res.width;
184+
this.$u.getRect('.' + this.elClass).then(res => {
185+
this.starWidth = res.width
135186
// 把每个星星右边到组件盒子左边的距离放入数组中
136187
for (let i = 0; i < this.count; i++) {
137-
this.starWidthArr[i] = (i + 1) * this.starWidth;
188+
this.starWidthArr[i] = (i + 1) * this.starWidth
138189
}
139190
})
140191
},
141192
// 手指滑动
142193
touchMove(e) {
143194
if (this.disabled) {
144-
return;
195+
return
145196
}
146197
if (!e.changedTouches[0]) {
147-
return;
198+
return
148199
}
149-
const movePageX = e.changedTouches[0].pageX;
200+
const movePageX = e.changedTouches[0].pageX
150201
// 滑动点相对于评分盒子左边的距离
151-
const distance = movePageX - this.starBoxLeft;
202+
const distance = movePageX - this.starBoxLeft
152203
153204
// 如果滑动到了评分盒子的左边界,就设置为0星
154205
if (distance <= 0) {
155-
this.activeIndex = 0;
206+
this.activeIndex = 0
156207
}
157208
// 滑动的距离,相当于多少颗星星
158-
let index = Math.ceil(distance / this.starWidth);
159-
this.activeIndex = index > this.count ? this.count : index;
209+
let index = Math.ceil(distance / this.starWidth)
210+
this.activeIndex = index > this.count ? this.count : index
160211
// 对最少颗星星的限制
161-
if (this.activeIndex < this.minCount) this.activeIndex = this.minCount;
162-
this.emitEvent();
212+
if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
213+
this.emitEvent()
163214
},
164215
// 通过点击,直接选中
165216
click(index, e) {
166217
if (this.disabled) {
167-
return;
218+
return
168219
}
169220
// 半星选择,尚未实现
170221
if (this.allowHalf) {
171222
}
172223
// 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价
173224
if (index == 1) {
174-
if (this.activeIndex == 1) this.activeIndex = 0;
175-
else this.activeIndex = 1;
225+
if (this.activeIndex == 1) {
226+
this.activeIndex = 0
227+
} else {
228+
this.activeIndex = 1
229+
}
176230
} else {
177-
this.activeIndex = index;
231+
this.activeIndex = index
178232
}
179233
// 对最少颗星星的限制
180-
if (this.activeIndex < this.minCount) this.activeIndex = this.minCount;
181-
this.emitEvent();
234+
if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
235+
this.emitEvent()
182236
},
183237
// 发出事件
184238
emitEvent() {
185239
// 发出change事件
186-
this.$emit('change', this.activeIndex);
240+
this.$emit('change', this.activeIndex)
187241
// 同时修改双向绑定的value的值
188-
if(this.value != -1) {
189-
this.$emit('input', this.activeIndex);
242+
if (this.value != -1) {
243+
this.$emit('input', this.activeIndex)
190244
}
245+
},
246+
showDecimalIcon(index) {
247+
return this.disabled && parseInt(this.activeIndex) === index
191248
}
192249
},
193250
mounted() {
194-
setTimeout(() => {
195-
this.getElRectById();
196-
this.getElRectByClass();
197-
}, 100)
251+
this.getElRectById()
252+
this.getElRectByClass()
198253
}
199-
};
254+
}
200255
</script>
201256

202257
<style scoped lang="scss">

0 commit comments

Comments
 (0)