From 9dc806590ba68eccc2c0996b2b7bda1a1840f3a4 Mon Sep 17 00:00:00 2001 From: Uyarn Date: Thu, 13 Apr 2023 14:40:14 +0800 Subject: [PATCH] fix(Select): context missing when change from a created option --- src/select/select.tsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/select/select.tsx b/src/select/select.tsx index 3388c9359..a53719a5a 100644 --- a/src/select/select.tsx +++ b/src/select/select.tsx @@ -132,6 +132,7 @@ export default defineComponent({ // value 类型的 value 变量,直接返回 return _value; }); + const setInnerValue = ( newVal: SelectValue, context: { trigger: SelectValueChangeTrigger; e?: MouseEvent | KeyboardEvent }, @@ -162,12 +163,20 @@ export default defineComponent({ [labelOfKeys]: get(option, labelOfKeys), }; }; + const getSelectedOption = (newVal: SelectValue) => { + let selectedOption: SelectValue = getOriginOptions(newVal); + if (!selectedOption && optionValue) { + // 处理create场景 + selectedOption = { label: optionValue, value: optionValue }; + } + return selectedOption; + }; // 构造 selectOptions if (multiple.value) { - (newVal as SelectValue[]).forEach((v) => selectedOptions.push(getOriginOptions(v))); + (newVal as SelectValue[]).forEach((v) => selectedOptions.push(getSelectedOption(v))); } else { - selectedOptions.push(getOriginOptions(newVal)); + selectedOptions.push(getSelectedOption(newVal)); } // 当 value 为 object 类型时,通过 innerValue 寻找对应的 object if (valueType.value === 'object') { @@ -179,8 +188,10 @@ export default defineComponent({ const outputContext = { ...context, selectedOptions }; if (optionValue) { + const outputContextOption: SelectValue = getSelectedOption(optionValue); + // eslint-disable-next-line dot-notation - outputContext['option'] = getOriginOptions(optionValue); + outputContext['option'] = outputContextOption; } setValue(newVal, outputContext); }; @@ -479,6 +490,7 @@ export default defineComponent({ handleCheckAllClick(e); } else { const optionValue = (displayOptions[hoverIndex.value] as TdOptionProps)?.value; + if (!optionValue) return; if (!multiple.value) { setInnerValue(optionValue, { e, trigger: 'check' }, optionValue);