Skip to content

Commit ec17787

Browse files
committed
fix: select option tootip error, close #5307
1 parent 669b22a commit ec17787

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

components/select/index.en-US.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Select component to select value from options.
4242
| filterSort | Sort function for search options sorting, see [Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)'s compareFunction | (optionA: Option, optionB: Option) => number | - | 3.0 |
4343
| firstActiveValue | Value of action option by default | string\|string\[] | - | |
4444
| getPopupContainer | Parent Node which the selector should be rendered to. Default to `body`. When position issues happen, try to modify it into scrollable content and position it relative. | function(triggerNode) | () => document.body | |
45-
| labelInValue | whether to embed label in value, turn the format of value from `string` to `{key: string, label: vNodes}` | boolean | false | |
45+
| labelInValue | whether to embed label in value, turn the format of value from `string` to `{key: string, label: vNodes, originLabel: any}`, originLabel (3.1) maintains the original type. If the node is constructed through a-select-option children, the value is a function (the default slot of a-select-option) | boolean | false | |
4646
| listHeight | Config popup height | number | 256 | |
4747
| loading | indicate loading state | Boolean | false | |
4848
| maxTagCount | Max tag count to show | number | - | |

components/select/index.zh-CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cover: https://gw.alipayobjects.com/zos/alicdn/_0XzgOis7/Select.svg
4343
| filterSort | 搜索时对筛选结果项的排序函数, 类似[Array.sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)里的 compareFunction | (optionA: Option, optionB: Option) => number | - | 3.0 |
4444
| firstActiveValue | 默认高亮的选项 | string\|string\[] | - | |
4545
| getPopupContainer | 菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。 | Function(triggerNode) | () => document.body | |
46-
| labelInValue | 是否把每个选项的 label 包装到 value 中,会把 Select 的 value 类型从 `string` 变为 `{key: string, label: vNodes}` 的格式 | boolean | false | |
46+
| labelInValue | 是否把每个选项的 label 包装到 value 中,会把 Select 的 value 类型从 `string` 变为 `{key: string, label: vNodes, originLabel: any}` 的格式, originLabel(3.1) 保持原始类型,如果通过 a-select-option children 构造的节点,该值是是个函数(即 a-select-option 的默认插槽) | boolean | false | |
4747
| listHeight | 设置弹窗滚动高度 | number | 256 | |
4848
| maxTagCount | 最多显示多少个 tag | number | - | |
4949
| maxTagPlaceholder | 隐藏 tag 时显示的内容 | slot/function(omittedValues) | - | |

components/vc-select/OptionList.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ const OptionList = defineComponent({
136136
baseProps.toggleOpen(false);
137137
}
138138
};
139-
const getLabel = (item: Record<string, any>) => item.label;
139+
const getLabel = (item: Record<string, any>) =>
140+
typeof item.label === 'function' ? item.label() : item.label;
140141
function renderItem(index: number) {
141142
const item = memoFlattenOptions.value[index];
142143
if (!item) return null;
@@ -275,8 +276,9 @@ const OptionList = defineComponent({
275276
virtual={virtual}
276277
v-slots={{
277278
default: (item, itemIndex) => {
278-
const { group, groupOption, data, label, value } = item;
279+
const { group, groupOption, data, value } = item;
279280
const { key } = data;
281+
const label = typeof item.label === 'function' ? item.label() : item.label;
280282
// Group
281283
if (group) {
282284
return (

components/vc-select/Select.tsx

+14-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type OnInternalSelect = (value: RawValueType, info: { selected: boolean }
6464
export type RawValueType = string | number;
6565
export interface LabelInValueType {
6666
label: any;
67+
originLabel?: any;
6768
value: RawValueType;
6869
/** @deprecated `key` is useless since it should always same as `value` */
6970
key?: Key;
@@ -283,7 +284,7 @@ export default defineComponent({
283284

284285
return mergedValues.value.map(item => ({
285286
...item,
286-
label: item.label ?? item.value,
287+
label: (typeof item.label === 'function' ? item.label() : item.label) ?? item.value,
287288
}));
288289
});
289290

@@ -391,7 +392,15 @@ export default defineComponent({
391392
(labeledValues.length !== mergedValues.value.length ||
392393
labeledValues.some((newVal, index) => mergedValues.value[index]?.value !== newVal?.value))
393394
) {
394-
const returnValues = props.labelInValue ? labeledValues : labeledValues.map(v => v.value);
395+
const returnValues = props.labelInValue
396+
? labeledValues.map(v => {
397+
return {
398+
...v,
399+
originLabel: v.label,
400+
label: typeof v.label === 'function' ? v.label() : v.label,
401+
};
402+
})
403+
: labeledValues.map(v => v.value);
395404
const returnOptions = labeledValues.map(v =>
396405
injectPropsWithOption(getMixedOption(v.value)),
397406
);
@@ -426,10 +435,12 @@ export default defineComponent({
426435
const triggerSelect = (val: RawValueType, selected: boolean) => {
427436
const getSelectEnt = (): [RawValueType | LabelInValueType, DefaultOptionType] => {
428437
const option = getMixedOption(val);
438+
const originLabel = option?.[mergedFieldNames.value.label];
429439
return [
430440
props.labelInValue
431441
? {
432-
label: option?.[mergedFieldNames.value.label],
442+
label: typeof originLabel === 'function' ? originLabel() : originLabel,
443+
originLabel,
433444
value: val,
434445
key: option.key ?? val,
435446
}

components/vc-select/utils/legacyUtil.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function convertNodeToOption<OptionType extends BaseOptionType = DefaultOptionTy
1414
children: { default?: () => any };
1515
key: string | number;
1616
};
17-
const child = children && children.default ? children.default() : undefined;
17+
const child = children.default;
1818
return {
1919
key,
2020
value: value !== undefined ? value : key,

0 commit comments

Comments
 (0)