Blame view

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