Skip to content

Commit 68c1f45

Browse files
committed
feat: select support custom option by option slot
1 parent 07f6d9e commit 68c1f45

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

components/select/index.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ const Select = defineComponent({
7676
props: SelectProps(),
7777
SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE',
7878
emits: ['change', 'update:value'],
79-
slots: ['notFoundContent', 'suffixIcon', 'itemIcon', 'removeIcon', 'clearIcon', 'dropdownRender'],
79+
slots: [
80+
'notFoundContent',
81+
'suffixIcon',
82+
'itemIcon',
83+
'removeIcon',
84+
'clearIcon',
85+
'dropdownRender',
86+
'option',
87+
],
8088
setup(props, { attrs, emit, slots, expose }) {
8189
const selectRef = ref(null);
8290

@@ -194,6 +202,7 @@ const Select = defineComponent({
194202
dropdownClassName={rcSelectRtlDropDownClassName}
195203
onChange={triggerChange}
196204
dropdownRender={selectProps.dropdownRender || slots.dropdownRender}
205+
v-slots={{ option: slots.option }}
197206
>
198207
{slots.default?.()}
199208
</RcSelect>

components/vc-select/OptionList.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const OptionListProps = {
7777
const OptionList = defineComponent<OptionListProps, { state?: any }>({
7878
name: 'OptionList',
7979
inheritAttrs: false,
80+
slots: ['option'],
8081
setup(props) {
8182
const itemPrefixCls = computed(() => `${props.prefixCls}-item`);
8283

@@ -268,6 +269,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
268269
setActive,
269270
onSelectValue,
270271
memoFlattenOptions,
272+
$slots,
271273
} = this as any;
272274
const {
273275
id,
@@ -281,6 +283,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
281283
onScroll,
282284
onMouseenter,
283285
} = this.$props as OptionListProps;
286+
const renderOption = $slots.option;
284287
const { activeIndex } = this.state;
285288
// ========================== Render ==========================
286289
if (memoFlattenOptions.length === 0) {
@@ -315,12 +318,11 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
315318
onMouseenter={onMouseenter}
316319
children={({ group, groupOption, data }, itemIndex) => {
317320
const { label, key } = data;
318-
319321
// Group
320322
if (group) {
321323
return (
322324
<div class={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}>
323-
{label !== undefined ? label : key}
325+
{renderOption ? renderOption(data) : label !== undefined ? label : key}
324326
</div>
325327
);
326328
}
@@ -387,7 +389,9 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
387389
}}
388390
style={style}
389391
>
390-
<div class={`${optionPrefixCls}-content`}>{content}</div>
392+
<div class={`${optionPrefixCls}-content`}>
393+
{renderOption ? renderOption(data) : content}
394+
</div>
391395
{isValidElement(menuItemSelectedIcon) || selected}
392396
{iconVisible && (
393397
<TransBtn

components/vc-select/Select.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const Select = defineComponent<Omit<ExportedSelectProps, 'children'>>({
8484
ref={selectRef}
8585
{...(props as any)}
8686
{...attrs}
87+
v-slots={slots}
8788
children={slots.default?.() || []}
8889
/>
8990
);

components/vc-select/generate.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ export default function generateSelector<
340340
} = config as any;
341341
const Select = defineComponent<SelectProps<OptionsType, ValueType>>({
342342
name: 'Select',
343+
slots: ['option'],
343344
setup(props: SelectProps<OptionsType, ValueType>) {
344345
const useInternalProps = computed(
345346
() => props.internalProps && props.internalProps.mark === INTERNAL_PROPS_MARK,
@@ -1093,6 +1094,7 @@ export default function generateSelector<
10931094
displayValues,
10941095
activeValue,
10951096
onSearchSubmit,
1097+
$slots: slots,
10961098
} = this as any;
10971099
const {
10981100
prefixCls = defaultPrefixCls,
@@ -1204,6 +1206,7 @@ export default function generateSelector<
12041206
menuItemSelectedIcon={menuItemSelectedIcon}
12051207
virtual={virtual !== false && dropdownMatchSelectWidth !== false}
12061208
onMouseenter={onPopupMouseEnter}
1209+
v-slots={{ option: slots.option }}
12071210
/>
12081211
);
12091212

v2-doc

Submodule v2-doc updated 65 files

0 commit comments

Comments
 (0)