From ca9379de0d9382f1a9ff0ab0948e455a4e5a1d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Fri, 1 Sep 2017 13:33:12 +0800 Subject: [PATCH] Checkbox: support change event --- docs/examples-docs/checkbox.md | 3 +-- packages/checkbox/index.vue | 21 ++++++++------------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/docs/examples-docs/checkbox.md b/docs/examples-docs/checkbox.md index f470ef3be..ef23ecd81 100644 --- a/docs/examples-docs/checkbox.md +++ b/docs/examples-docs/checkbox.md @@ -45,8 +45,7 @@ Vue.component(Checkbox.name, Checkbox); ### 代码演示 #### 基础用法 - -通过`v-model`绑定值即可。当`Checkbox`选中时,绑定的值即为`true`,否则为`false`。当单个`Checkbox`使用时,更建议使用`Switch`组件。 +通过`v-model`绑定 checkbox 的勾选状态 :::demo 基础用法 ```html diff --git a/packages/checkbox/index.vue b/packages/checkbox/index.vue index 1b31f0e1e..6ff5d91b2 100644 --- a/packages/checkbox/index.vue +++ b/packages/checkbox/index.vue @@ -31,22 +31,23 @@ export default { mixins: [findParent], props: { - disabled: Boolean, value: {}, + disabled: Boolean, name: [String, Number] }, + watch: { + value(val) { + this.$emit('change', val); + } + }, + computed: { - /** - * `checkbox`是否在`van-checkbox-group`中 - */ + // checkbox 是否在 van-checkbox-group 中 isGroup() { return !!this.findParentByComponentName('van-checkbox-group'); }, - /** - * `checkbox`当前值 - */ currentValue: { get() { return this.isGroup && this.parentGroup ? this.parentGroup.value.indexOf(this.name) !== -1 : this.value; @@ -75,9 +76,6 @@ export default { } }, - /** - * `checkbox`是否被选中 - */ isChecked() { const currentValue = this.currentValue; if ({}.toString.call(currentValue) === '[object Boolean]') { @@ -87,9 +85,6 @@ export default { } }, - /** - * `checkbox`是否被禁用 - */ isDisabled() { return this.isGroup && this.parentGroup ? this.parentGroup.disabled