Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Select): context missing when change from a created option #2333

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export default defineComponent({
// value 类型的 value 变量,直接返回
return _value;
});

const setInnerValue = (
newVal: SelectValue,
context: { trigger: SelectValueChangeTrigger; e?: MouseEvent | KeyboardEvent },
Expand Down Expand Up @@ -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') {
Expand All @@ -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);
};
Expand Down Expand Up @@ -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);
Expand Down