Blame view

src/components/checkbox/checkbox.vue 3.53 KB
7fa943eb   梁灏   init
1
2
3
4
5
6
7
8
9
  <template>
      <label :class="wrapClasses">
          <span :class="checkboxClasses">
              <span :class="innerClasses"></span>
              <input
                  v-if="group"
                  type="checkbox"
                  :class="inputClasses"
                  :disabled="disabled"
cbe03a12   梁灏   support Checkbox
10
                  :value="label"
7fa943eb   梁灏   init
11
12
13
14
15
16
17
                  v-model="model"
                  @change="change">
              <input
                  v-if="!group"
                  type="checkbox"
                  :class="inputClasses"
                  :disabled="disabled"
cbe03a12   梁灏   support Checkbox
18
                  :checked="currentValue"
7fa943eb   梁灏   init
19
20
                  @change="change">
          </span>
cbe03a12   梁灏   support Checkbox
21
          <slot v-if="showSlot"><span ref="slot">{{ label }}</span></slot>
7fa943eb   梁灏   init
22
23
24
25
26
27
      </label>
  </template>
  <script>
      const prefixCls = 'ivu-checkbox';
  
      export default {
cbe03a12   梁灏   support Checkbox
28
          name: 'Checkbox',
7fa943eb   梁灏   init
29
30
31
32
33
34
          props: {
              disabled: {
                  type: Boolean,
                  default: false
              },
              value: {
7fa943eb   梁灏   init
35
36
                  type: Boolean,
                  default: false
b923c818   梁灏   update Tree
37
              },
cbe03a12   梁灏   support Checkbox
38
39
40
              label: {
                  type: [String, Number, Boolean]
              },
b923c818   梁灏   update Tree
41
42
43
              indeterminate: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
44
45
46
47
48
              }
          },
          data () {
              return {
                  model: [],
cbe03a12   梁灏   support Checkbox
49
                  currentValue: this.value,
71699f6b   梁灏   optimize Checkbox...
50
51
                  group: false,
                  showSlot: true
b0893113   jingsam   :art: add eslint
52
              };
7fa943eb   梁灏   init
53
54
55
56
57
58
59
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
cbe03a12   梁灏   support Checkbox
60
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
7fa943eb   梁灏   init
61
62
                          [`${prefixCls}-wrapper-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
63
                  ];
7fa943eb   梁灏   init
64
65
66
67
68
              },
              checkboxClasses () {
                  return [
                      `${prefixCls}`,
                      {
cbe03a12   梁灏   support Checkbox
69
                          [`${prefixCls}-checked`]: this.currentValue,
b923c818   梁灏   update Tree
70
71
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-indeterminate`]: this.indeterminate
7fa943eb   梁灏   init
72
                      }
b0893113   jingsam   :art: add eslint
73
                  ];
7fa943eb   梁灏   init
74
75
76
77
78
79
80
81
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
cbe03a12   梁灏   support Checkbox
82
83
          mounted () {
              // todo 使用 while向上查找
829a2e4c   梁灏   fixed CheckboxGro...
84
              if (this.$parent && this.$parent.$options.name === 'checkboxGroup') this.group = true;
7fa943eb   梁灏   init
85
86
              if (!this.group) {
                  this.updateModel();
cbe03a12   梁灏   support Checkbox
87
                  if (this.$refs.slot && this.$refs.slot.innerHTML === '') {
71699f6b   梁灏   optimize Checkbox...
88
                      this.showSlot = false;
71699f6b   梁灏   optimize Checkbox...
89
                  }
7fa943eb   梁灏   init
90
91
92
93
94
95
96
97
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
cbe03a12   梁灏   support Checkbox
98
99
100
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
101
102
103
104
  
                  if (this.group) {
                      this.$parent.change(this.model);
                  } else {
cbe03a12   梁灏   support Checkbox
105
106
107
                      this.$emit('on-change', checked);
                      // todo 事件
  //                    this.$dispatch('on-form-change', checked);
7fa943eb   梁灏   init
108
109
110
                  }
              },
              updateModel () {
cbe03a12   梁灏   support Checkbox
111
                  this.currentValue = this.value;
7fa943eb   梁灏   init
112
113
114
              }
          },
          watch: {
cbe03a12   梁灏   support Checkbox
115
              value () {
7fa943eb   梁灏   init
116
117
118
                  this.updateModel();
              }
          }
b0893113   jingsam   :art: add eslint
119
120
      };
  </script>