uni-rate.vue 6.5 KB

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