From b953cb71cc4ee6123a441467d9bd5047f08ba292 Mon Sep 17 00:00:00 2001 From: FEI <1556610925@qq.com> Date: Wed, 1 Mar 2023 17:07:23 +0800 Subject: [PATCH] bug fix --- src/components/select/select.vue | 58 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/components/select/select.vue b/src/components/select/select.vue index a30d6f0..f70e060 100644 --- a/src/components/select/select.vue +++ b/src/components/select/select.vue @@ -267,22 +267,6 @@ this.checkUpdateStatus(); - // remote search, set instanceof - if (this.remote && this.value && this.initialLabel) { - if (!this.multiple) { - this.query = this.initialLabel; - } else if (this.multiple && (this.initialLabel instanceof Array) && this.value.length === this.initialLabel.length) { - const values = this.value.map((item, index) => { - return { - value: item, - label: this.initialLabel[index] - }; - }); - setTimeout(() => { - this.values = values; - }); - } - } }, data () { @@ -364,11 +348,13 @@ return selectOptions && selectOptions.length === 0 && (!remote || (remote && !loading)); }, publicValue(){ - if (this.labelInValue){ - return this.multiple ? this.values : this.values[0]; - } else { + // 改变 labelInValue 实现,解决 bug:Select,label-in-value时,搜索、多选,先选一个,再选第二个,会替代第一个 + // if (this.labelInValue){ + // return this.multiple ? this.values : this.values[0]; + // } else { + // return this.multiple ? this.values.map(option => option.value) : (this.values[0] || {}).value; + // } return this.multiple ? this.values.map(option => option.value) : (this.values[0] || {}).value; - } }, canBeCleared(){ const uiStateMatch = this.hasMouseHoverHead || this.active; @@ -459,6 +445,8 @@ } }, clearSingleSelect(){ // PUBLIC API + // fix #446 + if (!this.multiple) this.$emit('input', ''); this.$emit('on-clear'); this.hideMenu(); if (this.clearable) this.reset(); @@ -467,9 +455,12 @@ const option = this.flatOptions.find(({componentOptions}) => componentOptions.propsData.value === value); if (!option) return null; const label = getOptionLabel(option); + // 修复多选时,选项有disabled属性,选中项仍然能删除的 bug + const disabled = option.componentOptions.propsData.disabled; return { value: value, label: label, + disabled: disabled }; }, getInitialValue(){ @@ -725,21 +716,36 @@ if (value === '') this.values = []; else if (checkValuesNotEqual(value,publicValue,values)) { this.$nextTick(() => this.values = getInitialValue().map(getOptionData).filter(Boolean)); - this.dispatch('FormItem', 'on-form-change', this.publicValue); + if (!this.multiple) this.dispatch('FormItem', 'on-form-change', this.publicValue); } }, values(now, before){ const newValue = JSON.stringify(now); const oldValue = JSON.stringify(before); // v-model is always just the value, event with labelInValue === true - const vModelValue = (this.publicValue && this.labelInValue) ? - (this.multiple ? this.publicValue.map(({value}) => value) : this.publicValue.value) : - this.publicValue; + // const vModelValue = (this.publicValue && this.labelInValue === false) ? + // (this.multiple ? this.publicValue.map(({value}) => value) : this.publicValue.value) : + // this.publicValue; + // 改变 labelInValue 的实现:直接在 emit 时改数据 + let vModelValue = this.publicValue; const shouldEmitInput = newValue !== oldValue && vModelValue !== this.value; if (shouldEmitInput) { + let emitValue = this.publicValue; + if (this.labelInValue) { + if (this.multiple) { + emitValue = this.values; + } else { + emitValue = this.values[0]; + } + } + + // Form 重置时,如果初始值是 null,也置为 null,而不是 [] + if (Array.isArray(vModelValue) && !vModelValue.length && this.value === null) vModelValue = null; + else if (vModelValue === undefined && this.value === null) vModelValue = null; + this.$emit('input', vModelValue); // to update v-model - this.$emit('on-change', this.publicValue); - this.dispatch('FormItem', 'on-form-change', this.publicValue); + this.$emit('on-change', emitValue); + this.dispatch('FormItem', 'on-form-change', emitValue); } }, query (query) { -- libgit2 0.21.4