u-rate.vue 10 KB

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