Switch add loading prop

This commit is contained in:
梁灏 2018-06-22 10:14:13 +08:00
parent ac2f8493b1
commit eb7f31db00
3 changed files with 65 additions and 9 deletions

View file

@ -1,15 +1,16 @@
<template> <template>
<div> <div>
<i-switch v-model="m1" true-value="yes" false-value="no"> <i-switch v-model="m1" :loading="loading">
<span slot="open"></span> <span slot="open"></span>
<span slot="close"></span> <span slot="close"></span>
</i-switch> </i-switch>
{{ m1 }} {{ m1 }}
<div @click="m1 = 'no'">toggle</div> <div @click="m1 = !m1">toggle</div>
<div @click="loading = !loading">loading</div>
<br><br> <br><br>
<i-switch size="large"></i-switch> <i-switch size="large" loading></i-switch>
<i-switch></i-switch> <i-switch></i-switch>
<i-switch size="small"></i-switch> <i-switch size="small" v-model="m1" :loading="loading"></i-switch>
<br><br> <br><br>
<i-switch> <i-switch>
<span slot="open"></span> <span slot="open"></span>
@ -24,7 +25,7 @@
<span slot="open">开启</span> <span slot="open">开启</span>
<span slot="close">关闭</span> <span slot="close">关闭</span>
</i-switch> </i-switch>
<i-switch size="large"> <i-switch size="large" v-model="m1" :loading="loading">
<span slot="open">ON</span> <span slot="open">ON</span>
<span slot="close">OFF</span> <span slot="close">OFF</span>
</i-switch> </i-switch>
@ -37,8 +38,9 @@
export default { export default {
data () { data () {
return { return {
m1: 'yes', m1: true,
disabled: true disabled: true,
loading: false
} }
}, },
methods: { methods: {

View file

@ -45,6 +45,10 @@
}, },
name: { name: {
type: String type: String
},
loading: {
type: Boolean,
default: false
} }
}, },
data () { data () {
@ -59,7 +63,8 @@
{ {
[`${prefixCls}-checked`]: this.currentValue === this.trueValue, [`${prefixCls}-checked`]: this.currentValue === this.trueValue,
[`${prefixCls}-disabled`]: this.disabled, [`${prefixCls}-disabled`]: this.disabled,
[`${prefixCls}-${this.size}`]: !!this.size [`${prefixCls}-${this.size}`]: !!this.size,
[`${prefixCls}-loading`]: this.loading,
} }
]; ];
}, },
@ -70,7 +75,7 @@
methods: { methods: {
toggle (event) { toggle (event) {
event.preventDefault(); event.preventDefault();
if (this.disabled) { if (this.disabled || this.loading) {
return false; return false;
} }

View file

@ -14,6 +14,10 @@
user-select: none; user-select: none;
transition: all @transition-time @ease-in-out; transition: all @transition-time @ease-in-out;
&-loading{
opacity: .4;
}
&-inner { &-inner {
color: #fff; color: #fff;
font-size: @font-size-small; font-size: @font-size-small;
@ -44,6 +48,26 @@
width: 26px; width: 26px;
} }
&:before{
content: '';
display: none;
width: 14px;
height: 14px;
border-radius: 50%;
background-color: transparent;
position: absolute;
left: 3px;
top: 3px;
z-index: 1;
border: 1px solid @primary-color;
border-color: transparent transparent transparent @primary-color;
animation: switch-loading 1s linear;
animation-iteration-count: infinite;
}
&-loading:before{
display: block;
}
&:focus { &:focus {
box-shadow: 0 0 0 2px fade(@primary-color, 20%); box-shadow: 0 0 0 2px fade(@primary-color, 20%);
outline: 0; outline: 0;
@ -64,11 +88,20 @@
&:active:after { &:active:after {
width: 14px; width: 14px;
} }
&:before{
width: 10px;
height: 10px;
left: 2px;
top: 2px;
}
} }
&-small&-checked:after { &-small&-checked:after {
left: 13px; left: 13px;
} }
&-small&-checked:before {
left: 14px;
}
&-small:active&-checked:after { &-small:active&-checked:after {
left: 11px; left: 11px;
@ -88,6 +121,9 @@
&-large&-checked:after { &-large&-checked:after {
left: 35px; left: 35px;
} }
&-large&-checked:before {
left: 37px;
}
&-large:active&-checked:after { &-large:active&-checked:after {
left: 23px; left: 23px;
@ -104,6 +140,9 @@
&:after { &:after {
left: 23px; left: 23px;
} }
&:before{
left: 25px;
}
&:active:after { &:active:after {
left: 15px; left: 15px;
@ -124,4 +163,14 @@
color: #ccc; color: #ccc;
} }
} }
} }
@keyframes switch-loading {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}