Blame view

src/components/checkbox/checkbox.vue 3.65 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向上查找
e6c0b158   梁灏   update
84
              if (this.$parent && this.$parent.$options.name === 'CheckboxGroup') this.group = true;
7fa943eb   梁灏   init
85
86
              if (!this.group) {
                  this.updateModel();
e6c0b158   梁灏   update
87
88
89
90
  //                if (this.$refs.slot && this.$refs.slot.innerHTML === '') {
  //                    this.showSlot = false;
  //                }
                  if (this.$slots.default === undefined) {
71699f6b   梁灏   optimize Checkbox...
91
                      this.showSlot = false;
71699f6b   梁灏   optimize Checkbox...
92
                  }
7fa943eb   梁灏   init
93
94
95
96
97
98
99
100
              }
          },
          methods: {
              change (event) {
                  if (this.disabled) {
                      return false;
                  }
  
cbe03a12   梁灏   support Checkbox
101
102
103
                  const checked = event.target.checked;
                  this.currentValue = checked;
                  this.$emit('input', checked);
7fa943eb   梁灏   init
104
105
106
107
  
                  if (this.group) {
                      this.$parent.change(this.model);
                  } else {
cbe03a12   梁灏   support Checkbox
108
109
110
                      this.$emit('on-change', checked);
                      // todo 事件
  //                    this.$dispatch('on-form-change', checked);
7fa943eb   梁灏   init
111
112
113
                  }
              },
              updateModel () {
cbe03a12   梁灏   support Checkbox
114
                  this.currentValue = this.value;
7fa943eb   梁灏   init
115
116
117
              }
          },
          watch: {
cbe03a12   梁灏   support Checkbox
118
              value () {
7fa943eb   梁灏   init
119
120
121
                  this.updateModel();
              }
          }
b0893113   jingsam   :art: add eslint
122
123
      };
  </script>