Blame view

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