feat: add switch
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
|
||||
@component-namespace o2{
|
||||
@component switch {
|
||||
height: 29px;
|
||||
width: 49px;
|
||||
display: inline-block;
|
||||
border-radius: 16px;
|
||||
position: relative;
|
||||
border: 1px solid;
|
||||
|
||||
@descendent node {
|
||||
width: 27px;
|
||||
height: 27px;
|
||||
border-radius: 100%;
|
||||
background-color: #fff;
|
||||
border: .5px solid rgba(0, 0, 0, .1);
|
||||
position: absolute;
|
||||
box-shadow: 0 3px 1px 0 rgba(0, 0, 0, .05), 0 2px 2px 0 rgba(0, 0, 0, .1), 0 3px 3px 0 rgba(0, 0, 0, .05);
|
||||
@when on {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
@when off {
|
||||
right: 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@when on {
|
||||
background-color: #44db5e;
|
||||
border-color: #44db5e;
|
||||
}
|
||||
|
||||
@when off {
|
||||
background-color: #fff;
|
||||
border-color: rgba(0, 0, 0, .1);
|
||||
}
|
||||
|
||||
@when loading {
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
@when disabled {
|
||||
background-color: #f2f2f2;
|
||||
border-color: rgba(0, 0, 0, .1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<template>
|
||||
<div class="o2-switch" :class="['is-' + switchState]" @click="toggleState">
|
||||
<div class="o2-switch-node"></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
/**
|
||||
* o2-switch
|
||||
* @module components/switch
|
||||
* @desc 开关
|
||||
* @param {boolean} [checked=false] - 开关状态
|
||||
* @param {boolean} [disabled=false] - 禁用
|
||||
* @param {boolean} [loading=false] - loading状态
|
||||
* @param {callback} [onChange] - 开关状态改变回调函数。
|
||||
*
|
||||
* @example
|
||||
* <o2-switch checked="true" disabled="false"></o2-switch>
|
||||
*/
|
||||
export default
|
||||
{
|
||||
name: 'o2-switch',
|
||||
props: {
|
||||
checked: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
disabled: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
loading: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
onChange: {
|
||||
type: Function,
|
||||
default: function() {}
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
switchState: function() {
|
||||
if (this.disabled) {
|
||||
return 'disabled';
|
||||
} else if (this.loading) {
|
||||
return 'loading';
|
||||
} else if (this.checked) {
|
||||
return 'on';
|
||||
} else {
|
||||
return 'off';
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
/*
|
||||
* 开关状态交互。
|
||||
*/
|
||||
toggleState: function() {
|
||||
if(this.disabled || this.loading) return;
|
||||
this.onChange(!this.checked);
|
||||
},
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
getState: function() {
|
||||
return this.checked;
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
Reference in New Issue
Block a user