uni-rate.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view class="uni-rate">
  3. <view :key="index" :style="{ marginLeft: margin + 'px' }" @click="_onClick(index)" class="uni-rate__icon" v-for="(star, index) in stars">
  4. <uni-icons :color="color" :size="size" :type="isFill ? 'star-filled' : 'star'" />
  5. <!-- #ifdef APP-NVUE -->
  6. <view :style="{ width: star.activeWitch.replace('%','')*size/100+'px'}" class="uni-rate__icon-on">
  7. <uni-icons style="text-align: left;" :color="activeColor" :size="size" type="star-filled" />
  8. </view>
  9. <!-- #endif -->
  10. <!-- #ifndef APP-NVUE -->
  11. <view :style="{ width: star.activeWitch,top:-size/2+'px' }" class="uni-rate__icon-on">
  12. <uni-icons :color="activeColor" :size="size" type="star-filled" />
  13. </view>
  14. <!-- #endif -->
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import uniIcons from "../uni-icons/uni-icons.vue";
  20. /**
  21. * Rate 评分
  22. * @description 评分组件
  23. * @tutorial https://ext.dcloud.net.cn/plugin?id=33
  24. * @property {Number} value 当前评分
  25. * @property {Number} max 最大的评分
  26. * @property {Number} size 星星的大小
  27. * @property {Number} margin 星星的间距
  28. * @property {String} color 星星的颜色
  29. * @property {String} activeColor 选中状态的星星的颜色
  30. * @property {Boolean} isFill = [true|false] 星星的类型,是否为实心类型
  31. * @property {Boolean} disabled = [true|false] 是否为不可点击状态
  32. * @event {Function} change uniRate 的 value 改变时触发事件,e={value:Number}
  33. */
  34. export default {
  35. name: "UniRate",
  36. components: {
  37. uniIcons
  38. },
  39. props: {
  40. isFill: {
  41. // 星星的类型,是否镂空
  42. type: [Boolean, String],
  43. default: true
  44. },
  45. color: {
  46. // 星星的颜色
  47. type: String,
  48. default: "#ececec"
  49. },
  50. activeColor: {
  51. // 星星选中状态颜色
  52. type: String,
  53. default: "#ffca3e"
  54. },
  55. size: {
  56. // 星星的大小
  57. type: [Number, String],
  58. default: 24
  59. },
  60. value: {
  61. // 当前评分
  62. type: [Number, String],
  63. default: 0
  64. },
  65. max: {
  66. // 最大评分
  67. type: [Number, String],
  68. default: 5
  69. },
  70. margin: {
  71. // 星星的间距
  72. type: [Number, String],
  73. default: 0
  74. },
  75. disabled: {
  76. // 是否可点击
  77. type: [Boolean, String],
  78. default: false
  79. }
  80. },
  81. data() {
  82. return {
  83. valueSync: ""
  84. };
  85. },
  86. computed: {
  87. stars() {
  88. const value = this.valueSync ? this.valueSync : 0;
  89. const starList = [];
  90. const floorValue = Math.floor(value);
  91. const ceilValue = Math.ceil(value);
  92. // console.log("ceilValue: " + ceilValue);
  93. // console.log("floorValue: " + floorValue);
  94. for (let i = 0; i < this.max; i++) {
  95. if (floorValue > i) {
  96. starList.push({
  97. activeWitch: "100%"
  98. });
  99. } else if (ceilValue - 1 === i) {
  100. starList.push({
  101. activeWitch: (value - floorValue) * 100 + "%"
  102. });
  103. } else {
  104. starList.push({
  105. activeWitch: "0"
  106. });
  107. }
  108. }
  109. // console.log("starList[4]: " + starList[4].activeWitch);
  110. return starList;
  111. }
  112. },
  113. created() {
  114. this.valueSync = Number(this.value);
  115. },
  116. methods: {
  117. _onClick(index) {
  118. if (this.disabled) {
  119. return;
  120. }
  121. this.valueSync = index + 1;
  122. this.$emit("change", {
  123. value: this.valueSync
  124. });
  125. }
  126. }
  127. };
  128. </script>
  129. <style scoped>
  130. .uni-rate {
  131. /* #ifndef APP-NVUE */
  132. display: flex;
  133. /* #endif */
  134. line-height: 0;
  135. font-size: 0;
  136. flex-direction: row;
  137. }
  138. .uni-rate__icon {
  139. position: relative;
  140. line-height: 0;
  141. font-size: 0;
  142. }
  143. .uni-rate__icon-on {
  144. overflow: hidden;
  145. position: absolute;
  146. top: 0;
  147. left: 0;
  148. line-height: 1;
  149. text-align: left;
  150. }
  151. </style>