Blame view

src/components/checkbox/checkbox.vue 5.03 KB
7fa943eb   梁灏   init
1
  <template>
98a755be   Xotic750   Use native w3c
2
      <label :class="wrapClasses">
7fa943eb   梁灏   init
3
          <span :class="checkboxClasses">
2191fc5a   Xotic750   Remove ref
4
              <span :class="innerClasses"></span>
7fa943eb   梁灏   init
5
6
7
              <input
                  v-if="group"
                  type="checkbox"
7fa943eb   梁灏   init
8
9
                  :class="inputClasses"
                  :disabled="disabled"
cbe03a12   梁灏   support Checkbox
10
                  :value="label"
7fa943eb   梁灏   init
11
                  v-model="model"
0460a1e8   梁灏   fixed #812
12
                  :name="name"
ea49834d   Sergio Crisostomo   Focus on <label> ...
13
                  @change="change"
98a755be   Xotic750   Use native w3c
14
15
                  @focus="onFocus"
                  @blur="onBlur">
7fa943eb   梁灏   init
16
              <input
98a755be   Xotic750   Use native w3c
17
                  v-else
7fa943eb   梁灏   init
18
                  type="checkbox"
7fa943eb   梁灏   init
19
20
                  :class="inputClasses"
                  :disabled="disabled"
cbe03a12   梁灏   support Checkbox
21
                  :checked="currentValue"
0460a1e8   梁灏   fixed #812
22
                  :name="name"
ea49834d   Sergio Crisostomo   Focus on <label> ...
23
                  @change="change"
98a755be   Xotic750   Use native w3c
24
25
                  @focus="onFocus"
                  @blur="onBlur">
7fa943eb   梁灏   init
26
          </span>
7c2ed52c   梁灏   update Checkbox
27
          <slot><span v-if="showSlot">{{ label }}</span></slot>
7fa943eb   梁灏   init
28
29
30
      </label>
  </template>
  <script>
77f1cc2e   梁灏   Checkbox add size...
31
      import { findComponentUpward, oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
32
33
      import Emitter from '../../mixins/emitter';
  
7fa943eb   梁灏   init
34
35
36
      const prefixCls = 'ivu-checkbox';
  
      export default {
cbe03a12   梁灏   support Checkbox
37
          name: 'Checkbox',
cd78c9c4   梁灏   some comps suppor...
38
          mixins: [ Emitter ],
7fa943eb   梁灏   init
39
40
41
42
43
44
          props: {
              disabled: {
                  type: Boolean,
                  default: false
              },
              value: {
a81253ec   Rijn   Update checkbox t...
45
46
47
48
49
50
51
52
53
                  type: [String, Number, Boolean],
                  default: false
              },
              trueValue: {
                  type: [String, Number, Boolean],
                  default: true
              },
              falseValue: {
                  type: [String, Number, Boolean],
7fa943eb   梁灏   init
54
                  default: false
b923c818   梁灏   update Tree
55
              },
cbe03a12   梁灏   support Checkbox
56
57
58
              label: {
                  type: [String, Number, Boolean]
              },
b923c818   梁灏   update Tree
59
60
61
              indeterminate: {
                  type: Boolean,
                  default: false
77f1cc2e   梁灏   Checkbox add size...
62
63
64
65
66
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
                  }
0460a1e8   梁灏   fixed #812
67
68
69
              },
              name: {
                  type: String
7fa943eb   梁灏   init
70
71
72
73
74
              }
          },
          data () {
              return {
                  model: [],
cbe03a12   梁灏   support Checkbox
75
                  currentValue: this.value,
71699f6b   梁灏   optimize Checkbox...
76
                  group: false,
99cde29d   梁灏   update Checkbox
77
                  showSlot: true,
98a755be   Xotic750   Use native w3c
78
79
                  parent: findComponentUpward(this, 'CheckboxGroup'),
                  focusInner: false
b0893113   jingsam   :art: add eslint
80
              };
7fa943eb   梁灏   init
81
82
83
84
85
86
87
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
cbe03a12   梁灏   support Checkbox
88
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
77f1cc2e   梁灏   Checkbox add size...
89
90
                          [`${prefixCls}-wrapper-disabled`]: this.disabled,
                          [`${prefixCls}-${this.size}`]: !!this.size
7fa943eb   梁灏   init
91
                      }
b0893113   jingsam   :art: add eslint
92
                  ];
