index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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="'padding-right: '+(index !== count - 1 ? gutterWithUnit : '')">
  4. <van-icon :name="index + 1 <= innerValue ? icon : voidIcon" class="van-rate__icon" :style="'font-size :'+(size? sizeWithUnit : '')" 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 :'+(size? sizeWithUnit : '')" 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. import { addUnit } from '../common/utils';
  16. VantComponent({
  17. field: true,
  18. classes: ['icon-class'],
  19. props: {
  20. value: Number,
  21. readonly: Boolean,
  22. disabled: Boolean,
  23. allowHalf: Boolean,
  24. size: {
  25. type: null,
  26. observer: 'setSizeWithUnit'
  27. },
  28. icon: {
  29. type: String,
  30. value: 'star'
  31. },
  32. voidIcon: {
  33. type: String,
  34. value: 'star-o'
  35. },
  36. color: {
  37. type: String,
  38. value: '#ffd21e'
  39. },
  40. voidColor: {
  41. type: String,
  42. value: '#c7c7c7'
  43. },
  44. disabledColor: {
  45. type: String,
  46. value: '#bdbdbd'
  47. },
  48. count: {
  49. type: Number,
  50. value: 5
  51. },
  52. gutter: {
  53. type: null,
  54. observer: 'setGutterWithUnit'
  55. },
  56. touchable: {
  57. type: Boolean,
  58. value: true
  59. }
  60. },
  61. data: {
  62. innerValue: 0,
  63. gutterWithUnit: undefined,
  64. sizeWithUnit: null
  65. },
  66. watch: {
  67. value(value) {
  68. if (value !== this.data.innerValue) {
  69. this.setData({ innerValue: value });
  70. }
  71. }
  72. },
  73. methods: {
  74. setGutterWithUnit(val) {
  75. this.setData({
  76. gutterWithUnit: addUnit(val)
  77. });
  78. },
  79. setSizeWithUnit(size) {
  80. this.setData({
  81. sizeWithUnit: addUnit(size)
  82. });
  83. },
  84. onSelect(event) {
  85. const { data } = this;
  86. const { score } = event.currentTarget.dataset;
  87. if (!data.disabled && !data.readonly) {
  88. this.setData({ innerValue: score + 1 });
  89. this.$emit('input', score + 1);
  90. this.$emit('change', score + 1);
  91. }
  92. },
  93. onTouchMove(event) {
  94. const { touchable } = this.data;
  95. if (!touchable)
  96. return;
  97. const { clientX } = event.touches[0];
  98. this.getRect('.van-rate__icon', true).then((list) => {
  99. const target = list
  100. .sort(item => item.right - item.left)
  101. .find(item => clientX >= item.left && clientX <= item.right);
  102. if (target != null) {
  103. this.onSelect(Object.assign(Object.assign({}, event), { currentTarget: target }));
  104. }
  105. });
  106. }
  107. }
  108. });
  109. export default global['__wxComponents']['vant/rate/index']
  110. </script>
  111. <style platform="mp-weixin">
  112. @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)}
  113. </style>