Blame view

src/components/switch/switch.vue 1.89 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
11
12
13
14
          </span>
      </span>
  </template>
  <script>
      import { oneOf } from '../../utils/assist';
  
      const prefixCls = 'ivu-switch';
  
      export default {
34ee7b4a   梁灏   support Tree & ad...
15
          name: 'Switch',
7fa943eb   梁灏   init
16
          props: {
2d5ba278   梁灏   support Switch
17
              value: {
7fa943eb   梁灏   init
18
19
20
21
22
23
24
25
26
                  type: Boolean,
                  default: false
              },
              disabled: {
                  type: Boolean,
                  default: false
              },
              size: {
                  validator (value) {
2af5843d   梁灏   Switch add large ...
27
                      return oneOf(value, ['large', 'small']);
7fa943eb   梁灏   init
28
29
30
                  }
              }
          },
2d5ba278   梁灏   support Switch
31
32
33
          data () {
              return {
                  currentValue: this.value
5d122b37   梁灏   support Alert
34
              };
2d5ba278   梁灏   support Switch
35
          },
7fa943eb   梁灏   init
36
37
38
39
40
          computed: {
              wrapClasses () {
                  return [
                      `${prefixCls}`,
                      {
2d5ba278   梁灏   support Switch
41
                          [`${prefixCls}-checked`]: this.currentValue,
7fa943eb   梁灏   init
42
43
44
                          [`${prefixCls}-disabled`]: this.disabled,
                          [`${prefixCls}-${this.size}`]: !!this.size
                      }
b0893113   jingsam   :art: add eslint
45
                  ];
7fa943eb   梁灏   init
46
47
48
49
50
51
52
53
54
55
56
              },
              innerClasses () {
                  return `${prefixCls}-inner`;
              }
          },
          methods: {
              toggle () {
                  if (this.disabled) {
                      return false;
                  }
  
2d5ba278   梁灏   support Switch
57
58
59
60
61
62
63
64
65
66
67
                  const checked = !this.currentValue;
                  this.currentValue = checked;
                  this.$emit('input', checked);
                  this.$emit('on-change', checked);
                  // todo 事件
  //                this.$dispatch('on-form-change', this.checked);
              }
          },
          watch: {
              value (val) {
                  this.currentValue = val;
7fa943eb   梁灏   init
68
69
              }
          }
b0893113   jingsam   :art: add eslint
70
71
      };
  </script>