Blame view

src/components/checkbox/checkbox.vue 3.7 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
      </label>
  </template>
  <script>
cd78c9c4   梁灏   some comps suppor...
25
26
      import Emitter from '../../mixins/emitter';
  
7fa943eb   梁灏   init
27
28
29
      const prefixCls = 'ivu-checkbox';
  
      export default {
cbe03a12   梁灏   support Checkbox
30
          name: 'Checkbox',
cd78c9c4   梁灏   some comps suppor...
31
          mixins: [ Emitter ],
7fa943eb   梁灏   init
32
33
34
35
36
37
          props: {
              disabled: {
                  type: Boolean,
                  default: false
              },
              value: {
7fa943eb   梁灏   init
38
39
                  type: Boolean,
                  default: false
b923c818   梁灏   update Tree
40
              },
cbe03a12   梁灏   support Checkbox
41
42
43
              label: {
                  type: [String, Number, Boolean]
              },
b923c818   梁灏   update Tree
44
45
46
              indeterminate: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
47
48
49
50
51
              }
          },
          data () {
              return {
                  model: [],
cbe03a12   梁灏   support Checkbox
52
                  currentValue: this.value,
71699f6b   梁灏   optimize Checkbox...
53
54
                  group: false,
                  showSlot: true
b0893113   jingsam   :art: add eslint
55
              };
7fa943eb   梁灏   init
56
57
58
59
60
61
62
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
cbe03a12   梁灏   support Checkbox
63
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
7fa943eb   梁灏   init
64
65
                          [`${prefixCls}-wrapper-disabled`]: this.disabled
                      }
b0893113   jingsam   :art: add eslint
66
                  ];
7fa943eb   梁灏   init
67
68
69
70
71
              },
              checkboxClasses () {
                  return [
                      `${prefixCls}`,
                      {
cbe03a12   梁灏   support Checkbox
72
                          [`${prefixCls}-checked`]: this.currentValue,
b923c818   梁灏   update Tree
73
74
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-indeterminate`]: this.indeterminate
7fa943eb   梁灏   init
75
                      }
b0893113   jingsam   :art: add eslint
76
                  ];
7fa943eb   梁灏   init
77
78
79
80
81
82
83
84
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
cbe03a12   梁灏   support Checkbox
85
86
          mounted () {
              // todo 使用 while向上查找
e6c0b158   梁灏   update
87
              if (this.$parent && this.$parent.$options.name === 'CheckboxGroup') this.group = true;
7fa943eb   梁灏   init
88
89
              if (!this.group) {
                  this.updateModel();
e6c0b158   梁灏   update
90
91
92
93
  //                if (this.$refs.slot && this.$refs.slot.innerHTML === '') {
  //                    this.showSlot = false;
  //                }
                  if (this.$slots.default === undefined) {
71699f6b   梁灏   optimize Checkbox...
94
                      this.showSlot = false;
71699f6b   梁灏   optimize Checkbox...
95
                  }
7fa943eb   梁灏   init
96
97
98
99
100
101
102
103
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
cbe03a12   梁灏   support Checkbox
104
105
106
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
107
108
109
110
  
                  if (this.group) {
                      this.$parent.change(this.model);
                  } else {
cbe03a12   梁灏   support Checkbox
111
                      this.$emit('on-change', checked);
cd78c9c4   梁灏   some comps suppor...
112
                      this.dispatch('FormItem', 'on-form-change', checked);
7fa943eb   梁灏   init
113
114
115
                  }
              },
              updateModel () {
cbe03a12   梁灏   support Checkbox
116
                  this.currentValue = this.value;
7fa943eb   梁灏   init
117
118
119
              }
          },
          watch: {
cbe03a12   梁灏   support Checkbox
120
              value () {
7fa943eb   梁灏   init
121
122
123
                  this.updateModel();
              }
          }
b0893113   jingsam   :art: add eslint
124
125
      };
  </script>