Blame view

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