mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
[new feature] Switch: add active-value & inactive-value prop (#1297)
This commit is contained in:
parent
7b9432a51c
commit
1dd9218a67
@ -98,6 +98,8 @@ Page({
|
|||||||
| size | 开关尺寸 | `String` | `30px` |
|
| size | 开关尺寸 | `String` | `30px` |
|
||||||
| active-color | 打开时的背景色 | `String` | `#1989fa` |
|
| active-color | 打开时的背景色 | `String` | `#1989fa` |
|
||||||
| inactive-color | 关闭时的背景色 | `String` | `#fff` |
|
| inactive-color | 关闭时的背景色 | `String` | `#fff` |
|
||||||
|
| active-value | 打开时的值 | `any` | `true` |
|
||||||
|
| inactive-value | 关闭时的值 | `any` | `false` |
|
||||||
|
|
||||||
### Event
|
### Event
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ VantComponent({
|
|||||||
classes: ['node-class'],
|
classes: ['node-class'],
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
checked: Boolean,
|
checked: null,
|
||||||
loading: Boolean,
|
loading: Boolean,
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
activeColor: String,
|
activeColor: String,
|
||||||
@ -14,6 +14,14 @@ VantComponent({
|
|||||||
size: {
|
size: {
|
||||||
type: String,
|
type: String,
|
||||||
value: '30px'
|
value: '30px'
|
||||||
|
},
|
||||||
|
activeValue: {
|
||||||
|
type: null,
|
||||||
|
value: true
|
||||||
|
},
|
||||||
|
inactiveValue: {
|
||||||
|
type: null,
|
||||||
|
value: false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -29,10 +37,12 @@ VantComponent({
|
|||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onClick() {
|
onClick() {
|
||||||
|
const { activeValue, inactiveValue } = this.data;
|
||||||
if (!this.data.disabled && !this.data.loading) {
|
if (!this.data.disabled && !this.data.loading) {
|
||||||
const checked = !this.data.checked;
|
const checked = this.data.checked === activeValue;
|
||||||
this.$emit('input', checked);
|
const value = checked ? inactiveValue : activeValue;
|
||||||
this.$emit('change', checked);
|
this.$emit('input', value);
|
||||||
|
this.$emit('change', value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<wxs src="../wxs/utils.wxs" module="utils" />
|
<wxs src="../wxs/utils.wxs" module="utils" />
|
||||||
|
|
||||||
<view
|
<view
|
||||||
class="custom-class {{ utils.bem('switch', { on: checked, disabled }) }}"
|
class="custom-class {{ utils.bem('switch', { on: value === activeValue, disabled }) }}"
|
||||||
style="font-size: {{ size }}; {{ (checked ? activeColor : inactiveColor) ? 'background-color: ' + (checked ? activeColor : inactiveColor ) : '' }}"
|
style="font-size: {{ size }}; {{ (checked ? activeColor : inactiveColor) ? 'background-color: ' + (checked ? activeColor : inactiveColor ) : '' }}"
|
||||||
bind:tap="onClick"
|
bind:tap="onClick"
|
||||||
>
|
>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user