Blame view

src/components/switch/switch.vue 2.46 KB
7fa943eb   梁灏   init
1
2
3
  <template>
      <span :class="wrapClasses" @click="toggle">
          <span :class="innerClasses">
1f41c9ca   梁灏   fixed #1797
4
5
              <slot name="open" v-if="currentValue === trueValue"></slot>
              <slot name="close" v-if="currentValue === falseValue"></slot>
7fa943eb   梁灏   init
6
7
8
9
10
          </span>
      </span>
  </template>
  <script>
      import { oneOf } from '../../utils/assist';
cd78c9c4   梁灏   some comps suppor...
11
      import Emitter from '../../mixins/emitter';
7fa943eb   梁灏   init
12
13
14
15
  
      const prefixCls = 'ivu-switch';
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
16
          name: 'Switch',
cd78c9c4   梁灏   some comps suppor...
17
          mixins: [ Emitter ],
7fa943eb   梁灏   init
18
          props: {
2d5ba278   梁灏   support Switch
19
              value: {
6c97e57a   梁灏   fixed #1399
20
21
22
23
24
25
26
27
28
                  type: [String, Number, Boolean],
                  default: false
              },
              trueValue: {
                  type: [String, Number, Boolean],
                  default: true
              },
              falseValue: {
                  type: [String, Number, Boolean],
7fa943eb   梁灏   init
29
30
31
32
33
34
35
36
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              size: {
                  validator (value) {
f00a037c   梁灏   some Components's...
37
                      return oneOf(value, ['large', 'small', 'default']);
7fa943eb   梁灏   init
38
39
40
                  }
              }
          },
2d5ba278   梁灏   support Switch
41
42
43
          data () {
              return {
                  currentValue: this.value
5d122b37   梁灏   support Alert
44
              };
2d5ba278   梁灏   support Switch
45
          },
7fa943eb   梁灏   init
46
47
48
49
50
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
1f41c9ca   梁灏   fixed #1797
51
                          [`${prefixCls}-checked`]: this.currentValue === this.trueValue,
7fa943eb   梁灏   init
52
53
54
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-${this.size}`]: !!this.size
                      }
b0893113   jingsam   :art: add eslint
55
                  ];
7fa943eb   梁灏   init
56
57
58
59
60
61
62
63
64
65
66
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              }
          },
          methods: {
              toggle () {
                  if (this.disabled) {
                      return false;
                  }
  
6c97e57a   梁灏   fixed #1399
67
68
                  const checked = this.currentValue === this.trueValue ? this.falseValue : this.trueValue;
  
2d5ba278   梁灏   support Switch
69
70
71
                  this.currentValue = checked;
                  this.$emit('input', checked);
                  this.$emit('on-change', checked);
b2012015   梁灏   fixed Switch disp...
72
                  this.dispatch('FormItem', 'on-form-change', checked);
2d5ba278   梁灏   support Switch
73
74
75
76
              }
          },
          watch: {
              value (val) {
6c97e57a   梁灏   fixed #1399
77
78
79
                  if (val !== this.trueValue && val !== this.falseValue) {
                      throw 'Value should be trueValue or falseValue.';
                  }
2d5ba278   梁灏   support Switch
80
                  this.currentValue = val;
7fa943eb   梁灏   init
81
82
              }
          }
b0893113   jingsam   :art: add eslint
83
84
      };
  </script>