Blame view

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