index.js 1.29 KB
Newer Older
李嘉林 committed
1 2 3
import { VantComponent } from '../common/component';
import { BLUE, GRAY_DARK } from '../common/color';
VantComponent({
程默 committed
4 5 6 7 8 9
  field: true,
  classes: ['node-class'],
  props: {
    checked: {
      type: null,
      observer(value) {
李嘉林 committed
10 11
        const loadingColor = this.getLoadingColor(value);
        this.setData({ value, loadingColor });
程默 committed
12 13 14 15 16 17 18 19 20 21 22 23 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
      },
    },
    loading: Boolean,
    disabled: Boolean,
    activeColor: String,
    inactiveColor: String,
    size: {
      type: String,
      value: '30px',
    },
    activeValue: {
      type: null,
      value: true,
    },
    inactiveValue: {
      type: null,
      value: false,
    },
  },
  created() {
    const { checked: value } = this.data;
    const loadingColor = this.getLoadingColor(value);
    this.setData({ value, loadingColor });
  },
  methods: {
    getLoadingColor(checked) {
      const { activeColor, inactiveColor } = this.data;
      return checked ? activeColor || BLUE : inactiveColor || GRAY_DARK;
    },
    onClick() {
      const { activeValue, inactiveValue } = this.data;
      if (!this.data.disabled && !this.data.loading) {
        const checked = this.data.checked === activeValue;
        const value = checked ? inactiveValue : activeValue;
        this.$emit('input', value);
        this.$emit('change', value);
      }
李嘉林 committed
49
    },
程默 committed
50
  },
李嘉林 committed
51
});