Blame view

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