From ee3265a1516242713970fb35794a881e4b6b8688 Mon Sep 17 00:00:00 2001 From: iczer <1126263215@qq.com> Date: Thu, 9 Jul 2020 10:58:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20problem=20of=20img-check-box=20component?= =?UTF-8?q?=20in=20radio=20mode;=20:bug:=20=E4=BF=AE=E5=A4=8D=EF=BC=9Aimg-?= =?UTF-8?q?check-box=20=E7=BB=84=E4=BB=B6=E5=9C=A8=E5=8D=95=E9=80=89?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/checkbox/ImgCheckbox.vue | 33 ++++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/components/checkbox/ImgCheckbox.vue b/src/components/checkbox/ImgCheckbox.vue index 014c21a..b59dd22 100644 --- a/src/components/checkbox/ImgCheckbox.vue +++ b/src/components/checkbox/ImgCheckbox.vue @@ -36,22 +36,25 @@ const Group = { } }, watch: { - 'values': function (newVal, oldVal) { - // 此条件是为解决单选时,触发两次chang事件问题 - if (!(newVal.length === 1 && oldVal.length === 1 && newVal[0] === oldVal[0])) { - this.$emit('change', this.values) - } + 'values': function (value) { + this.$emit('change', value) + // // 此条件是为解决单选时,触发两次chang事件问题 + // if (!(newVal.length === 1 && oldVal.length === 1 && newVal[0] === oldVal[0])) { + // this.$emit('change', this.values) + // } } }, methods: { handleChange (option) { if (!option.checked) { - this.values = this.values.filter(item => item !== option.value) + if (this.values.indexOf(option.value) > -1) { + this.values = this.values.filter(item => item != option.value) + } } else { if (!this.multiple) { this.values = [option.value] this.options.forEach(item => { - if (item.value !== option.value) { + if (item.value != option.value) { item.sChecked = false } }) @@ -92,7 +95,7 @@ export default { }, data () { return { - sChecked: this.checked + sChecked: this.initChecked() } }, inject: ['groupContext'], @@ -118,7 +121,19 @@ export default { }, methods: { toggle () { - this.sChecked = !this.sChecked + if (this.groupContext.multiple || !this.sChecked) { + this.sChecked = !this.sChecked + } + }, + initChecked() { + let groupContext = this.groupContext + if (!groupContext) { + return this.checked + }else if (groupContext.multiple) { + return groupContext.defaultValues.indexOf(this.value) > -1 + } else { + return groupContext.defaultValues[0] == this.value + } } } }