uni-rate.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view>
  3. <view ref="uni-rate" class="uni-rate">
  4. <view class="uni-rate__icon" :style="{ 'margin-right': margin + 'px' }" v-for="(star, index) in stars" :key="index" @touchstart.stop="touchstart" @touchmove.stop="touchmove">
  5. <uni-icons :color="color" :size="size" :type="isFill ? 'star-filled' : 'star'" />
  6. <!-- #ifdef APP-NVUE -->
  7. <view :style="{ width: star.activeWitch.replace('%','')*size/100+'px'}" class="uni-rate__icon-on">
  8. <uni-icons style="text-align: left;" :color="disabled?'#ccc':activeColor" :size="size" type="star-filled" />
  9. </view>
  10. <!-- #endif -->
  11. <!-- #ifndef APP-NVUE -->
  12. <view :style="{ width: star.activeWitch}" class="uni-rate__icon-on">
  13. <uni-icons :color="disabled?disabledColor:activeColor" :size="size" type="star-filled" />
  14. </view>
  15. <!-- #endif -->
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. // #ifdef APP-NVUE
  22. const dom = uni.requireNativePlugin('dom');
  23. // #endif
  24. import uniIcons from "../uni-icons/uni-icons.vue";
  25. /**
  26. * Rate 评分
  27. * @description 评分组件
  28. * @tutorial https://ext.dcloud.net.cn/plugin?id=33
  29. * @property {Boolean} isFill = [true|false] 星星的类型,是否为实心类型, 默认为实心
  30. * @property {String} color 未选中状态的星星颜色,默认为 "#ececec"
  31. * @property {String} activeColor 选中状态的星星颜色,默认为 "#ffca3e"
  32. * @property {String} disabledColor 禁用状态的星星颜色,默认为 "#c0c0c0"
  33. * @property {Number} size 星星的大小
  34. * @property {Number} value/v-model 当前评分
  35. * @property {Number} max 最大评分评分数量,目前一分一颗星
  36. * @property {Number} margin 星星的间距,单位 px
  37. * @property {Boolean} disabled = [true|false] 是否为禁用状态,默认为 false
  38. * @property {Boolean} readonly = [true|false] 是否为只读状态,默认为 false
  39. * @property {Boolean} allowHalf = [true|false] 是否实现半星,默认为 false
  40. * @property {Boolean} touchable = [true|false] 是否支持滑动手势,默认为 true
  41. * @event {Function} change uniRate 的 value 改变时触发事件,e={value:Number}
  42. */
  43. export default {
  44. components: {
  45. uniIcons
  46. },
  47. name: "UniRate",
  48. props: {
  49. isFill: {
  50. // 星星的类型,是否镂空
  51. type: [Boolean, String],
  52. default: true
  53. },
  54. color: {
  55. // 星星未选中的颜色
  56. type: String,
  57. default: "#ececec"
  58. },
  59. activeColor: {
  60. // 星星选中状态颜色
  61. type: String,
  62. default: "#ffca3e"
  63. },
  64. disabledColor: {
  65. // 星星禁用状态颜色
  66. type: String,
  67. default: "#c0c0c0"
  68. },
  69. size: {
  70. // 星星的大小
  71. type: [Number, String],
  72. default: 24
  73. },
  74. value: {
  75. // 当前评分
  76. type: [Number, String],
  77. default: 1
  78. },
  79. max: {
  80. // 最大评分
  81. type: [Number, String],
  82. default: 5
  83. },
  84. margin: {
  85. // 星星的间距
  86. type: [Number, String],
  87. default: 0
  88. },
  89. disabled: {
  90. // 是否可点击
  91. type: [Boolean, String],
  92. default: false
  93. },
  94. readonly: {
  95. // 是否只读
  96. type: [Boolean, String],
  97. default: false
  98. },
  99. allowHalf: {
  100. // 是否显示半星
  101. type: [Boolean, String],
  102. default: false
  103. },
  104. touchable: {
  105. // 是否支持滑动手势
  106. type: [Boolean, String],
  107. default: true
  108. }
  109. },
  110. data() {
  111. return {
  112. valueSync: ""
  113. };
  114. },
  115. watch: {
  116. value(newVal) {
  117. this.valueSync = Number(newVal);
  118. }
  119. },
  120. computed: {
  121. stars() {
  122. const value = this.valueSync ? this.valueSync : 0;
  123. const starList = [];
  124. const floorValue = Math.floor(value);
  125. const ceilValue = Math.ceil(value);
  126. for (let i = 0; i < this.max; i++) {
  127. if (floorValue > i) {
  128. starList.push({
  129. activeWitch: "100%"
  130. });
  131. } else if (ceilValue - 1 === i) {
  132. starList.push({
  133. activeWitch: (value - floorValue) * 100 + "%"
  134. });
  135. } else {
  136. starList.push({
  137. activeWitch: "0"
  138. });
  139. }
  140. }
  141. return starList;
  142. }
  143. },
  144. created() {
  145. this.valueSync = Number(this.value);
  146. this._rateBoxLeft = 0
  147. this._oldValue = null
  148. },
  149. mounted() {
  150. setTimeout(() => {
  151. this._getSize()
  152. }, 100)
  153. },
  154. methods: {
  155. touchstart(e) {
  156. if (this.readonly || this.disabled) return
  157. const {
  158. clientX,
  159. screenX
  160. } = e.changedTouches[0]
  161. // TODO 做一下兼容,只有 Nvue 下才有 screenX,其他平台式 clientX
  162. this._getRateCount(clientX || screenX)
  163. },
  164. touchmove(e) {
  165. if (this.readonly || this.disabled || !this.touchable) return
  166. const {
  167. clientX,
  168. screenX
  169. } = e.changedTouches[0]
  170. this._getRateCount(clientX || screenX)
  171. },
  172. /**
  173. * 获取星星个数
  174. */
  175. _getRateCount(clientX) {
  176. const rateMoveRange = clientX - this._rateBoxLeft
  177. let index = parseInt(rateMoveRange / (this.size + this.margin))
  178. index = index < 0 ? 0 : index;
  179. index = index > this.max ? this.max : index;
  180. const range = parseInt(rateMoveRange - (this.size + this.margin) * index);
  181. let value = 0;
  182. if (this._oldValue === index) return;
  183. this._oldValue = index;
  184. if (this.allowHalf) {
  185. if (range > (this.size / 2)) {
  186. value = index + 1
  187. } else {
  188. value = index + 0.5
  189. }
  190. } else {
  191. value = index + 1
  192. }
  193. value = Math.max(0.5, Math.min(value, this.max))
  194. this.valueSync = value
  195. this._onChange()
  196. },
  197. /**
  198. * 触发动态修改
  199. */
  200. _onChange() {
  201. this.$emit("input", this.valueSync);
  202. this.$emit("change", {
  203. value: this.valueSync
  204. });
  205. },
  206. /**
  207. * 获取星星距离屏幕左侧距离
  208. */
  209. _getSize() {
  210. // #ifndef APP-NVUE
  211. uni.createSelectorQuery()
  212. .in(this)
  213. .select('.uni-rate')
  214. .boundingClientRect()
  215. .exec(ret => {
  216. if (ret) {
  217. this._rateBoxLeft = ret[0].left
  218. }
  219. })
  220. // #endif
  221. // #ifdef APP-NVUE
  222. dom.getComponentRect(this.$refs['uni-rate'], (ret) => {
  223. const size = ret.size
  224. if (size) {
  225. this._rateBoxLeft = size.left
  226. }
  227. })
  228. // #endif
  229. }
  230. }
  231. };
  232. </script>
  233. <style scoped>
  234. .uni-rate {
  235. /* #ifndef APP-NVUE */
  236. display: flex;
  237. /* #endif */
  238. line-height: 1;
  239. font-size: 0;
  240. flex-direction: row;
  241. }
  242. .uni-rate__icon {
  243. position: relative;
  244. line-height: 1;
  245. font-size: 0;
  246. }
  247. .uni-rate__icon-on {
  248. overflow: hidden;
  249. position: absolute;
  250. top: 0;
  251. left: 0;
  252. line-height: 1;
  253. text-align: left;
  254. }
  255. </style>