uni-rate.vue 6.6 KB

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