index.vue 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <uni-shadow-root class="vant-rate-index"><view class="van-rate custom-class" @touchmove="onTouchMove">
  3. <view v-for="(item,index) in (count)" :key="item.index" class="van-rate__item" :style="'font-size: '+(size)+'px;'">
  4. <van-icon :name="index + 1 <= innerValue ? icon : voidIcon" class="van-rate__icon" 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'])" 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/rate/index'
  14. import { VantComponent } from '../common/component';
  15. VantComponent({
  16. field: true,
  17. classes: ['icon-class'],
  18. props: {
  19. value: Number,
  20. readonly: Boolean,
  21. disabled: Boolean,
  22. allowHalf: Boolean,
  23. size: {
  24. type: Number,
  25. value: 20
  26. },
  27. icon: {
  28. type: String,
  29. value: 'star'
  30. },
  31. voidIcon: {
  32. type: String,
  33. value: 'star-o'
  34. },
  35. color: {
  36. type: String,
  37. value: '#ffd21e'
  38. },
  39. voidColor: {
  40. type: String,
  41. value: '#c7c7c7'
  42. },
  43. disabledColor: {
  44. type: String,
  45. value: '#bdbdbd'
  46. },
  47. count: {
  48. type: Number,
  49. value: 5
  50. }
  51. },
  52. data: {
  53. innerValue: 0
  54. },
  55. watch: {
  56. value(value) {
  57. if (value !== this.data.innerValue) {
  58. this.set({ innerValue: value });
  59. }
  60. }
  61. },
  62. methods: {
  63. onSelect(event) {
  64. const { data } = this;
  65. const { score } = event.currentTarget.dataset;
  66. if (!data.disabled && !data.readonly) {
  67. this.set({ innerValue: score + 1 });
  68. this.$emit('input', score + 1);
  69. this.$emit('change', score + 1);
  70. }
  71. },
  72. onTouchMove(event) {
  73. const { clientX, clientY } = event.touches[0];
  74. this.getRect('.van-rate__icon', true).then((list) => {
  75. const target = list
  76. .sort(item => item.right - item.left)
  77. .find(item => clientX >= item.left &&
  78. clientX <= item.right &&
  79. clientY >= item.top &&
  80. clientY <= item.bottom);
  81. if (target != null) {
  82. this.onSelect(Object.assign({}, event, { currentTarget: target }));
  83. }
  84. });
  85. }
  86. }
  87. });
  88. export default global['__wxComponents']['vant/rate/index']
  89. </script>
  90. <style platform="mp-weixin">
  91. @import '../common/index.css';.van-rate{-webkit-user-select:none;user-select:none}.van-rate__item{position:relative;display:inline-block;padding:0 2px}.van-rate__icon{display:block;height:1em}.van-rate__icon--half{position:absolute;top:0;left:2px;width:.5em;overflow:hidden}
  92. </style>