index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <uni-shadow-root class="vant-dist-rate-index"><view class="van-rate custom-class" @touchmove="onTouchMove">
  3. <view v-for="(item,index) in (innerCountArray)" :key="item.index" class="van-rate__item" :style="'padding-right: '+(index !== count - 1 ? utils.addUnit(gutter) : '')">
  4. <van-icon :name="index + 1 <= innerValue ? icon : voidIcon" class="van-rate__icon" :style="'font-size: '+(utils.addUnit(size))" custom-class="icon-class" :data-score="index" :color="disabled ? disabledColor : index + 1 <= innerValue ? color : voidColor" @click="onSelect"></van-icon>
  5. <van-icon v-if="allowHalf" :name="index + 0.5 <= innerValue ? icon : voidIcon" :class="utils.bem('rate__icon', ['half'])" :style="'font-size: '+(utils.addUnit(size))" custom-class="icon-class" :data-score="index - 0.5" :color="disabled ? disabledColor : index + 0.5 <= innerValue ? color : voidColor" @click="onSelect"></van-icon>
  6. </view>
  7. </view></uni-shadow-root>
  8. </template>
  9. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  10. <script>
  11. import VanIcon from '../icon/index.vue'
  12. global['__wxVueOptions'] = {components:{'van-icon': VanIcon}}
  13. global['__wxRoute'] = 'vant/dist/rate/index'
  14. import { VantComponent } from '../common/component';
  15. import { canIUseModel } from '../common/version';
  16. VantComponent({
  17. field: true,
  18. classes: ['icon-class'],
  19. props: {
  20. value: {
  21. type: Number,
  22. observer(value) {
  23. if (value !== this.data.innerValue) {
  24. this.setData({ innerValue: value });
  25. }
  26. },
  27. },
  28. readonly: Boolean,
  29. disabled: Boolean,
  30. allowHalf: Boolean,
  31. size: null,
  32. icon: {
  33. type: String,
  34. value: 'star',
  35. },
  36. voidIcon: {
  37. type: String,
  38. value: 'star-o',
  39. },
  40. color: {
  41. type: String,
  42. value: '#ffd21e',
  43. },
  44. voidColor: {
  45. type: String,
  46. value: '#c7c7c7',
  47. },
  48. disabledColor: {
  49. type: String,
  50. value: '#bdbdbd',
  51. },
  52. count: {
  53. type: Number,
  54. value: 5,
  55. observer(value) {
  56. this.setData({ innerCountArray: Array.from({ length: value }) });
  57. },
  58. },
  59. gutter: null,
  60. touchable: {
  61. type: Boolean,
  62. value: true,
  63. },
  64. },
  65. data: {
  66. innerValue: 0,
  67. innerCountArray: Array.from({ length: 5 }),
  68. },
  69. methods: {
  70. onSelect(event) {
  71. const { data } = this;
  72. const { score } = event.currentTarget.dataset;
  73. if (!data.disabled && !data.readonly) {
  74. this.setData({ innerValue: score + 1 });
  75. if (canIUseModel()) {
  76. this.setData({ value: score + 1 });
  77. }
  78. wx.nextTick(() => {
  79. this.$emit('input', score + 1);
  80. this.$emit('change', score + 1);
  81. });
  82. }
  83. },
  84. onTouchMove(event) {
  85. const { touchable } = this.data;
  86. if (!touchable) return;
  87. const { clientX } = event.touches[0];
  88. this.getRect('.van-rate__icon', true).then((list) => {
  89. const target = list
  90. .sort((item) => item.right - item.left)
  91. .find((item) => clientX >= item.left && clientX <= item.right);
  92. if (target != null) {
  93. this.onSelect(
  94. Object.assign(Object.assign({}, event), { currentTarget: target })
  95. );
  96. }
  97. });
  98. },
  99. },
  100. });
  101. export default global['__wxComponents']['vant/dist/rate/index']
  102. </script>
  103. <style platform="mp-weixin">
  104. @import '../common/index.css';.van-rate{display:-webkit-inline-flex;display:inline-flex;-webkit-user-select:none;user-select:none}.van-rate__item{position:relative;padding:0 2px;padding:0 var(--rate-horizontal-padding,2px)}.van-rate__icon{display:block;height:1em;font-size:20px;font-size:var(--rate-icon-size,20px)}.van-rate__icon--half{position:absolute;top:0;width:.5em;overflow:hidden;left:2px;left:var(--rate-horizontal-padding,2px)}
  105. </style>