Blame view

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