From 1dd9218a672a0f3b0ee357a17929bcd44d4776a4 Mon Sep 17 00:00:00 2001 From: neverland Date: Wed, 13 Feb 2019 16:49:10 +0800 Subject: [PATCH] [new feature] Switch: add active-value & inactive-value prop (#1297) --- packages/switch/README.md | 2 ++ packages/switch/index.ts | 18 ++++++++++++++---- packages/switch/index.wxml | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/switch/README.md b/packages/switch/README.md index 72ee1e03..875c885b 100644 --- a/packages/switch/README.md +++ b/packages/switch/README.md @@ -98,6 +98,8 @@ Page({ | size | 开关尺寸 | `String` | `30px` | | active-color | 打开时的背景色 | `String` | `#1989fa` | | inactive-color | 关闭时的背景色 | `String` | `#fff` | +| active-value | 打开时的值 | `any` | `true` | +| inactive-value | 关闭时的值 | `any` | `false` | ### Event diff --git a/packages/switch/index.ts b/packages/switch/index.ts index 8bf5b019..f83ba923 100644 --- a/packages/switch/index.ts +++ b/packages/switch/index.ts @@ -6,7 +6,7 @@ VantComponent({ classes: ['node-class'], props: { - checked: Boolean, + checked: null, loading: Boolean, disabled: Boolean, activeColor: String, @@ -14,6 +14,14 @@ VantComponent({ size: { type: String, value: '30px' + }, + activeValue: { + type: null, + value: true + }, + inactiveValue: { + type: null, + value: false } }, @@ -29,10 +37,12 @@ VantComponent({ methods: { onClick() { + const { activeValue, inactiveValue } = this.data; if (!this.data.disabled && !this.data.loading) { - const checked = !this.data.checked; - this.$emit('input', checked); - this.$emit('change', checked); + const checked = this.data.checked === activeValue; + const value = checked ? inactiveValue : activeValue; + this.$emit('input', value); + this.$emit('change', value); } } } diff --git a/packages/switch/index.wxml b/packages/switch/index.wxml index e215473c..74ce520e 100644 --- a/packages/switch/index.wxml +++ b/packages/switch/index.wxml @@ -1,7 +1,7 @@