Blame view

src/components/CheckBox/checkbox.vue 6.84 KB
1 2 3
<!--
 * @Description: 
 * @Autor: renchao
4
 * @LastEditTime: 2023-07-25 16:06:03
5
-->
任超 committed
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
<template>
  <label class="el-checkbox" :class="[
    border && checkboxSize ? 'el-checkbox--' + checkboxSize : '',
    { 'is-disabled': isDisabled },
    { 'is-bordered': border },
    { 'is-checked': isChecked }
  ]" :id="id">
    <span class="el-checkbox__input" :class="{
      'is-disabled': isDisabled,
      'is-checked': isChecked,
      'is-indeterminate': indeterminate,
      'is-focus': focus
    }" :tabindex="indeterminate ? 0 : false" :role="indeterminate ? 'checkbox' : false"
      :aria-checked="indeterminate ? 'mixed' : false">
      <span class="el-checkbox__inner"></span>
      <input v-if="trueLabel || falseLabel" class="el-checkbox__original" type="checkbox"
        :aria-hidden="indeterminate ? 'true' : 'false'" :name="name" :disabled="isDisabled" :true-value="trueLabel"
        :false-value="falseLabel" v-model="model" @change="handleChange" @focus="focus = true" @blur="focus = false">
      <input v-else class="el-checkbox__original" type="checkbox" :aria-hidden="indeterminate ? 'true' : 'false'"
        :disabled="isDisabled" :value="label" :name="name" v-model="model" @change="handleChange" @focus="focus = true"
        @blur="focus = false">
    </span>
    <span class="el-checkbox__label">
      <slot></slot>
    </span>
  </label>
</template>
<script>
34
  import Emitter from 'element-ui/src/mixins/emitter';
任超 committed
35

