u-rate.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view
  3. class="u-rate"
  4. :id="elId"
  5. ref="u-rate"
  6. :style="[$u.addStyle(customStyle)]"
  7. >
  8. <view
  9. class="u-rate__content"
  10. @touchmove.stop="touchMove"
  11. @touchend.stop="touchEnd"
  12. >
  13. <view
  14. class="u-rate__content__item"
  15. v-for="(item, index) in Number(count)"
  16. :key="index"
  17. :class="[elClass]"
  18. >
  19. <view
  20. class="u-rate__content__item__icon-wrap"
  21. ref="u-rate__content__item__icon-wrap"
  22. @tap.stop="clickHandler($event, index + 1)"
  23. >
  24. <u-icon
  25. :name="
  26. Math.floor(activeIndex) > index
  27. ? activeIcon
  28. : inactiveIcon
  29. "
  30. :color="
  31. disabled
  32. ? '#c8c9cc'
  33. : Math.floor(activeIndex) > index
  34. ? activeColor
  35. : inactiveColor
  36. "
  37. :custom-style="{
  38. 'padding-left': $u.addUnit(gutter / 2),
  39. 'padding-right': $u.addUnit(gutter / 2)
  40. }"
  41. :size="size"
  42. ></u-icon>
  43. </view>
  44. <view
  45. v-if="allowHalf"
  46. @tap.stop="clickHandler($event, index + 1)"
  47. class="u-rate__content__item__icon-wrap u-rate__content__item__icon-wrap--half"
  48. :style="[{
  49. width: $u.addUnit(rateWidth / 2),
  50. }]"
  51. ref="u-rate__content__item__icon-wrap"
  52. >
  53. <u-icon
  54. :name="
  55. Math.ceil(activeIndex) > index
  56. ? activeIcon
  57. : inactiveIcon
  58. "
  59. :color="
  60. disabled
  61. ? '#c8c9cc'
  62. : Math.ceil(activeIndex) > index
  63. ? activeColor
  64. : inactiveColor
  65. "
  66. :custom-style="{
  67. 'padding-left': $u.addUnit(gutter / 2),
  68. 'padding-right': $u.addUnit(gutter / 2)
  69. }"
  70. :size="size"
  71. ></u-icon>
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. </template>
  77. <script>
  78. import props from './props.js';
  79. import mixin from '../../libs/mixin/mixin'
  80. import mpMixin from '../../libs/mixin/mpMixin';
  81. // #ifdef APP-NVUE
  82. const dom = weex.requireModule("dom");
  83. // #endif
  84. /**
  85. * rate 评分
  86. * @description 该组件一般用于满意度调查,星型评分的场景
  87. * @tutorial https://uview.d3u.cn/components/rate.html
  88. * @property {String | Number} value 用于v-model双向绑定选中的星星数量 (默认 1 )
  89. * @property {String | Number} count 最多可选的星星数量 (默认 5 )
  90. * @property {Boolean} disabled 是否禁止用户操作 (默认 false )
  91. * @property {Boolean} readonly 是否只读 (默认 false )
  92. * @property {String | Number} size 星星的大小,单位px (默认 18 )
  93. * @property {String} inactiveColor 未选中星星的颜色 (默认 '#b2b2b2' )
  94. * @property {String} activeColor 选中的星星颜色 (默认 '#FA3534' )
  95. * @property {String | Number} gutter 星星之间的距离 (默认 4 )
  96. * @property {String | Number} minCount 最少选中星星的个数 (默认 1 )
  97. * @property {Boolean} allowHalf 是否允许半星选择 (默认 false )
  98. * @property {String} activeIcon 选中时的图标名,只能为uView的内置图标 (默认 'star-fill' )
  99. * @property {String} inactiveIcon 未选中时的图标名,只能为uView的内置图标 (默认 'star' )
  100. * @property {Boolean} touchable 是否可以通过滑动手势选择评分 (默认 'true' )
  101. * @property {Object} customStyle 组件的样式,对象形式
  102. * @event {Function} change 选中的星星发生变化时触发
  103. * @example <u-rate :count="count" :value="2"></u-rate>
  104. */
  105. export default {
  106. name: "u-rate",
  107. mixins: [mpMixin, mixin, props],
  108. data() {
  109. return {
  110. // 生成一个唯一id,否则一个页面多个评分组件,会造成冲突
  111. elId: uni.$u.guid(),
  112. elClass: uni.$u.guid(),
  113. rateBoxLeft: 0, // 评分盒子左边到屏幕左边的距离,用于滑动选择时计算距离
  114. // #ifdef VUE2
  115. activeIndex: this.value,
  116. // #endif
  117. // #ifdef VUE3
  118. activeIndex: this.modelValue,
  119. // #endif
  120. rateWidth: 0, // 每个星星的宽度
  121. // 标识是否正在滑动,由于iOS事件上touch比click先触发,导致快速滑动结束后,接着触发click,导致事件混乱而出错
  122. moving: false,
  123. };
  124. },
  125. watch: {
  126. // #ifdef VUE2
  127. value(val) {
  128. this.activeIndex = val;
  129. },
  130. // #endif
  131. // #ifdef VUE3
  132. modelValue(val) {
  133. this.activeIndex = val;
  134. },
  135. // #endif
  136. activeIndex: 'emitEvent'
  137. },
  138. // #ifdef VUE3
  139. emits: ['update:modelValue', 'change'],
  140. // #endif
  141. methods: {
  142. init() {
  143. uni.$u.sleep().then(() => {
  144. this.getRateItemRect();
  145. this.getRateIconWrapRect();
  146. })
  147. },
  148. // 获取评分组件盒子的布局信息
  149. async getRateItemRect() {
  150. await uni.$u.sleep();
  151. // uView封装的获取节点的方法,详见文档
  152. // #ifndef APP-NVUE
  153. this.$uGetRect("#" + this.elId).then((res) => {
  154. this.rateBoxLeft = res.left;
  155. });
  156. // #endif
  157. // #ifdef APP-NVUE
  158. dom.getComponentRect(this.$refs["u-rate"], (res) => {
  159. this.rateBoxLeft = res.size.left;
  160. });
  161. // #endif
  162. },
  163. // 获取单个星星的尺寸
  164. getRateIconWrapRect() {
  165. // uView封装的获取节点的方法,详见文档
  166. // #ifndef APP-NVUE
  167. this.$uGetRect("." + this.elClass).then((res) => {
  168. this.rateWidth = res.width;
  169. });
  170. // #endif
  171. // #ifdef APP-NVUE
  172. dom.getComponentRect(
  173. this.$refs["u-rate__content__item__icon-wrap"][0],
  174. (res) => {
  175. this.rateWidth = res.size.width;
  176. }
  177. );
  178. // #endif
  179. },
  180. // 手指滑动
  181. touchMove(e) {
  182. // 如果禁止通过手动滑动选择,返回
  183. if (!this.touchable) {
  184. return;
  185. }
  186. this.preventEvent(e);
  187. const x = e.changedTouches[0].pageX;
  188. this.getActiveIndex(x);
  189. },
  190. // 停止滑动
  191. touchEnd(e) {
  192. // 如果禁止通过手动滑动选择,返回
  193. if (!this.touchable) {
  194. return;
  195. }
  196. this.preventEvent(e);
  197. const x = e.changedTouches[0].pageX;
  198. this.getActiveIndex(x);
  199. },
  200. // 通过点击,直接选中
  201. clickHandler(e, index) {
  202. // ios上,moving状态取消事件触发
  203. if (uni.$u.os() === "ios" && this.moving) {
  204. return;
  205. }
  206. this.preventEvent(e);
  207. let x = 0;
  208. // 点击时,在nvue上,无法获得点击的坐标,所以无法实现点击半星选择
  209. // #ifndef APP-NVUE
  210. x = e.changedTouches[0].pageX;
  211. // #endif
  212. // #ifdef APP-NVUE
  213. // nvue下,无法通过点击获得坐标信息,这里通过元素的位置尺寸值模拟坐标
  214. x = index * this.rateWidth + this.rateBoxLeft;
  215. // #endif
  216. this.getActiveIndex(x,true);
  217. },
  218. // 发出事件
  219. emitEvent() {
  220. // 发出change事件
  221. this.$emit("change", this.activeIndex);
  222. // 同时修改双向绑定的value的值
  223. // #ifdef VUE2
  224. this.$emit("input", this.activeIndex);
  225. // #endif
  226. // #ifdef VUE3
  227. this.$emit("update:modelValue", this.activeIndex);
  228. // #endif
  229. },
  230. // 获取当前激活的评分图标
  231. getActiveIndex(x,isClick = false) {
  232. if (this.disabled || this.readonly) {
  233. return;
  234. }
  235. // 判断当前操作的点的x坐标值,是否在允许的边界范围内
  236. const allRateWidth = this.rateWidth * this.count + this.rateBoxLeft;
  237. // 如果小于第一个图标的左边界,设置为最小值,如果大于所有图标的宽度,则设置为最大值
  238. x = uni.$u.range(this.rateBoxLeft, allRateWidth, x) - this.rateBoxLeft
  239. // 滑动点相对于评分盒子左边的距离
  240. const distance = x;
  241. // 滑动的距离,相当于多少颗星星
  242. let index;
  243. // 判断是否允许半星
  244. if (this.allowHalf) {
  245. index = Math.floor(distance / this.rateWidth);
  246. // 取余,判断小数的区间范围
  247. const decimal = distance % this.rateWidth;
  248. if (decimal <= this.rateWidth / 2 && decimal > 0) {
  249. index += 0.5;
  250. } else if (decimal > this.rateWidth / 2) {
  251. index++;
  252. }
  253. } else {
  254. index = Math.floor(distance / this.rateWidth);
  255. // 取余,判断小数的区间范围
  256. const decimal = distance % this.rateWidth;
  257. // 非半星时,只有超过了图标的一半距离,才认为是选择了这颗星
  258. if (isClick){
  259. if (decimal > 0) index++;
  260. } else {
  261. if (decimal > this.rateWidth / 2) index++;
  262. }
  263. }
  264. this.activeIndex = Math.min(index, this.count);
  265. // 对最少颗星星的限制
  266. if (this.activeIndex < this.minCount) {
  267. this.activeIndex = this.minCount;
  268. }
  269. // 设置延时为了让click事件在touchmove之前触发
  270. setTimeout(() => {
  271. this.moving = true;
  272. }, 10);
  273. // 一定时间后,取消标识为移动中状态,是为了让click事件无效
  274. setTimeout(() => {
  275. this.moving = false;
  276. }, 10);
  277. },
  278. },
  279. mounted() {
  280. this.init();
  281. },
  282. };
  283. </script>
  284. <style lang="scss" scoped>
  285. @import "../../libs/css/components.scss";
  286. $u-rate-margin: 0 !default;
  287. $u-rate-padding: 0 !default;
  288. $u-rate-item-icon-wrap-half-top: 0 !default;
  289. $u-rate-item-icon-wrap-half-left: 0 !default;
  290. .u-rate {
  291. @include flex;
  292. align-items: center;
  293. margin: $u-rate-margin;
  294. padding: $u-rate-padding;
  295. /* #ifndef APP-NVUE */
  296. touch-action: none;
  297. /* #endif */
  298. &__content {
  299. @include flex;
  300. &__item {
  301. position: relative;
  302. &__icon-wrap {
  303. &--half {
  304. position: absolute;
  305. overflow: hidden;
  306. top: $u-rate-item-icon-wrap-half-top;
  307. left: $u-rate-item-icon-wrap-half-left;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. .u-icon {
  314. /* #ifndef APP-NVUE */
  315. box-sizing: border-box;
  316. /* #endif */
  317. }
  318. </style>