uni-rate.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. text: {
  139. // 传值
  140. type: String,
  141. default: ''
  142. },
  143. evaItem:{
  144. }
  145. },
  146. data() {
  147. return {
  148. valueSync: "",
  149. textSync:'',
  150. evaItemSync:''
  151. };
  152. },
  153. watch: {
  154. value(newVal) {
  155. this.valueSync = Number(newVal);
  156. },
  157. text(newVal){
  158. this.textSync = newVal;
  159. },
  160. },
  161. computed: {
  162. stars() {
  163. const value = this.valueSync ? this.valueSync : 0;
  164. const starList = [];
  165. const floorValue = Math.floor(value);
  166. const ceilValue = Math.ceil(value);
  167. for (let i = 0; i < this.max; i++) {
  168. if (floorValue > i) {
  169. starList.push({
  170. activeWitch: "100%"
  171. });
  172. } else if (ceilValue - 1 === i&&this.allowHalf==true) {
  173. starList.push({
  174. activeWitch: (value - floorValue) * 100 + "%"
  175. });
  176. } else {
  177. starList.push({
  178. activeWitch: "0"
  179. });
  180. }
  181. }
  182. return starList;
  183. }
  184. },
  185. created() {
  186. this.valueSync = Number(this.value);
  187. this.textSync=this.text
  188. this.evaItemSync=this.evaItem
  189. this._rateBoxLeft = 0
  190. this._oldValue = null
  191. },
  192. mounted() {
  193. setTimeout(() => {
  194. this._getSize()
  195. }, 100)
  196. },
  197. methods: {
  198. touchstart(e) {
  199. if (this.readonly || this.disabled) return
  200. const {
  201. clientX,
  202. screenX
  203. } = e.changedTouches[0]
  204. // TODO 做一下兼容,只有 Nvue 下才有 screenX,其他平台式 clientX
  205. this._getRateCount(clientX || screenX)
  206. },
  207. touchmove(e) {
  208. if (this.readonly || this.disabled || !this.touchable) return
  209. const {
  210. clientX,
  211. screenX
  212. } = e.changedTouches[0]
  213. this._getRateCount(clientX || screenX)
  214. },
  215. /**
  216. * 获取星星个数
  217. */
  218. _getRateCount(clientX) {
  219. const rateMoveRange = clientX - this._rateBoxLeft
  220. const index = parseInt(rateMoveRange / (this.size + this.margin))
  221. const range = parseInt(rateMoveRange - ((this.size + this.margin) * index))
  222. let value = 0
  223. if (this.allowHalf==true) {
  224. if (range > (this.size / 2)) {
  225. value = index + 1
  226. } else {
  227. value = index + 0.5
  228. }
  229. } else {
  230. value = index + 1
  231. }
  232. value = Math.max(0.5, Math.min(value, this.max))
  233. if (this.valueSync !== value) {
  234. this.valueSync = value
  235. this._onChange()
  236. }
  237. // const rateCount = parseInt(rateMoveRange / (this.size / 2)) + 1
  238. },
  239. /**
  240. * 触发动态修改
  241. */
  242. _onChange() {
  243. this.$emit("input", this.valueSync);
  244. this.$emit("change", {
  245. value: this.valueSync,
  246. text:this.textSync,
  247. item:this.evaItemSync
  248. });
  249. },
  250. /**
  251. * 获取星星距离屏幕左侧距离
  252. */
  253. _getSize() {
  254. // #ifndef APP-NVUE
  255. uni.createSelectorQuery()
  256. .in(this)
  257. .select('.uni-rate')
  258. .boundingClientRect()
  259. .exec(ret => {
  260. if (ret) {
  261. this._rateBoxLeft = ret[0].left
  262. }
  263. })
  264. // #endif
  265. // #ifdef APP-NVUE
  266. dom.getComponentRect(this.$refs['uni-rate'], (ret) => {
  267. const size = ret.size
  268. if (size) {
  269. this._rateBoxLeft = size.left
  270. }
  271. })
  272. // #endif
  273. }
  274. }
  275. };
  276. </script>
  277. <style
  278. lang="scss"
  279. scoped
  280. >
  281. .uni-rate {
  282. /* #ifndef APP-NVUE */
  283. display: flex;
  284. /* #endif */
  285. line-height: 1;
  286. font-size: 0;
  287. flex-direction: row;
  288. }
  289. .uni-rate__icon {
  290. position: relative;
  291. line-height: 1;
  292. font-size: 0;
  293. }
  294. .uni-rate__icon-on {
  295. overflow: hidden;
  296. position: absolute;
  297. top: 0;
  298. left: 0;
  299. line-height: 1;
  300. text-align: left;
  301. }
  302. </style>