Blame view

src/components/switch/switch.vue 1.95 KB
7fa943eb   梁灏   init
1
2
3
  <template>
      <span :class="wrapClasses" @click="toggle">
          <span :class="innerClasses">
2d5ba278   梁灏   support Switch
4
5
              <slot name="open" v-if="currentValue"></slot>
              <slot name="close" v-if="!currentValue"></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: {
7fa943eb   梁灏   init
20
21
22
23
24
25
26
27
28
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              size: {
                  validator (value) {
f00a037c   梁灏   some Components's...
29
                      return oneOf(value, ['large', 'small', 'default']);
7fa943eb   梁灏   init
30
31
32
                  }
              }
          },
2d5ba278   梁灏   support Switch
33
34
35
          data () {
              return {
                  currentValue: this.value
5d122b37   梁灏   support Alert
36
              };
2d5ba278   梁灏   support Switch
37
          },
7fa943eb   梁灏   init
38
39
40
41
42
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
2d5ba278   梁灏   support Switch
43
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
44
45
46
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-${this.size}`]: !!this.size
                      }
b0893113   jingsam   :art: add eslint
47
                  ];
7fa943eb   梁灏   init
48
49
50
51
52
53
54
55
56
57
58
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              }
          },
          methods: {
              toggle () {
                  if (this.disabled) {
                      return false;
                  }
  
2d5ba278   梁灏   support Switch
59
60
61
62
                  const checked = !this.currentValue;
                  this.currentValue = checked;
                  this.$emit('input', checked);
                  this.$emit('on-change', checked);
b2012015   梁灏   fixed Switch disp...
63
                  this.dispatch('FormItem', 'on-form-change', checked);
2d5ba278   梁灏   support Switch
64
65
66
67
68
              }
          },
          watch: {
              value (val) {
                  this.currentValue = val;
7fa943eb   梁灏   init
69
70
              }
          }
b0893113   jingsam   :art: add eslint
71
72
      };
  </script>