Blame view

src/components/switch/switch.vue 3.01 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']);
22111588   梁灏   Switch support gl...
44
45
46
                  },
                  default () {
                      return this.$IVIEW.size === '' ? 'default' : this.$IVIEW.size;
7fa943eb   梁灏   init
47
                  }
0460a1e8   梁灏   fixed #812
48
49
50
              },
              name: {
                  type: String
eb7f31db   梁灏   Switch add loadin...
51
52
53
54
              },
              loading: {
                  type: Boolean,
                  default: false
7fa943eb   梁灏   init
55
56
              }
          },
2d5ba278   梁灏   support Switch
57
58
59
          data () {
              return {
                  currentValue: this.value
5d122b37   梁灏   support Alert
60
              };
2d5ba278   梁灏   support Switch
61
          },
7fa943eb   梁灏   init
62
63
64
65
66
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
1f41c9ca   梁灏   fixed #1797
67
                          [`${prefixCls}-checked`]: this.currentValue === this.trueValue,
7fa943eb   梁灏   init
68
                          [`${prefixCls}-disabled`]: this.disabled,
eb7f31db   梁灏   Switch add loadin...
69
70
                          [`${prefixCls}-${this.size}`]: !!this.size,
                          [`${prefixCls}-loading`]: this.loading,
7fa943eb   梁灏   init
71
                      }
b0893113   jingsam   :art: add eslint
72
                  ];
7fa943eb   梁灏   init
73
74
75
76
77
78
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              }
          },
          methods: {
5bb01822   梁灏   Switch prevent sp...
79
80
              toggle (event) {
                  event.preventDefault();
eb7f31db   梁灏   Switch add loadin...
81
                  if (this.disabled || this.loading) {
7fa943eb   梁灏   init
82
83
84
                      return false;
                  }
  
6c97e57a   梁灏   fixed #1399
85
86
                  const checked = this.currentValue === this.trueValue ? this.falseValue : this.trueValue;
  
2d5ba278   梁灏   support Switch
87
88
89
                  this.currentValue = checked;
                  this.$emit('input', checked);
                  this.$emit('on-change', checked);
b2012015   梁灏   fixed Switch disp...
90
                  this.dispatch('FormItem', 'on-form-change', checked);
2d5ba278   梁灏   support Switch
91
92
93
94
              }
          },
          watch: {
              value (val) {
6c97e57a   梁灏   fixed #1399
95
96
97
                  if (val !== this.trueValue && val !== this.falseValue) {
                      throw 'Value should be trueValue or falseValue.';
                  }
2d5ba278   梁灏   support Switch
98
                  this.currentValue = val;
7fa943eb   梁灏   init
99
100
              }
          }
b0893113   jingsam   :art: add eslint
101
102
      };
  </script>