Blame view

src/components/checkbox/checkbox.vue 5.36 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
          props: {
              disabled: {
                  type: Boolean,
                  default: false
              },
e80b805f   chenhaodong   初始化
44
45
46
47
              readonly: {
                  type: Boolean,
                  default: false
              },
7fa943eb   梁灏   init
48
              value: {
a81253ec   Rijn   Update checkbox t...
49
50
51
52
53
54
55
56
57
                  type: [String, Number, Boolean],
                  default: false
              },
              trueValue: {
                  type: [String, Number, Boolean],
                  default: true
              },
              falseValue: {
                  type: [String, Number, Boolean],
7fa943eb   梁灏   init
58
                  default: false
b923c818   梁灏   update Tree
59
              },
cbe03a12   梁灏   support Checkbox
60
61
62
              label: {
                  type: [String, Number, Boolean]
              },
b923c818   梁灏   update Tree
63
64
65
              indeterminate: {
                  type: Boolean,
                  default: false
77f1cc2e   梁灏   Checkbox add size...
66
67
68
69
              },
              size: {
                  validator (value) {
                      return oneOf(value, ['small', 'large', 'default']);
2968067e   梁灏   Checkbox support ...
70
71
                  },
                  default () {
fe5ffd7f   梁灏   fixed #4196 #4165
72
                      return !this.$IVIEW || this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
77f1cc2e   梁灏   Checkbox add size...
73
                  }
0460a1e8   梁灏   fixed #812
74
75
76
              },
              name: {
                  type: String
7fa943eb   梁灏   init
77
78
79
80
81
              }
          },
          data () {
              return {
                  model: [],
cbe03a12   梁灏   support Checkbox
82
                  currentValue: this.value,
71699f6b   梁灏   optimize Checkbox...
83
                  group: false,
99cde29d   梁灏   update Checkbox
84
                  showSlot: true,
98a755be   Xotic750   Use native w3c
85
86
                  parent: findComponentUpward(this, 'CheckboxGroup'),
                  focusInner: false
b0893113   jingsam   :art: add eslint
87
              };
7fa943eb   梁灏   init
88
89
90
91
92
93
94
          },
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}-wrapper`,
                      {
                          [`${prefixCls}-group-item`]: this.group,
cbe03a12   梁灏   support Checkbox
95
                          [`${prefixCls}-wrapper-checked`]: this.currentValue,
77f1cc2e   梁灏   Checkbox add size...
96
97
                          [`${prefixCls}-wrapper-disabled`]: this.disabled,
                          [`${prefixCls}-${this.size}`]: !!this.size
7fa943eb   梁灏   init
98
                      }
b0893113   jingsam   :art: add eslint
99
                  ];
7fa943eb   梁灏   init
100
101
102
103
104
              },
              checkboxClasses () {
                  return [
                      `${prefixCls}`,
                      {
cbe03a12   梁灏   support Checkbox
105
                          [`${prefixCls}-checked`]: this.currentValue,
b923c818   梁灏   update Tree
106
107
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-indeterminate`]: this.indeterminate
7fa943eb   梁灏   init
108
                      }
b0893113   jingsam   :art: add eslint
109
                  ];
7fa943eb   梁灏   init
110
111
              },
              innerClasses () {
98a755be   Xotic750   Use native w3c
112
113
114
115
116
117
                  return [
                      `${prefixCls}-inner`,
                      {
                          [`${prefixCls}-focus`]: this.focusInner
                      }
                  ];
7fa943eb   梁灏   init
118
119
120
121
122
              },
              inputClasses () {
                  return `${prefixCls}-input`;
              }
          },
cbe03a12   梁灏   support Checkbox
123
          mounted () {
99cde29d   梁灏   update Checkbox
124
              this.parent = findComponentUpward(this, 'CheckboxGroup');
98a755be   Xotic750   Use native w3c
125
126
127
128
129
130
131
              if (this.parent) {
                  this.group = true;
              }
  
              if (this.group) {
                  this.parent.updateModel(true);
              } else {
7fa943eb   梁灏   init
132
                  this.updateModel();
7c2ed52c   梁灏   update Checkbox
133
                  this.showSlot = this.$slots.default !== undefined;
7fa943eb   梁灏   init
134
135
136
137
              }
          },
          methods: {
              change (event) {
7fa943eb   梁灏   init
138
139
140
                  if (this.disabled) {
                      return false;
                  }
e80b805f   chenhaodong   初始化
141
142
143
                  if (this.readonly) {
                      return false;
                  }
7fa943eb   梁灏   init
144
  
cbe03a12   梁灏   support Checkbox
145
146
                  const checked = event.target.checked;
                  this.currentValue = checked;
a81253ec   Rijn   Update checkbox t...
147
  
98a755be   Xotic750   Use native w3c
148
                  const value = checked ? this.trueValue : this.falseValue;
a81253ec   Rijn   Update checkbox t...
149
                  this.$emit('input', value);
7fa943eb   梁灏   init
150
151
  
                  if (this.group) {
9c83f0e5   Aresn   fixed Checkbox bug
152
                      this.parent.change(this.model);
7fa943eb   梁灏   init
153
                  } else {
a81253ec   Rijn   Update checkbox t...
154
155
                      this.$emit('on-change', value);
                      this.dispatch('FormItem', 'on-form-change', value);
7fa943eb   梁灏   init
156
157
158
                  }
              },
              updateModel () {
a81253ec   Rijn   Update checkbox t...
159
                  this.currentValue = this.value === this.trueValue;
98a755be   Xotic750   Use native w3c
160
161
162
163
164
165
              },
              onBlur () {
                  this.focusInner = false;
              },
              onFocus () {
                  this.focusInner = true;
7fa943eb   梁灏   init
166
167
168
              }
          },
          watch: {
a81253ec   Rijn   Update checkbox t...
169
              value (val) {
98a755be   Xotic750   Use native w3c
170
171
172
                  if (val === this.trueValue || val === this.falseValue) {
                      this.updateModel();
                  } else {
a81253ec   Rijn   Update checkbox t...
173
174
                      throw 'Value should be trueValue or falseValue.';
                  }
7fa943eb   梁灏   init
175
176
              }
          }
b0893113   jingsam   :art: add eslint
177
178
      };
  </script>