36 37
  export default {
    name: 'ElCheckbox',
任超 committed
38

39
    mixins: [Emitter],
任超 committed
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    inject: {
      elForm: {
        default: ''
      },
      elFormItem: {
        default: ''
      }
    },
    componentName: 'ElCheckbox',
    data () {
      return {
        selfModel: false,
        focus: false,
        isLimitExceeded: false
      };
任超 committed
56
    },
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    computed: {
      model: {
        /**
         * @description: get
         * @author: renchao
         */
        get () {
          return this.isGroup
            ? this.store : this.value !== undefined
              ? this.value : this.selfModel;
        },
        /**
         * @description: set
         * @param {*} val
         * @author: renchao
         */
        set (val) {
          if (this.isGroup) {
            this.isLimitExceeded = false;
            (this._checkboxGroup.min !== undefined &&
              val.length < this._checkboxGroup.min &&
              (this.isLimitExceeded = true));
任超 committed
79

80 81 82 83 84 85 86 87 88 89 90 91
            (this._checkboxGroup.max !== undefined &&
              val.length > this._checkboxGroup.max &&
              (this.isLimitExceeded = true));

            this.isLimitExceeded === false &&
              this.dispatch('ElCheckboxGroup', 'input', [val]);
          } else {
            this.$emit('input', val);
            this.selfModel = val;
          }
        }
      },
任超 committed
92

yuanbo committed
93
      /**
94
       * @description: isChecked
yuanbo committed
95 96
       * @author: renchao
       */
97 98 99 100 101 102 103 104
      isChecked () {
        if ({}.toString.call(this.model) === '[object Boolean]') {
          return this.model;
        } else if (Array.isArray(this.model)) {
          return this.model.indexOf(this.label) > -1;
        } else if (this.model !== null && this.model !== undefined) {
          return this.model === this.trueLabel;
        }
任超 committed
105
      },
yuanbo committed
106
      /**
107
       * @description: isGroup
yuanbo committed
108 109
       * @author: renchao
       */
110 111 112 113 114 115 116 117 118
      isGroup () {
        let parent = this.$parent;
        while (parent) {
          if (parent.$options.componentName !== 'ElCheckboxGroup') {
            parent = parent.$parent;
          } else {
            this._checkboxGroup = parent;
            return true;
          }
任超 committed
119
        }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        return false;
      },
      /**
       * @description: store
       * @author: renchao
       */
      store () {
        return this._checkboxGroup ? this._checkboxGroup.value : this.value;
      },
      /**
       * @description: isLimitDisabled
       * @author: renchao
       */
      isLimitDisabled () {
        const { max, min } = this._checkboxGroup;
        return !!(max || min) &&
          (this.model.length >= max && !this.isChecked) ||
          (this.model.length <= min && this.isChecked);
      },
      /**
       * @description: isDisabled
       * @author: renchao
       */
      isDisabled () {
        return this.isGroup
          ? this._checkboxGroup.disabled || this.disabled || (this.elForm || {}).disabled || this.isLimitDisabled
          : this.disabled || (this.elForm || {}).disabled;
      },
      /**
       * @description: _elFormItemSize
       * @author: renchao
       */
      _elFormItemSize () {
        return (this.elFormItem || {}).elFormItemSize;
      },
      /**
       * @description: checkboxSize
       * @author: renchao
       */
      checkboxSize () {
        const temCheckboxSize = this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
        return this.isGroup
          ? this._checkboxGroup.checkboxGroupSize || temCheckboxSize
          : temCheckboxSize;
任超 committed
164 165 166
      }
    },

167 168 169 170 171 172 173 174 175 176 177 178 179
    props: {
      value: {},
      label: {},
      indeterminate: Boolean,
      disabled: Boolean,
      checked: Boolean,
      name: String,
      trueLabel: [String, Number],
      falseLabel: [String, Number],
      id: String, /* 当indeterminate为真时,为controls提供相关连的checkbox的id,表明元素间的控制关系*/
      controls: String, /* 当indeterminate为真时,为controls提供相关连的checkbox的id,表明元素间的控制关系*/
      border: Boolean,
      size: String
任超 committed
180
    },
181 182 183 184 185 186 187 188 189 190 191 192

    methods: {
      /**
       * @description: addToStore
       * @author: renchao
       */
      addToStore () {
        if (
          Array.isArray(this.model) &&
          this.model.indexOf(this.label) === -1
        ) {
          this.model.push(this.label);
任超 committed
193
        } else {
194
          this.model = this.trueLabel || true;
任超 committed
195
        }
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      },
      /**
       * @description: handleChange
       * @author: renchao
       */
      handleChange (ev) {
        if (this.isLimitExceeded) return;
        let value;
        if (ev.target.checked) {
          value = this.trueLabel === undefined ? true : this.trueLabel;
        } else {
          value = this.falseLabel === undefined ? false : this.falseLabel;
        }
        this.$emit('change', value, ev);
        this.$nextTick(() => {
          if (this.isGroup) {
            this.dispatch('ElCheckboxGroup', 'change', [this._checkboxGroup.value]);
          }
        });
任超 committed
215 216
      }
    },
yuanbo committed
217
    /**
218
     * @description: created
yuanbo committed
219 220
     * @author: renchao
     */
221 222
    created () {
      this.checked && this.addToStore();
任超 committed
223
    },
yuanbo committed
224
    /**
225
     * @description: mounted
yuanbo committed
226 227
     * @author: renchao
     */
228 229 230
    mounted () { // 为indeterminate元素 添加aria-controls 属性
      if (this.indeterminate) {
        this.$el.setAttribute('aria-controls', this.controls);
任超 committed
231 232
      }
    },
yuanbo committed
233
    /**
234
     * @description: watch
yuanbo committed
235 236
     * @author: renchao
     */
237 238 239
    watch: {
      value (value) {
        this.dispatch('ElFormItem', 'el.form.change', value);
任超 committed
240 241
      }
    }
242
  };
任超 committed
243
</script>