feat: select support custom option by option slot

pull/4499/head
tangjinzhou 2021-08-11 15:22:49 +08:00
parent 07f6d9edf0
commit 68c1f45501
5 changed files with 22 additions and 5 deletions

View File

@ -76,7 +76,15 @@ const Select = defineComponent({
props: SelectProps(), props: SelectProps(),
SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE', SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE',
emits: ['change', 'update:value'], emits: ['change', 'update:value'],
slots: ['notFoundContent', 'suffixIcon', 'itemIcon', 'removeIcon', 'clearIcon', 'dropdownRender'], slots: [
'notFoundContent',
'suffixIcon',
'itemIcon',
'removeIcon',
'clearIcon',
'dropdownRender',
'option',
],
setup(props, { attrs, emit, slots, expose }) { setup(props, { attrs, emit, slots, expose }) {
const selectRef = ref(null); const selectRef = ref(null);
@ -194,6 +202,7 @@ const Select = defineComponent({
dropdownClassName={rcSelectRtlDropDownClassName} dropdownClassName={rcSelectRtlDropDownClassName}
onChange={triggerChange} onChange={triggerChange}
dropdownRender={selectProps.dropdownRender || slots.dropdownRender} dropdownRender={selectProps.dropdownRender || slots.dropdownRender}
v-slots={{ option: slots.option }}
> >
{slots.default?.()} {slots.default?.()}
</RcSelect> </RcSelect>

View File

@ -77,6 +77,7 @@ const OptionListProps = {
const OptionList = defineComponent<OptionListProps, { state?: any }>({ const OptionList = defineComponent<OptionListProps, { state?: any }>({
name: 'OptionList', name: 'OptionList',
inheritAttrs: false, inheritAttrs: false,
slots: ['option'],
setup(props) { setup(props) {
const itemPrefixCls = computed(() => `${props.prefixCls}-item`); const itemPrefixCls = computed(() => `${props.prefixCls}-item`);
@ -268,6 +269,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
setActive, setActive,
onSelectValue, onSelectValue,
memoFlattenOptions, memoFlattenOptions,
$slots,
} = this as any; } = this as any;
const { const {
id, id,
@ -281,6 +283,7 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
onScroll, onScroll,
onMouseenter, onMouseenter,
} = this.$props as OptionListProps; } = this.$props as OptionListProps;
const renderOption = $slots.option;
const { activeIndex } = this.state; const { activeIndex } = this.state;
// ========================== Render ========================== // ========================== Render ==========================
if (memoFlattenOptions.length === 0) { if (memoFlattenOptions.length === 0) {
@ -315,12 +318,11 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
onMouseenter={onMouseenter} onMouseenter={onMouseenter}
children={({ group, groupOption, data }, itemIndex) => { children={({ group, groupOption, data }, itemIndex) => {
const { label, key } = data; const { label, key } = data;
// Group // Group
if (group) { if (group) {
return ( return (
<div class={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}> <div class={classNames(itemPrefixCls, `${itemPrefixCls}-group`)}>
{label !== undefined ? label : key} {renderOption ? renderOption(data) : label !== undefined ? label : key}
</div> </div>
); );
} }
@ -387,7 +389,9 @@ const OptionList = defineComponent<OptionListProps, { state?: any }>({
}} }}
style={style} style={style}
> >
<div class={`${optionPrefixCls}-content`}>{content}</div> <div class={`${optionPrefixCls}-content`}>
{renderOption ? renderOption(data) : content}
</div>
{isValidElement(menuItemSelectedIcon) || selected} {isValidElement(menuItemSelectedIcon) || selected}
{iconVisible && ( {iconVisible && (
<TransBtn <TransBtn

View File

@ -84,6 +84,7 @@ const Select = defineComponent<Omit<ExportedSelectProps, 'children'>>({
ref={selectRef} ref={selectRef}
{...(props as any)} {...(props as any)}
{...attrs} {...attrs}
v-slots={slots}
children={slots.default?.() || []} children={slots.default?.() || []}
/> />
); );

View File

@ -340,6 +340,7 @@ export default function generateSelector<
} = config as any; } = config as any;
const Select = defineComponent<SelectProps<OptionsType, ValueType>>({ const Select = defineComponent<SelectProps<OptionsType, ValueType>>({
name: 'Select', name: 'Select',
slots: ['option'],
setup(props: SelectProps<OptionsType, ValueType>) { setup(props: SelectProps<OptionsType, ValueType>) {
const useInternalProps = computed( const useInternalProps = computed(
() => props.internalProps && props.internalProps.mark === INTERNAL_PROPS_MARK, () => props.internalProps && props.internalProps.mark === INTERNAL_PROPS_MARK,
@ -1093,6 +1094,7 @@ export default function generateSelector<
displayValues, displayValues,
activeValue, activeValue,
onSearchSubmit, onSearchSubmit,
$slots: slots,
} = this as any; } = this as any;
const { const {
prefixCls = defaultPrefixCls, prefixCls = defaultPrefixCls,
@ -1204,6 +1206,7 @@ export default function generateSelector<
menuItemSelectedIcon={menuItemSelectedIcon} menuItemSelectedIcon={menuItemSelectedIcon}
virtual={virtual !== false && dropdownMatchSelectWidth !== false} virtual={virtual !== false && dropdownMatchSelectWidth !== false}
onMouseenter={onPopupMouseEnter} onMouseenter={onPopupMouseEnter}
v-slots={{ option: slots.option }}
/> />
); );

2
v2-doc

@ -1 +1 @@
Subproject commit 6b53258cc2b3709e070d340714e992760e660e67 Subproject commit 157cce105e1f0a369658dfb29cc802ebc09d0d93