uni-rate.vue 6.4 KB

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