u-rate.vue 8.0 KB

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