7fa943eb   梁灏   init
93
94
95
96
97
              },
              checkboxClasses () {
                  return [
                      `${prefixCls}`,
                      {
cbe03a12   梁灏   support Checkbox
98
                          [`${prefixCls}-checked`]: this.currentValue,
b923c818   梁灏   update Tree
99
100
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-indeterminate`]: this.indeterminate
7fa943eb   梁灏   init
101
                      }
b0893113   jingsam   :art: add eslint
102
                  ];
7fa943eb   梁灏   init
103
104
              },
              innerClasses () {
98a755be   Xotic750   Use native w3c
105
106
107
108
109
110
                  return [
                      `${prefixCls}-inner`,
                      {
                          [`${prefixCls}-focus`]: this.focusInner
                      }
                  ];
7fa943eb   梁灏   init
111
112
113
114
115
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
cbe03a12   梁灏   support Checkbox
116
          mounted () {
99cde29d   梁灏   update Checkbox
117
              this.parent = findComponentUpward(this, 'CheckboxGroup');
98a755be   Xotic750   Use native w3c
118
119
120
121
122
123
124
              if (this.parent) {
                  this.group = true;
              }
  
              if (this.group) {
                  this.parent.updateModel(true);
              } else {
7fa943eb   梁灏   init
125
                  this.updateModel();
7c2ed52c   梁灏   update Checkbox
126
                  this.showSlot = this.$slots.default !== undefined;
7fa943eb   梁灏   init
127
128
129
130
              }
          },
          methods: {
              change (event) {
7fa943eb   梁灏   init
131
132
133
134
                  if (this.disabled) {
                      return false;
                  }
  
cbe03a12   梁灏   support Checkbox
135
136
                  const checked = event.target.checked;
                  this.currentValue = checked;
a81253ec   Rijn   Update checkbox t...
137
  
98a755be   Xotic750   Use native w3c
138
                  const value = checked ? this.trueValue : this.falseValue;
a81253ec   Rijn   Update checkbox t...
139
                  this.$emit('input', value);
7fa943eb   梁灏   init
140
141
  
                  if (this.group) {
9c83f0e5   Aresn   fixed Checkbox bug
142
                      this.parent.change(this.model);
7fa943eb   梁灏   init
143
                  } else {
a81253ec   Rijn   Update checkbox t...
144
145
                      this.$emit('on-change', value);
                      this.dispatch('FormItem', 'on-form-change', value);
7fa943eb   梁灏   init
146
147
148
                  }
              },
              updateModel () {
a81253ec   Rijn   Update checkbox t...
149
                  this.currentValue = this.value === this.trueValue;
98a755be   Xotic750   Use native w3c
150
151
152
153
154
155
              },
              onBlur () {
                  this.focusInner = false;
              },
              onFocus () {
                  this.focusInner = true;
7fa943eb   梁灏   init
156
157
158
              }
          },
          watch: {
a81253ec   Rijn   Update checkbox t...
159
              value (val) {
98a755be   Xotic750   Use native w3c
160
161
162
                  if (val === this.trueValue || val === this.falseValue) {
                      this.updateModel();
                  } else {
a81253ec   Rijn   Update checkbox t...
163
164
                      throw 'Value should be trueValue or falseValue.';
                  }
7fa943eb   梁灏   init
165
166
              }
          }
b0893113   jingsam   :art: add eslint
167
168
      };
  </script>