From 702a9dd8323278619bd217f8afcbf0634b7a20f5 Mon Sep 17 00:00:00 2001 From: chenghx Date: Thu, 23 Aug 2018 15:54:58 +0800 Subject: [PATCH] feat: complete function for ColorCheckBox components --- src/components/check/ColorCheckBox.vue | 58 ++++++++++++++++++++++---- src/components/setting/Setting.vue | 10 +++-- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/components/check/ColorCheckBox.vue b/src/components/check/ColorCheckBox.vue index 9aff731..7a0c05f 100644 --- a/src/components/check/ColorCheckBox.vue +++ b/src/components/check/ColorCheckBox.vue @@ -14,11 +14,28 @@ const Group = { type: Array, required: false, default: () => [] + }, + multiple: { + type: Boolean, + required: false, + default: false } }, data () { return { - values: this.defaultValues + values: [], + options: [] + } + }, + computed: { + colors () { + let colors = [] + this.options.forEach(item => { + if (item.sChecked) { + colors.push(item.color) + } + }) + return colors } }, provide () { @@ -26,15 +43,30 @@ const Group = { groupContext: this } }, + watch: { + 'values': function (newVal, oldVal) { + // 此条件是为解决单选时,触发两次chang事件问题 + if (!(newVal.length === 1 && oldVal.length === 1 && newVal[0] === oldVal[0])) { + this.$emit('change', this.values, this.colors) + } + } + }, methods: { - handleChange (value) { - if (!value.checked) { - const values = this.values.filter(item => item.value !== value.value) - this.values = values + handleChange (option) { + if (!option.checked) { + this.values = this.values.filter(item => item !== option.value) } else { - this.values.push(value) + if (!this.multiple) { + this.values = [option.value] + this.options.forEach(item => { + if (item.value !== option.value) { + item.sChecked = false + } + }) + } else { + this.values.push(option.value) + } } - this.$emit('change', this.values) } }, render (h) { @@ -80,7 +112,17 @@ export default { checked: this.sChecked } this.$emit('change', value) - this.groupContext.handleChange(value) + const groupContext = this.groupContext + if (groupContext) { + groupContext.handleChange(value) + } + } + }, + created () { + const groupContext = this.groupContext + if (groupContext) { + this.sChecked = groupContext.defaultValues.indexOf(this.value) >= 0 + groupContext.options.push(this) } }, methods: { diff --git a/src/components/setting/Setting.vue b/src/components/setting/Setting.vue index 1f5d170..6e2efdb 100644 --- a/src/components/setting/Setting.vue +++ b/src/components/setting/Setting.vue @@ -7,8 +7,8 @@ - - + + @@ -97,8 +97,10 @@ export default { AIcon, ALayoutSider}, methods: { - onColorChange (values) { - console.log(values) + onColorChange (values, colors) { + if (colors.length > 0) { + this.$message.info(`选择了主题色 ${colors}`) + } } } }