all files / packages/checkbox/src/ checkbox.vue

25% Statements 6/24
14.29% Branches 4/28
16.67% Functions 1/6
10% Lines 2/20
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 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 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                 
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
 
import findParent from 'src/mixins/findParent';
 
export default {
  name: 'zan-checkbox',
 
  mixins: [findParent],
 
  props: {
    disabled: Boolean,
    value: {},
    name: [String, Number]
  },
 
  computed: {
    /**
     * `checkbox`是否在`zan-checkbox-group`中
     */
    isGroup() {
      return !!this.findParentByComponentName('zan-checkbox-group');
    },
 
    /**
     * `checkbox`当前值
     */
    currentValue: {
      get() {
        return this.isGroup && this.parentGroup ? this.parentGroup.value.indexOf(this.name) !== -1 : this.value;
      },
 
      set(val) {
        if (this.isGroup && this.parentGroup) {
          let parentValue = this.parentGroup.value.slice();
          if (val) {
            if (parentValue.indexOf(this.name) === -1) {
              parentValue.push(this.name);
              this.parentGroup.$emit('input', parentValue);
            }
          } else {
            let index = parentValue.indexOf(this.name);
            if (index !== -1) {
              parentValue.splice(index, 1);
              this.parentGroup.$emit('input', parentValue);
            }
          }
        } else {
          this.$emit('input', val);
        }
      }
    },
 
    /**
     * `checkbox`是否被选中
     */
    isChecked() {
      if ({}.toString.call(this.currentValue) === '[object Boolean]') {
        return this.currentValue;
      } else if (Array.isArray(this.currentValue)) {
        return this.currentValue.indexOf(this.name) > -1;
      }
    },
 
    /**
     * `checkbox`是否被禁用
     */
    isDisabled() {
      return this.isGroup && this.parentGroup
          ? this.parentGroup.disabled || this.disabled
          : this.disabled;
    }
  }
};