Commit eb7f31db00f8aad1a17692d251f11172c2c2c9cd

Authored by 梁灏
1 parent ac2f8493

Switch add loading prop

examples/routers/switch.vue
1 <template> 1 <template>
2 <div> 2 <div>
3 - <i-switch v-model="m1" true-value="yes" false-value="no"> 3 + <i-switch v-model="m1" :loading="loading">
4 <span slot="open">开</span> 4 <span slot="open">开</span>
5 <span slot="close">关</span> 5 <span slot="close">关</span>
6 </i-switch> 6 </i-switch>
7 {{ m1 }} 7 {{ m1 }}
8 - <div @click="m1 = 'no'">toggle</div> 8 + <div @click="m1 = !m1">toggle</div>
  9 + <div @click="loading = !loading">loading</div>
9 <br><br> 10 <br><br>
10 - <i-switch size="large"></i-switch> 11 + <i-switch size="large" loading></i-switch>
11 <i-switch></i-switch> 12 <i-switch></i-switch>
12 - <i-switch size="small"></i-switch> 13 + <i-switch size="small" v-model="m1" :loading="loading"></i-switch>
13 <br><br> 14 <br><br>
14 <i-switch> 15 <i-switch>
15 <span slot="open">开</span> 16 <span slot="open">开</span>
@@ -24,7 +25,7 @@ @@ -24,7 +25,7 @@
24 <span slot="open">开启</span> 25 <span slot="open">开启</span>
25 <span slot="close">关闭</span> 26 <span slot="close">关闭</span>
26 </i-switch> 27 </i-switch>
27 - <i-switch size="large"> 28 + <i-switch size="large" v-model="m1" :loading="loading">
28 <span slot="open">ON</span> 29 <span slot="open">ON</span>
29 <span slot="close">OFF</span> 30 <span slot="close">OFF</span>
30 </i-switch> 31 </i-switch>
@@ -37,8 +38,9 @@ @@ -37,8 +38,9 @@
37 export default { 38 export default {
38 data () { 39 data () {
39 return { 40 return {
40 - m1: 'yes',  
41 - disabled: true 41 + m1: true,
  42 + disabled: true,
  43 + loading: false
42 } 44 }
43 }, 45 },
44 methods: { 46 methods: {
src/components/switch/switch.vue
@@ -45,6 +45,10 @@ @@ -45,6 +45,10 @@
45 }, 45 },
46 name: { 46 name: {
47 type: String 47 type: String
  48 + },
  49 + loading: {
  50 + type: Boolean,
  51 + default: false
48 } 52 }
49 }, 53 },
50 data () { 54 data () {
@@ -59,7 +63,8 @@ @@ -59,7 +63,8 @@
59 { 63 {
60 [`${prefixCls}-checked`]: this.currentValue === this.trueValue, 64 [`${prefixCls}-checked`]: this.currentValue === this.trueValue,
61 [`${prefixCls}-disabled`]: this.disabled, 65 [`${prefixCls}-disabled`]: this.disabled,
62 - [`${prefixCls}-${this.size}`]: !!this.size 66 + [`${prefixCls}-${this.size}`]: !!this.size,
  67 + [`${prefixCls}-loading`]: this.loading,
63 } 68 }
64 ]; 69 ];
65 }, 70 },
@@ -70,7 +75,7 @@ @@ -70,7 +75,7 @@
70 methods: { 75 methods: {
71 toggle (event) { 76 toggle (event) {
72 event.preventDefault(); 77 event.preventDefault();
73 - if (this.disabled) { 78 + if (this.disabled || this.loading) {
74 return false; 79 return false;
75 } 80 }
76 81
src/styles/components/switch.less
@@ -14,6 +14,10 @@ @@ -14,6 +14,10 @@
14 user-select: none; 14 user-select: none;
15 transition: all @transition-time @ease-in-out; 15 transition: all @transition-time @ease-in-out;
16 16
  17 + &-loading{
  18 + opacity: .4;
  19 + }
  20 +
17 &-inner { 21 &-inner {
18 color: #fff; 22 color: #fff;
19 font-size: @font-size-small; 23 font-size: @font-size-small;
@@ -44,6 +48,26 @@ @@ -44,6 +48,26 @@
44 width: 26px; 48 width: 26px;
45 } 49 }
46 50
  51 + &:before{
  52 + content: '';
  53 + display: none;
  54 + width: 14px;
  55 + height: 14px;
  56 + border-radius: 50%;
  57 + background-color: transparent;
  58 + position: absolute;
  59 + left: 3px;
  60 + top: 3px;
  61 + z-index: 1;
  62 + border: 1px solid @primary-color;
  63 + border-color: transparent transparent transparent @primary-color;
  64 + animation: switch-loading 1s linear;
  65 + animation-iteration-count: infinite;
  66 + }
  67 + &-loading:before{
  68 + display: block;
  69 + }
  70 +
47 &:focus { 71 &:focus {
48 box-shadow: 0 0 0 2px fade(@primary-color, 20%); 72 box-shadow: 0 0 0 2px fade(@primary-color, 20%);
49 outline: 0; 73 outline: 0;
@@ -64,11 +88,20 @@ @@ -64,11 +88,20 @@
64 &:active:after { 88 &:active:after {
65 width: 14px; 89 width: 14px;
66 } 90 }
  91 + &:before{
  92 + width: 10px;
  93 + height: 10px;
  94 + left: 2px;
  95 + top: 2px;
  96 + }
67 } 97 }
68 98
69 &-small&-checked:after { 99 &-small&-checked:after {
70 left: 13px; 100 left: 13px;
71 } 101 }
  102 + &-small&-checked:before {
  103 + left: 14px;
  104 + }
72 105
73 &-small:active&-checked:after { 106 &-small:active&-checked:after {
74 left: 11px; 107 left: 11px;
@@ -88,6 +121,9 @@ @@ -88,6 +121,9 @@
88 &-large&-checked:after { 121 &-large&-checked:after {
89 left: 35px; 122 left: 35px;
90 } 123 }
  124 + &-large&-checked:before {
  125 + left: 37px;
  126 + }
91 127
92 &-large:active&-checked:after { 128 &-large:active&-checked:after {
93 left: 23px; 129 left: 23px;
@@ -104,6 +140,9 @@ @@ -104,6 +140,9 @@
104 &:after { 140 &:after {
105 left: 23px; 141 left: 23px;
106 } 142 }
  143 + &:before{
  144 + left: 25px;
  145 + }
107 146
108 &:active:after { 147 &:active:after {
109 left: 15px; 148 left: 15px;
@@ -124,4 +163,14 @@ @@ -124,4 +163,14 @@
124 color: #ccc; 163 color: #ccc;
125 } 164 }
126 } 165 }
  166 +
127 } 167 }
  168 +
  169 +@keyframes switch-loading {
  170 + 0% {
  171 + transform: rotate(0);
  172 + }
  173 + 100% {
  174 + transform: rotate(360deg);
  175 + }
  176 +}
128 \ No newline at end of file 177 \ No newline at end of file