index.vue 4.4 KB

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