[improvement] Switch: add active-color prop (#845)

This commit is contained in:
neverland 2018-11-01 19:58:17 +08:00 committed by GitHub
parent 3ce1aa0e7d
commit 5e0a7cbea4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 15 deletions

View File

@ -1,4 +1,5 @@
import Page from '../../common/page'; import Page from '../../common/page';
import Dialog from '../../dist/dialog/dialog';
Page({ Page({
data: { data: {
@ -11,14 +12,11 @@ Page({
}, },
onChange2({ detail }) { onChange2({ detail }) {
wx.showModal({ Dialog.confirm({
title: '提示', title: '提示',
content: '是否切换开关?', message: '是否切换开关?'
success: res => { }).then((res) => {
if (res.confirm) { this.setData({ checked2: detail });
this.setData({ checked2: detail });
}
}
}); });
} }
}); });

View File

@ -2,6 +2,7 @@
"navigationBarTitleText": "Switch 开关", "navigationBarTitleText": "Switch 开关",
"usingComponents": { "usingComponents": {
"demo-block": "../../components/demo-block/index", "demo-block": "../../components/demo-block/index",
"van-switch": "../../dist/switch/index" "van-switch": "../../dist/switch/index",
"van-dialog": "../../dist/dialog/index"
} }
} }

View File

@ -25,6 +25,10 @@
<van-switch <van-switch
checked="{{ checked2 }}" checked="{{ checked2 }}"
size="36px" size="36px"
active-color="#4b0"
inactive-color="#f44"
bind:change="onChange2" bind:change="onChange2"
/> />
</demo-block> </demo-block>
<van-dialog id="van-dialog" />

View File

@ -1,6 +1,7 @@
## Switch 开关 ## Switch 开关
### 使用指南 ### 使用指南
在 index.json 中引入组件 在 index.json 中引入组件
```json ```json
"usingComponents": { "usingComponents": {
@ -11,6 +12,7 @@
### 代码演示 ### 代码演示
#### 基础用法 #### 基础用法
```html ```html
<van-switch checked="{{ checked }}" bind:change="onChange" /> <van-switch checked="{{ checked }}" bind:change="onChange" />
``` ```
@ -29,20 +31,25 @@ Page({
``` ```
#### 禁用状态 #### 禁用状态
```html ```html
<van-switch checked="{{ checked }}" disabled /> <van-switch checked="{{ checked }}" disabled />
``` ```
#### 加载状态 #### 加载状态
```html ```html
<van-switch checked="{{ checked }}" loading /> <van-switch checked="{{ checked }}" loading />
``` ```
#### 高级用法 #### 高级用法
```html ```html
<van-switch <van-switch
size="36px" size="36px"
checked="{{ checked }}" checked="{{ checked }}"
active-color="#4b0"
inactive-color="#f44"
bind:change="onChange" bind:change="onChange"
/> />
``` ```
@ -76,6 +83,8 @@ Page({
| loading | 是否为加载状态 | `Boolean` | `false` | | loading | 是否为加载状态 | `Boolean` | `false` |
| disabled | 是否为禁用状态 | `Boolean` | `false` | | disabled | 是否为禁用状态 | `Boolean` | `false` |
| size | 开关尺寸 | `String` | `30px` | | size | 开关尺寸 | `String` | `30px` |
| active-color | 打开时的背景色 | `String` | `#1989fa` |
| inactive-color | 关闭时的背景色 | `String` | `#fff` |
### Event ### Event

View File

@ -2,13 +2,14 @@
.van-switch { .van-switch {
height: 1em; height: 1em;
width: 1.6em; width: 1.8em;
display: inline-block; display: inline-block;
position: relative; position: relative;
background: @white; border-radius: 1em;
box-sizing: content-box; box-sizing: content-box;
border: 1px solid rgba(0, 0, 0, .1); border: 1px solid rgba(0, 0, 0, .1);
border-radius: 1em; background-color: @white;
transition: background-color .3s;
&__node { &__node {
top: 0; top: 0;
@ -35,7 +36,7 @@
background-color: @blue; background-color: @blue;
.van-switch__node { .van-switch__node {
transform: translateX(.6em); transform: translateX(.8em);
} }
} }

View File

@ -9,6 +9,8 @@ VantComponent({
checked: Boolean, checked: Boolean,
loading: Boolean, loading: Boolean,
disabled: Boolean, disabled: Boolean,
activeColor: String,
inactiveColor: String,
size: { size: {
type: String, type: String,
value: '30px' value: '30px'
@ -21,6 +23,20 @@ VantComponent({
} }
}, },
computed: {
classes(): string {
return this.classNames('custom-class', 'van-switch', {
'van-switch--on': this.data.checked,
'van-switch--disabled': this.data.disabled
});
},
style() {
const backgroundColor = this.data.checked ? this.data.activeColor : this.data.inactiveColor;
return `font-size: ${this.data.size}; ${ backgroundColor ? `background-color: ${backgroundColor}` : '' }`
}
},
created() { created() {
this.setData({ value: this.data.checked }); this.setData({ value: this.data.checked });
}, },

View File

@ -1,6 +1,6 @@
<view <view
class="van-switch custom-class {{ checked ? 'van-switch--on' : '' }} {{ disabled ? 'van-switch--disabled' : '' }}" style="{{ style }}"
style="font-size: {{ size }};" class="{{ classes }}"
bind:tap="onClick" bind:tap="onClick"
> >
<view class="van-switch__node node-class"> <view class="van-switch__node node-class">