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): 增加option cache,避免搜索过滤时optionsList为空导致选中选项显示异常 #3410

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/select/hooks/useSelectOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function useSelectOptions(
) {
// 内部 options 记录
const options = ref<UniOption[]>([]);
const optionsCache = ref<UniOption[]>([]);

// 指向当前 slots 数组,用来判断 slot 是否被更新
let innerSlotRecord: VNode[] = null;
Expand Down Expand Up @@ -113,7 +114,7 @@ export default function useSelectOptions(

const optionsMap = computed(() => {
const res = new Map<SelectValue, TdOptionProps>();
optionsList.value.forEach((option: TdOptionProps) => {
optionsCache.value.concat(optionsList.value).forEach((option: TdOptionProps) => {
res.set(option.value, option);
});
return res;
Expand Down Expand Up @@ -154,5 +155,6 @@ export default function useSelectOptions(
options,
optionsMap,
optionsList,
optionsCache,
};
}
30 changes: 26 additions & 4 deletions src/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export default defineComponent({
value: props.keys?.value || 'value',
disabled: props.keys?.disabled || 'disabled',
}));
const { options: innerOptions, optionsMap, optionsList } = useSelectOptions(props, instance, keys);
const {
options: innerOptions, optionsMap, optionsList, optionsCache,
} = useSelectOptions(props, instance, keys);

const [value, setValue] = useVModel(valueProps, props.defaultValue, props.onChange, 'change');
const innerValue = computed(() => {
Expand Down Expand Up @@ -164,6 +166,23 @@ export default defineComponent({
[labelOfKeys]: get(option, labelOfKeys),
};
};
const addCache = (val: SelectValue) => {
if (multiple.value) {
const newCache: TdOptionProps[] = [];
(val as SelectValue[]).forEach((item) => {
const option = optionsMap.value.get(item);
if (option) {
newCache.push(option);
}
});
optionsCache.value = Array.from(new Set([...newCache, ...optionsCache.value]));
} else {
const option = optionsMap.value.get(val);
if (option) {
optionsCache.value = Array.from(new Set([option, ...optionsCache.value]));
}
}
};
const getSelectedOption = (newVal: SelectValue) => {
let selectedOption: SelectValue = getOriginOptions(newVal);
if (!selectedOption && optionValue) {
Expand Down Expand Up @@ -194,6 +213,9 @@ export default defineComponent({
// eslint-disable-next-line dot-notation
outputContext['option'] = outputContextOption;
}
nextTick(() => {
addCache(newVal);
});
setValue(newVal, outputContext);
};

Expand Down Expand Up @@ -232,7 +254,7 @@ export default defineComponent({
() => ((!multiple.value
&& innerPopupVisible.value
&& ((valueType.value === 'object' && (value.value?.[keys.value.label] || innerValue.value))
|| getSingleContent(innerValue.value, optionsList.value)))
|| getSingleContent(innerValue.value, optionsMap.value)))
|| placeholder.value)
?? t(global.value.placeholder),
);
Expand All @@ -242,12 +264,12 @@ export default defineComponent({
if (valueType.value === 'object') {
return (value.value as SelectValue[]).map((v) => v[keys.value.label]);
}
return getMultipleContent(innerValue.value as SelectValue[], optionsList.value);
return getMultipleContent(innerValue.value as SelectValue[], optionsMap.value);
}
if (valueType.value === 'object') {
return value.value?.[keys.value.label] || innerValue.value;
}
return getSingleContent(innerValue.value, optionsList.value);
return getSingleContent(innerValue.value, optionsMap.value);
});

const valueDisplayParams = computed(() => {
Expand Down
18 changes: 8 additions & 10 deletions src/select/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import {
SelectOption, SelectOptionGroup, SelectValue, TdOptionProps, TdSelectProps,
} from './type';

export const getSingleContent = (value: TdSelectProps['value'], options: SelectOption[]): string => {
for (const option of options) {
if ((option as TdOptionProps).value === value) {
// 保底使用 value 作为显示
return option?.label || String((option as TdOptionProps).value);
}
}
return value !== undefined && value !== null ? String(value) : undefined;
export const getSingleContent = (
value: TdSelectProps['value'],
optionsMap: Map<SelectValue<SelectOption>, TdOptionProps>,
): string => {
const option = optionsMap.get(value);
return option?.label || value?.toString();
};

export const getMultipleContent = (value: SelectValue[], options: SelectOption[]) => {
export const getMultipleContent = (value: SelectValue[], optionsMap: Map<SelectValue<SelectOption>, TdOptionProps>) => {
const res: string[] = [];
for (const iterator of value) {
const resLabel = getSingleContent(iterator, options);
const resLabel = getSingleContent(iterator, optionsMap);
if (resLabel) {
res.push(resLabel);
}
Expand Down