Blame view

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