Skip to content

Commit 53fa277

Browse files
committed
feat: add optionLabel slot
1 parent 5c23d97 commit 53fa277

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

components/auto-complete/index.tsx

+18-15
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ function isSelectOptionOrSelectOptGroup(child: any): boolean {
1414
}
1515

1616
const autoCompleteProps = {
17-
...selectProps(),
17+
...omit(selectProps(), ['loading', 'mode', 'optionLabelProp', 'labelInValue']),
1818
dataSource: PropTypes.array,
1919
dropdownMenuStyle: PropTypes.style,
20-
optionLabelProp: PropTypes.string,
20+
// optionLabelProp: PropTypes.string,
2121
dropdownMatchSelectWidth: { type: [Number, Boolean], default: true },
2222
};
2323

@@ -38,7 +38,7 @@ const AutoComplete = defineComponent({
3838
choiceTransitionName: PropTypes.string.def('zoom'),
3939
autofocus: PropTypes.looseBool,
4040
backfill: PropTypes.looseBool,
41-
optionLabelProp: PropTypes.string.def('children'),
41+
// optionLabelProp: PropTypes.string.def('children'),
4242
filterOption: PropTypes.oneOfType([PropTypes.looseBool, PropTypes.func]).def(false),
4343
defaultActiveFirstOption: PropTypes.looseBool.def(true),
4444
},
@@ -121,19 +121,22 @@ const AutoComplete = defineComponent({
121121
}
122122
}
123123

124-
const selectProps = {
125-
...omit(props, ['dataSource', 'optionLabelProp']),
126-
...attrs,
127-
mode: Select.SECRET_COMBOBOX_MODE_DO_NOT_USE,
128-
// optionLabelProp,
129-
getInputElement,
130-
notFoundContent,
131-
// placeholder: '',
132-
class: cls,
133-
ref: selectRef,
134-
};
124+
const selectProps = omit(
125+
{
126+
...props,
127+
...(attrs as any),
128+
mode: Select.SECRET_COMBOBOX_MODE_DO_NOT_USE,
129+
// optionLabelProp,
130+
getInputElement,
131+
notFoundContent,
132+
// placeholder: '',
133+
class: cls,
134+
ref: selectRef,
135+
},
136+
['dataSource', 'loading'],
137+
);
135138
return (
136-
<Select {...selectProps} v-slots={{ option: slots.option }}>
139+
<Select {...selectProps} v-slots={omit(slots, ['default', 'dataSource', 'options'])}>
137140
{optionChildren}
138141
</Select>
139142
);

components/select/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const Select = defineComponent({
7070
'placeholder',
7171
'tagRender',
7272
'maxTagPlaceholder',
73+
'optionLabel', // donot use, maybe remove it
7374
],
7475
setup(props, { attrs, emit, slots, expose }) {
7576
const selectRef = ref<BaseSelectRef>();
@@ -206,6 +207,7 @@ const Select = defineComponent({
206207
transitionName={transitionName.value}
207208
children={slots.default?.()}
208209
tagRender={props.tagRender || slots.tagRender}
210+
optionLabelRender={slots.optionLabel}
209211
maxTagPlaceholder={props.maxTagPlaceholder || slots.maxTagPlaceholder}
210212
></RcSelect>
211213
);

components/vc-select/BaseSelect.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ export const baseSelectPropsWithoutPrivate = () => {
157157
return {
158158
showSearch: { type: Boolean, default: undefined },
159159
tagRender: { type: Function as PropType<(props: CustomTagProps) => any> },
160+
optionLabelRender: { type: Function as PropType<(option: Record<string, any>) => any> },
160161
direction: { type: String as PropType<'ltr' | 'rtl'> },
161162

162163
// MISC
@@ -664,6 +665,7 @@ export default defineComponent({
664665
// Tags
665666
tokenSeparators,
666667
tagRender,
668+
optionLabelRender,
667669

668670
// Events
669671
onPopupScroll,
@@ -830,6 +832,7 @@ export default defineComponent({
830832
mode={mode}
831833
activeDescendantId={activeDescendantId}
832834
tagRender={tagRender}
835+
optionLabelRender={optionLabelRender}
833836
values={displayValues}
834837
open={mergedOpen.value}
835838
onToggleOpen={onToggleOpen}

components/vc-select/Selector/SingleSelector.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import useInjectLegacySelectContext from '../../vc-tree-select/LegacyContext';
99
interface SelectorProps extends InnerSelectorProps {
1010
inputElement: VueNode;
1111
activeValue: string;
12+
optionLabelRender: Function;
1213
}
1314
const props = {
1415
inputElement: PropTypes.any,
@@ -28,6 +29,7 @@ const props = {
2829
tabindex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
2930
activeValue: PropTypes.string,
3031
backfill: PropTypes.looseBool,
32+
optionLabelRender: PropTypes.func,
3133
onInputChange: PropTypes.func,
3234
onInputPaste: PropTypes.func,
3335
onInputKeyDown: PropTypes.func,
@@ -98,6 +100,7 @@ const SingleSelector = defineComponent<SelectorProps>({
98100
activeDescendantId,
99101
open,
100102
tabindex,
103+
optionLabelRender,
101104
onInputKeyDown,
102105
onInputMouseDown,
103106
onInputChange,
@@ -125,7 +128,7 @@ const SingleSelector = defineComponent<SelectorProps>({
125128
// titleNode = treeSelectContext.value.slots.titleRender(item.option?.data || {});
126129
// }
127130
} else {
128-
titleNode = item?.label;
131+
titleNode = optionLabelRender && item ? optionLabelRender(item.option) : item?.label;
129132
}
130133
return (
131134
<>

components/vc-select/Selector/index.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface SelectorProps {
4646
maxTagTextLength?: number;
4747
maxTagPlaceholder?: VueNode | ((omittedValues: DisplayValueType[]) => VueNode);
4848
tagRender?: (props: CustomTagProps) => VueNode;
49+
optionLabelRender?: (props: Record<string, any>) => VueNode;
4950

5051
/** Check if `tokenSeparators` contains `\n` or `\r\n` */
5152
tokenWithEnter?: boolean;
@@ -100,6 +101,7 @@ const Selector = defineComponent<SelectorProps>({
100101
maxTagTextLength: PropTypes.number,
101102
maxTagPlaceholder: PropTypes.any,
102103
tagRender: PropTypes.func,
104+
optionLabelRender: PropTypes.func,
103105

104106
/** Check if `tokenSeparators` contains `\n` or `\r\n` */
105107
tokenWithEnter: PropTypes.looseBool,

0 commit comments

Comments
 (0)