guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame] | 1 | <template> |
| 2 | <view class="u-rate" :id="elId" @touchmove.stop.prevent="touchMove"> |
| 3 | <view class="u-star-wrap" v-for="(item, index) in count" :key="index" :class="[elClass]"> |
| 4 | <u-icon |
| 5 | :name="activeIndex > index ? elActiveIcon : inactiveIcon" |
| 6 | @click="click(index + 1, $event)" |
| 7 | :color="activeIndex > index ? elActiveColor : inactiveColor" |
| 8 | :custom-style="{ |
| 9 | fontSize: size + 'rpx', |
| 10 | padding: `0 ${gutter / 2 + 'rpx'}` |
| 11 | }" |
| 12 | :custom-prefix="customPrefix" |
| 13 | :show-decimal-icon="showDecimalIcon(index)" |
| 14 | :percent="decimal" |
| 15 | :inactive-color="inactiveColor" |
| 16 | ></u-icon> |
| 17 | </view> |
| 18 | </view> |
| 19 | </template> |
| 20 | |
| 21 | <script>/** |
| 22 | * rate 评分 |
| 23 | * @description 该组件一般用于满意度调查,星型评分的场景 |
| 24 | * @tutorial https://www.uviewui.com/components/rate.html |
| 25 | * @property {String Number} count 最多可选的星星数量(默认5) |
| 26 | * @property {String Number} current 默认选中的星星数量(默认0) |
| 27 | * @property {Boolean} disabled 是否禁止用户操作(默认false) |
| 28 | * @property {String Number} size 星星的大小,单位rpx(默认32) |
| 29 | * @property {String} inactive-color 未选中星星的颜色(默认#b2b2b2) |
| 30 | * @property {String} active-color 选中的星星颜色(默认#FA3534) |
| 31 | * @property {String} active-icon 选中时的图标名,只能为uView的内置图标(默认star-fill) |
| 32 | * @property {String} inactive-icon 未选中时的图标名,只能为uView的内置图标(默认star) |
| 33 | * @property {String} gutter 星星之间的距离(默认10) |
| 34 | * @property {String Number} min-count 最少选中星星的个数(默认0) |
| 35 | * @property {Boolean} allow-half 是否允许半星选择(默认false) |
| 36 | * @event {Function} change 选中的星星发生变化时触发 |
| 37 | * @example <u-rate :count="count" :current="2"></u-rate> |
| 38 | */ |
| 39 | |
| 40 | export default { |
| 41 | name: 'u-rate', |
| 42 | props: { |
| 43 | // 用于v-model双向绑定选中的星星数量 |
| 44 | // 1.4.5版新增 |
| 45 | value: { |
| 46 | type: [Number, String], |
| 47 | default: -1 |
| 48 | }, |
| 49 | // 要显示的星星数量 |
| 50 | count: { |
| 51 | type: [Number, String], |
| 52 | default: 5 |
| 53 | }, |
| 54 | // 当前需要默认选中的星星(选中的个数) |
| 55 | // 1.4.5后通过value双向绑定,不再建议使用此参数 |
| 56 | current: { |
| 57 | type: [Number, String], |
| 58 | default: 0 |
| 59 | }, |
| 60 | // 是否不可选中 |
| 61 | disabled: { |
| 62 | type: Boolean, |
| 63 | default: false |
| 64 | }, |
| 65 | // 星星的大小,单位rpx |
| 66 | size: { |
| 67 | type: [Number, String], |
| 68 | default: 32 |
| 69 | }, |
| 70 | // 未选中时的颜色 |
| 71 | inactiveColor: { |
| 72 | type: String, |
| 73 | default: '#b2b2b2' |
| 74 | }, |
| 75 | // 选中的颜色 |
| 76 | activeColor: { |
| 77 | type: String, |
| 78 | default: '#FA3534' |
| 79 | }, |
| 80 | // 星星之间的间距,单位rpx |
| 81 | gutter: { |
| 82 | type: [Number, String], |
| 83 | default: 10 |
| 84 | }, |
| 85 | // 最少能选择的星星个数 |
| 86 | minCount: { |
| 87 | type: [Number, String], |
| 88 | default: 0 |
| 89 | }, |
| 90 | // 是否允许半星(功能尚未实现) |
| 91 | allowHalf: { |
| 92 | type: Boolean, |
| 93 | default: false |
| 94 | }, |
| 95 | // 选中时的图标(星星) |
| 96 | activeIcon: { |
| 97 | type: String, |
| 98 | default: 'star-fill' |
| 99 | }, |
| 100 | // 未选中时的图标(星星) |
| 101 | inactiveIcon: { |
| 102 | type: String, |
| 103 | default: 'star' |
| 104 | }, |
| 105 | // 自定义扩展前缀,方便用户扩展自己的图标库 |
| 106 | customPrefix: { |
| 107 | type: String, |
| 108 | default: 'uicon' |
| 109 | }, |
| 110 | colors: { |
| 111 | type: Array, |
| 112 | default() { |
| 113 | return [] |
| 114 | } |
| 115 | }, |
| 116 | icons: { |
| 117 | type: Array, |
| 118 | default() { |
| 119 | return [] |
| 120 | } |
| 121 | } |
| 122 | }, |
| 123 | data() { |
| 124 | return { |
| 125 | // 生成一个唯一id,否则一个页面多个评分组件,会造成冲突 |
| 126 | elId: this.$u.guid(), |
| 127 | elClass: this.$u.guid(), |
| 128 | starBoxLeft: 0, // 评分盒子左边到屏幕左边的距离,用于滑动选择时计算距离 |
| 129 | // 当前激活的星星的index,如果存在value,优先使用value,因为它可以双向绑定(1.4.5新增) |
| 130 | activeIndex: this.value != -1 ? this.value : this.current, |
| 131 | starWidth: 0, // 每个星星的宽度 |
| 132 | starWidthArr: [] //每个星星最右边到组件盒子最左边的距离 |
| 133 | } |
| 134 | }, |
| 135 | watch: { |
| 136 | current(val) { |
| 137 | this.activeIndex = val |
| 138 | }, |
| 139 | value(val) { |
| 140 | this.activeIndex = val |
| 141 | } |
| 142 | }, |
| 143 | computed: { |
| 144 | decimal() { |
| 145 | if (this.disabled) { |
| 146 | return this.activeIndex * 100 % 100 |
| 147 | } else if (this.allowHalf) { |
| 148 | return 50 |
| 149 | } |
| 150 | }, |
| 151 | elActiveIcon() { |
| 152 | const len = this.icons.length |
| 153 | // 此处规则类似于下方的elActiveColor参数,都是根据一定的规则,显示不同的图标 |
| 154 | // 结果可能如此:icons参数传递了3个图标,当选中两个时,用第一个图标,4个时,用第二个图标 |
| 155 | // 第三个时,用第三个图标作为激活的图标 |
| 156 | if (len && len <= this.count) { |
| 157 | const step = Math.round(this.activeIndex / Math.round(this.count / len)) |
| 158 | if (step < 1) return this.icons[0] |
| 159 | if (step > len) return this.icons[len - 1] |
| 160 | return this.icons[step - 1] |
| 161 | } |
| 162 | return this.activeIcon |
| 163 | }, |
| 164 | elActiveColor() { |
| 165 | const len = this.colors.length |
| 166 | // 如果有设置colors参数(此参数用于将图标分段,比如一共5颗星,colors传3个颜色值,那么根据一定的规则,2颗星可能为第一个颜色 |
| 167 | // 4颗星为第二个颜色值,5颗星为第三个颜色值) |
| 168 | if (len && len <= this.count) { |
| 169 | const step = Math.round(this.activeIndex / Math.round(this.count / len)) |
| 170 | if (step < 1) return this.colors[0] |
| 171 | if (step > len) return this.colors[len - 1] |
| 172 | return this.colors[step - 1] |
| 173 | } |
| 174 | return this.activeColor |
| 175 | } |
| 176 | }, |
| 177 | methods: { |
| 178 | // 获取评分组件盒子的布局信息 |
| 179 | getElRectById() { |
| 180 | // uView封装的获取节点的方法,详见文档 |
| 181 | this.$u.getRect('#' + this.elId).then(res => { |
| 182 | this.starBoxLeft = res.left |
| 183 | }) |
| 184 | }, |
| 185 | // 获取单个星星的尺寸 |
| 186 | getElRectByClass() { |
| 187 | // uView封装的获取节点的方法,详见文档 |
| 188 | this.$u.getRect('.' + this.elClass).then(res => { |
| 189 | this.starWidth = res.width |
| 190 | // 把每个星星右边到组件盒子左边的距离放入数组中 |
| 191 | for (let i = 0; i < this.count; i++) { |
| 192 | this.starWidthArr[i] = (i + 1) * this.starWidth |
| 193 | } |
| 194 | }) |
| 195 | }, |
| 196 | // 手指滑动 |
| 197 | touchMove(e) { |
| 198 | if (this.disabled) { |
| 199 | return |
| 200 | } |
| 201 | if (!e.changedTouches[0]) { |
| 202 | return |
| 203 | } |
| 204 | const movePageX = e.changedTouches[0].pageX |
| 205 | // 滑动点相对于评分盒子左边的距离 |
| 206 | const distance = movePageX - this.starBoxLeft |
| 207 | |
| 208 | // 如果滑动到了评分盒子的左边界,就设置为0星 |
| 209 | if (distance <= 0) { |
| 210 | this.activeIndex = 0 |
| 211 | } |
| 212 | // 滑动的距离,相当于多少颗星星 |
| 213 | let index = Math.ceil(distance / this.starWidth) |
| 214 | this.activeIndex = index > this.count ? this.count : index |
| 215 | // 对最少颗星星的限制 |
| 216 | if (this.activeIndex < this.minCount) this.activeIndex = this.minCount |
| 217 | this.emitEvent() |
| 218 | }, |
| 219 | // 通过点击,直接选中 |
| 220 | click(index, e) { |
| 221 | if (this.disabled) { |
| 222 | return |
| 223 | } |
| 224 | // 半星选择,尚未实现 |
| 225 | if (this.allowHalf) { |
| 226 | } |
| 227 | // 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价 |
| 228 | if (index == 1) { |
| 229 | if (this.activeIndex == 1) { |
| 230 | this.activeIndex = 0 |
| 231 | } else { |
| 232 | this.activeIndex = 1 |
| 233 | } |
| 234 | } else { |
| 235 | this.activeIndex = index |
| 236 | } |
| 237 | // 对最少颗星星的限制 |
| 238 | if (this.activeIndex < this.minCount) this.activeIndex = this.minCount |
| 239 | this.emitEvent() |
| 240 | }, |
| 241 | // 发出事件 |
| 242 | emitEvent() { |
| 243 | // 发出change事件 |
| 244 | this.$emit('change', this.activeIndex) |
| 245 | // 同时修改双向绑定的value的值 |
| 246 | if (this.value != -1) { |
| 247 | this.$emit('input', this.activeIndex) |
| 248 | } |
| 249 | }, |
| 250 | showDecimalIcon(index) { |
| 251 | return this.disabled && parseInt(this.activeIndex) === index |
| 252 | } |
| 253 | }, |
| 254 | mounted() { |
| 255 | this.getElRectById() |
| 256 | this.getElRectByClass() |
| 257 | } |
| 258 | } |
| 259 | </script> |
| 260 | |
| 261 | <style scoped lang="scss"> |
| 262 | @import "../../libs/css/style.components.scss"; |
| 263 | |
| 264 | .u-rate { |
| 265 | display: -webkit-inline-flex; |
| 266 | display: inline-flex; |
| 267 | align-items: center; |
| 268 | margin: 0; |
| 269 | padding: 0; |
| 270 | } |
| 271 | |
| 272 | .u-icon { |
| 273 | box-sizing: border-box; |
| 274 | } |
| 275 | </style> |