index.js 2.06 KB
Newer Older
程默 committed
1
import { getAllRect } from '../common/utils';
李嘉林 committed
2
import { VantComponent } from '../common/component';
程默 committed
3
import { canIUseModel } from '../common/version';
李嘉林 committed
4
VantComponent({
程默 committed
5 6 7 8 9 10 11 12
  field: true,
  classes: ['icon-class'],
  props: {
    value: {
      type: Number,
      observer(value) {
        if (value !== this.data.innerValue) {
          this.setData({ innerValue: value });
李嘉林 committed
13
        }
程默 committed
14
      },
李嘉林 committed
15
    },
程默 committed
16 17 18 19 20 21 22
    readonly: Boolean,
    disabled: Boolean,
    allowHalf: Boolean,
    size: null,
    icon: {
      type: String,
      value: 'star',
李嘉林 committed
23
    },
程默 committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    voidIcon: {
      type: String,
      value: 'star-o',
    },
    color: {
      type: String,
      value: '#ffd21e',
    },
    voidColor: {
      type: String,
      value: '#c7c7c7',
    },
    disabledColor: {
      type: String,
      value: '#bdbdbd',
    },
    count: {
      type: Number,
      value: 5,
      observer(value) {
        this.setData({ innerCountArray: Array.from({ length: value }) });
      },
    },
    gutter: null,
    touchable: {
      type: Boolean,
      value: true,
    },
  },
  data: {
    innerValue: 0,
    innerCountArray: Array.from({ length: 5 }),
  },
  methods: {
    onSelect(event) {
      const { data } = this;
      const { score } = event.currentTarget.dataset;
      if (!data.disabled && !data.readonly) {
        this.setData({ innerValue: score + 1 });
        if (canIUseModel()) {
          this.setData({ value: score + 1 });
        }
        wx.nextTick(() => {
          this.$emit('input', score + 1);
          this.$emit('change', score + 1);
        });
      }
    },
    onTouchMove(event) {
      const { touchable } = this.data;
      if (!touchable) return;
      const { clientX } = event.touches[0];
      getAllRect(this, '.van-rate__icon').then((list) => {
        const target = list
          .sort((item) => item.right - item.left)
          .find((item) => clientX >= item.left && clientX <= item.right);
        if (target != null) {
          this.onSelect(
            Object.assign(Object.assign({}, event), { currentTarget: target })
          );
李嘉林 committed
84
        }
程默 committed
85 86 87
      });
    },
  },
李嘉林 committed
88
});