u-rate.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 ? activeIcon : inactiveIcon"
  6. @click="click(index + 1, $event)"
  7. :color="activeIndex > index ? activeColor : inactiveColor"
  8. :style="{
  9. fontSize: size + 'rpx',
  10. padding: `0 ${gutter / 2 + 'rpx'}`
  11. }"
  12. ></u-icon>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * rate 评分
  19. * @description 该组件一般用于满意度调查,星型评分的场景
  20. * @tutorial https://www.uviewui.com/components/rate.html
  21. * @property {String Number} count 最多可选的星星数量(默认5)
  22. * @property {String Number} current 默认选中的星星数量(默认0)
  23. * @property {Boolean} disabled 是否禁止用户操作(默认false)
  24. * @property {String Number} size 星星的大小,单位rpx(默认32)
  25. * @property {String} inactive-color 未选中星星的颜色(默认#b2b2b2)
  26. * @property {String} active-color 选中的星星颜色(默认#FA3534)
  27. * @property {String} active-icon 选中时的图标名,只能为uView的内置图标(默认star-fill)
  28. * @property {String} inactive-icon 未选中时的图标名,只能为uView的内置图标(默认star)
  29. * @property {String} gutter 星星之间的距离(默认10)
  30. * @property {String Number} min-count 最少选中星星的个数(默认0)
  31. * @property {Boolean} allow-half 是否允许半星选择(默认false)
  32. * @event {Function} change 选中的星星发生变化时触发
  33. * @example <u-rate :count="count" :current="2"></u-rate>
  34. */
  35. export default {
  36. name: 'u-rate',
  37. props: {
  38. // 要显示的星星数量
  39. count: {
  40. type: [Number, String],
  41. default: 5
  42. },
  43. // 当前需要默认选中的星星(选中的个数)
  44. current: {
  45. type: [Number, String],
  46. default: 0
  47. },
  48. // 是否不可选中
  49. disabled: {
  50. type: Boolean,
  51. default: false
  52. },
  53. // 星星的大小,单位rpx
  54. size: {
  55. type: [Number, String],
  56. default: 32
  57. },
  58. // 未选中时的颜色
  59. inactiveColor: {
  60. type: String,
  61. default: '#b2b2b2'
  62. },
  63. // 选中的颜色
  64. activeColor: {
  65. type: String,
  66. default: '#FA3534'
  67. },
  68. // 星星之间的间距,单位rpx
  69. gutter: {
  70. type: [Number, String],
  71. default: 10
  72. },
  73. // 最少能选择的星星个数
  74. minCount: {
  75. type: [Number, String],
  76. default: 0
  77. },
  78. // 是否允许半星(功能尚未实现)
  79. allowHalf: {
  80. type: Boolean,
  81. default: false
  82. },
  83. // 选中时的图标(星星)
  84. activeIcon: {
  85. type: String,
  86. default: 'star-fill'
  87. },
  88. // 未选中时的图标(星星)
  89. inactiveIcon: {
  90. type: String,
  91. default: 'star'
  92. }
  93. },
  94. data() {
  95. return {
  96. // 生成一个唯一id,否则一个页面多个评分组件,会造成冲突
  97. elId: this.$u.guid(),
  98. elClass: this.$u.guid(),
  99. starBoxLeft: 0, // 评分盒子左边到屏幕左边的距离,用于滑动选择时计算距离
  100. activeIndex: this.current, // 当前激活的星星的index
  101. starWidth: 0, // 每个星星的宽度
  102. starWidthArr: [] //每个星星最右边到组件盒子最左边的距离
  103. };
  104. },
  105. watch: {
  106. current(val) {
  107. this.activeIndex = val;
  108. }
  109. },
  110. methods: {
  111. // 获取评分组件盒子的布局信息
  112. getElRectById() {
  113. // uView封装的获取节点的方法,详见文档
  114. this.$u.getRect('#' + this.elId).then(res => {
  115. this.starBoxLeft = res.left;
  116. })
  117. },
  118. // 获取单个星星的尺寸
  119. getElRectByClass() {
  120. // uView封装的获取节点的方法,详见文档
  121. this.$u.getRect('.' + this.elClass).then(res => {
  122. this.starWidth = res.width;
  123. // 把每个星星右边到组件盒子左边的距离放入数组中
  124. for (let i = 0; i < this.count; i++) {
  125. this.starWidthArr[i] = (i + 1) * this.starWidth;
  126. }
  127. })
  128. },
  129. // 手指滑动
  130. touchMove(e) {
  131. if (this.disabled) {
  132. return;
  133. }
  134. if (!e.changedTouches[0]) {
  135. return;
  136. }
  137. const movePageX = e.changedTouches[0].pageX;
  138. // 滑动点相对于评分盒子左边的距离
  139. const distance = movePageX - this.starBoxLeft;
  140. // 如果滑动到了评分盒子的左边界,就设置为0星
  141. if (distance <= 0) {
  142. this.activeIndex = 0;
  143. }
  144. // 滑动的距离,相当于多少颗星星
  145. let index = Math.ceil(distance / this.starWidth);
  146. this.activeIndex = index > this.count ? this.count : index;
  147. // 对最少颗星星的限制
  148. if (this.activeIndex < this.minCount) this.activeIndex = this.minCount;
  149. this.$emit('change', this.activeIndex);
  150. },
  151. // 通过点击,直接选中
  152. click(index, e) {
  153. if (this.disabled) {
  154. return;
  155. }
  156. // 半星选择,尚未实现
  157. if (this.allowHalf) {
  158. }
  159. // 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价
  160. if (index == 1) {
  161. if (this.activeIndex == 1) this.activeIndex = 0;
  162. else this.activeIndex = 1;
  163. } else {
  164. this.activeIndex = index;
  165. }
  166. // 对最少颗星星的限制
  167. if (this.activeIndex < this.minCount) this.activeIndex = this.minCount;
  168. this.$emit('change', this.activeIndex);
  169. }
  170. },
  171. mounted() {
  172. this.getElRectById();
  173. this.getElRectByClass();
  174. }
  175. };
  176. </script>
  177. <style scoped lang="scss">
  178. .u-rate {
  179. display: -webkit-inline-flex;
  180. display: inline-flex;
  181. align-items: center;
  182. margin: 0;
  183. padding: 0;
  184. }
  185. .u-icon {
  186. box-sizing: border-box;
  187. }
  188. </style>