feat: add switch

This commit is contained in:
jiangruowei 2017-02-07 17:40:38 +08:00
parent 4562ca8b19
commit 72ea01032e
13 changed files with 251 additions and 2 deletions

View File

@ -1,4 +1,5 @@
{
"sample": "./packages/sample/index.js",
"button": "./packages/button/index.js"
"button": "./packages/button/index.js",
"switch": "./packages/switch/index.js"
}

80
docs/examples/switch.md Normal file
View File

@ -0,0 +1,80 @@
<script>
export default {
data() {
return {
switchState: true
};
},
computed: {
switchStateText() {
return this.switchState ? 'ON' : 'OFF';
}
},
methods: {
updateState(newState) {
console.log('changing');
this.switchState = newState;
}
}
};
</script>
<style lang="css">
@component-namespace page {
@component switch {
padding: 0 15px 15px;
@descendent sample {
margin: 0 15px;
}
@descendent text {
margin-right: 20px;
}
}
}
</style>
## Switch组件
### 基础用法
:::demo 样例代码
```html
<div class="page-switch">
<span class="page-switch-text">Switch state: {{switchStateText}}</span>
<o2-switch class="page-switch-sample" :checked="switchState" :onChange="updateState"></o2-switch>
<o2-switch class="page-switch-sample" :checked="false" :disabled="true"></o2-switch>
</div>
```
```javascript
export default {
data() {
return {
switchState: false
};
},
computed: {
switchStateText() {
return this.switchState ? 'on' : 'off';
}
},
methods: {
updateState(newState) {
console.log('changing');
this.switchState = newState;
}
}
};
```
:::
### API
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|-----------|-----------|-----------|-------------|-------------|
| checked | 开关状态 | boolean | false | true,false |
| loading | loading状态 | boolean | false | true,false |
| disabled | 禁用状态 | boolean | false | true,false |
| onChange | 回调 | function | function{} | - |

View File

@ -13,6 +13,10 @@
{
"path": "/button",
"title": "Button组件"
},
{
"path": "/switch",
"title": "Switch组件"
}
]
}

View File

@ -100,6 +100,7 @@
"prismjs": "^1.5.1",
"rimraf": "^2.5.4",
"run-sequence": "^1.2.2",
"saladcss-bem":"^0.0.1",
"sass-loader": "^3.2.3",
"style-loader": "^0.13.1",
"theaterjs": "^3.0.0",

View File

26
packages/switch/README.md Normal file
View File

@ -0,0 +1,26 @@
# @youzan/<%= name %>
!!! 请在此处填写你的文档最简单描述 !!!
[![version][version-image]][download-url]
[![download][download-image]][download-url]
[version-image]: http://npm.qima-inc.com/badge/v/@youzan/<%= name %>.svg?style=flat-square
[download-image]: http://npm.qima-inc.com/badge/d/@youzan/<%= name %>.svg?style=flat-square
[download-url]: http://npm.qima-inc.com/package/@youzan/<%= name %>
## Demo
## Usage
## API
| 参数 | 说明 | 类型 | 默认值 | 可选值 |
|-----------|-----------|-----------|-------------|-------------|
| className | 自定义额外类名 | string | '' | '' |
## License
[MIT](https://opensource.org/licenses/MIT)

3
packages/switch/index.js Normal file
View File

@ -0,0 +1,3 @@
import Switch from './src/switch';
export default Switch;

View File

@ -0,0 +1 @@
@import "./src/switch.pcss";

View File

@ -0,0 +1,10 @@
{
"name": "@youzan/o2-switch",
"version": "0.0.1",
"description": "switch component",
"main": "./lib/index.js",
"author": "jiangruowei",
"license": "MIT",
"devDependencies": {},
"dependencies": {}
}

View File

@ -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);
}
}
}

View File

@ -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>

View File

@ -2,3 +2,4 @@
css组件库 入口,从各个地方拿css文件组装成css组件库
*/
@import '../../button/index.pcss';
@import '../../switch/index.pcss';

View File

@ -1,5 +1,6 @@
import Sample from '../packages/sample/index.js';
import Button from '../packages/button/index.js';
import Switch from '../packages/switch/index.js';
import 'es6-promise/auto';
import axios from 'axios';
import foreach from 'lodash/foreach';
@ -14,6 +15,7 @@ const install = function(Vue) {
Vue.component(Sample.name, Sample);
Vue.component(Button.name, Button);
Vue.component(Switch.name, Switch);
};
// auto install
@ -32,5 +34,6 @@ module.exports = {
map
},
Sample,
Button
Button,
Switch
};