Blame view

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