cl-rate.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <view class="cl-rate">
  3. <view class="cl-rate__icon" v-for="(item, index) in list" :key="index" @tap="onTap(index)">
  4. <cl-icon :name="icon" :size="size" :color="voidColor"></cl-icon>
  5. <view
  6. class="cl-rate__icon-active"
  7. :style="{
  8. width: item.width,
  9. }"
  10. >
  11. <cl-icon :name="icon" :size="size" :color="item.color"></cl-icon>
  12. </view>
  13. </view>
  14. <view class="cl-rate__text" v-if="showText">
  15. {{ value2 }}
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import { isNumber, getCurrentColor } from "../../utils";
  21. /**
  22. * rate 评分
  23. * @description 评分,可自定义图标
  24. * @tutorial https://docs.cool-js.com/uni/components/form/rate.html
  25. * @property {Number, String} value 绑定值
  26. * @property {String} icon 评分图标,默认cl-icon-favor-fill
  27. * @property {String, Array} color 选中颜色,Array下为多色。默认主色
  28. * @property {String} voidColor 未选中颜色,默认#C6D1DE
  29. * @property {Number} size 图标大小,默认40
  30. * @property {Number, String} max 最大值,默认5
  31. * @property {Boolean} disabled 是否禁用
  32. * @property {Boolean} showText 是否显示分数
  33. * @event {Function} change 绑定值改变时触发
  34. * @example <cl-rate />
  35. */
  36. export default {
  37. name: "cl-rate",
  38. props: {
  39. // 绑定值
  40. value: {
  41. type: [Number, String],
  42. default: 0,
  43. },
  44. // 评分图标
  45. icon: {
  46. type: String,
  47. default: "favor-fill",
  48. },
  49. // 选中颜色
  50. color: {
  51. type: [String, Array],
  52. default: "",
  53. },
  54. // 未选中颜色
  55. voidColor: {
  56. type: String,
  57. default: "#C6D1DE",
  58. },
  59. // 图标大小
  60. size: {
  61. type: [Number, String],
  62. default: 40,
  63. },
  64. // 最大值
  65. max: {
  66. type: [Number, String],
  67. default: 5,
  68. },
  69. // 是否禁用
  70. disabled: Boolean,
  71. // 是否显示分数
  72. showText: Boolean,
  73. },
  74. data() {
  75. return {
  76. value2: 0,
  77. };
  78. },
  79. watch: {
  80. value: {
  81. immediate: true,
  82. handler(val) {
  83. this.value2 = val;
  84. },
  85. },
  86. },
  87. computed: {
  88. list() {
  89. return new Array(this.max).fill(1).map((e, i) => {
  90. let int = parseInt(this.value2);
  91. let dec = this.value2 - int;
  92. // 处理宽度和颜色
  93. return {
  94. width: (this.value2 > i ? (int > i ? 100 : dec * 100) : 0) + "%",
  95. color: getCurrentColor({
  96. value: this.value2,
  97. color: this.color,
  98. max: this.max,
  99. }),
  100. };
  101. });
  102. },
  103. },
  104. methods: {
  105. onTap(index) {
  106. if (this.disabled) {
  107. return false;
  108. }
  109. this.value2 = index + 1;
  110. this.$emit("input", this.value2);
  111. this.$emit("change", this.value2);
  112. },
  113. },
  114. };
  115. </script>