2021-09-25 08:51:32 +00:00
|
|
|
import type { App, PropType, Plugin, ExtractPropTypes } from 'vue';
|
2021-06-26 01:35:40 +00:00
|
|
|
import { computed, defineComponent, ref } from 'vue';
|
2020-10-16 10:47:28 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2022-01-21 13:58:10 +00:00
|
|
|
import type { BaseSelectRef } from '../vc-select';
|
2021-09-25 08:51:32 +00:00
|
|
|
import RcSelect, { selectProps as vcSelectProps, Option, OptGroup } from '../vc-select';
|
2022-01-21 13:58:10 +00:00
|
|
|
import type { BaseOptionType, DefaultOptionType } from '../vc-select/Select';
|
|
|
|
import type { OptionProps } from '../vc-select/Option';
|
2020-10-16 10:47:28 +00:00
|
|
|
import getIcons from './utils/iconUtil';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-06-10 13:39:50 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2021-09-25 08:51:32 +00:00
|
|
|
import omit from '../_util/omit';
|
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
2021-12-17 08:59:50 +00:00
|
|
|
import { getTransitionName } from '../_util/transition';
|
2022-01-21 13:58:10 +00:00
|
|
|
import type { SizeType } from '../config-provider';
|
|
|
|
import { initDefaultProps } from '../_util/props-util';
|
2020-10-16 10:47:28 +00:00
|
|
|
|
|
|
|
type RawValue = string | number;
|
|
|
|
|
|
|
|
export type OptionType = typeof Option;
|
2022-01-21 13:58:10 +00:00
|
|
|
export type { OptionProps, BaseSelectRef as RefSelectProps, BaseOptionType, DefaultOptionType };
|
2020-10-16 10:47:28 +00:00
|
|
|
|
|
|
|
export interface LabeledValue {
|
|
|
|
key?: string;
|
|
|
|
value: RawValue;
|
2022-01-11 15:23:07 +00:00
|
|
|
label?: any;
|
2020-10-16 10:47:28 +00:00
|
|
|
}
|
2021-06-22 02:47:33 +00:00
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[] | undefined;
|
2020-10-16 10:47:28 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
export const selectProps = () => ({
|
2022-03-12 01:56:32 +00:00
|
|
|
...omit(vcSelectProps<SelectValue>(), [
|
|
|
|
'inputIcon',
|
|
|
|
'mode',
|
|
|
|
'getInputElement',
|
|
|
|
'getRawInputElement',
|
|
|
|
'backfill',
|
|
|
|
]),
|
2020-10-16 10:47:28 +00:00
|
|
|
value: {
|
2020-10-17 14:19:52 +00:00
|
|
|
type: [Array, Object, String, Number] as PropType<SelectValue>,
|
2020-10-16 10:47:28 +00:00
|
|
|
},
|
|
|
|
defaultValue: {
|
2020-10-17 14:19:52 +00:00
|
|
|
type: [Array, Object, String, Number] as PropType<SelectValue>,
|
2020-10-16 10:47:28 +00:00
|
|
|
},
|
2021-09-25 08:51:32 +00:00
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
suffixIcon: PropTypes.any,
|
|
|
|
itemIcon: PropTypes.any,
|
2022-01-21 13:58:10 +00:00
|
|
|
size: String as PropType<SizeType>,
|
|
|
|
mode: String as PropType<'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE'>,
|
|
|
|
bordered: { type: Boolean, default: true },
|
|
|
|
transitionName: String,
|
|
|
|
choiceTransitionName: { type: String, default: '' },
|
|
|
|
'onUpdate:value': Function as PropType<(val: SelectValue) => void>,
|
2020-10-17 14:19:52 +00:00
|
|
|
});
|
2018-02-23 08:14:59 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
export type SelectProps = Partial<ExtractPropTypes<ReturnType<typeof selectProps>>>;
|
|
|
|
|
2022-01-21 13:58:10 +00:00
|
|
|
const SECRET_COMBOBOX_MODE_DO_NOT_USE = 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
2020-10-12 10:46:05 +00:00
|
|
|
const Select = defineComponent({
|
2022-09-26 13:33:41 +00:00
|
|
|
compatConfig: { MODE: 3 },
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ASelect',
|
2020-10-16 10:47:28 +00:00
|
|
|
Option,
|
|
|
|
OptGroup,
|
|
|
|
inheritAttrs: false,
|
2022-01-21 13:58:10 +00:00
|
|
|
props: initDefaultProps(selectProps(), {
|
|
|
|
listHeight: 256,
|
2022-03-19 06:38:11 +00:00
|
|
|
listItemHeight: 24,
|
2022-01-21 13:58:10 +00:00
|
|
|
}),
|
|
|
|
SECRET_COMBOBOX_MODE_DO_NOT_USE,
|
|
|
|
// emits: ['change', 'update:value', 'blur'],
|
2021-08-11 07:22:49 +00:00
|
|
|
slots: [
|
|
|
|
'notFoundContent',
|
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'dropdownRender',
|
|
|
|
'option',
|
2021-12-07 14:23:36 +00:00
|
|
|
'placeholder',
|
2022-01-21 13:58:10 +00:00
|
|
|
'tagRender',
|
|
|
|
'maxTagPlaceholder',
|
2022-01-27 07:12:15 +00:00
|
|
|
'optionLabel', // donot use, maybe remove it
|
2021-08-11 07:22:49 +00:00
|
|
|
],
|
2021-06-10 13:39:50 +00:00
|
|
|
setup(props, { attrs, emit, slots, expose }) {
|
2022-01-21 13:58:10 +00:00
|
|
|
const selectRef = ref<BaseSelectRef>();
|
2021-09-25 08:51:32 +00:00
|
|
|
const formItemContext = useInjectFormItemContext();
|
2020-10-16 10:47:28 +00:00
|
|
|
const focus = () => {
|
2022-01-21 13:58:10 +00:00
|
|
|
selectRef.value?.focus();
|
2020-10-16 10:47:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const blur = () => {
|
2022-01-21 13:58:10 +00:00
|
|
|
selectRef.value?.blur();
|
|
|
|
};
|
|
|
|
|
|
|
|
const scrollTo: BaseSelectRef['scrollTo'] = arg => {
|
|
|
|
selectRef.value?.scrollTo(arg);
|
2020-10-16 10:47:28 +00:00
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-10-17 14:19:52 +00:00
|
|
|
const mode = computed(() => {
|
|
|
|
const { mode } = props;
|
2019-01-05 08:22:25 +00:00
|
|
|
|
2020-10-16 10:47:28 +00:00
|
|
|
if ((mode as any) === 'combobox') {
|
|
|
|
return undefined;
|
2019-01-05 08:22:25 +00:00
|
|
|
}
|
2020-10-16 10:47:28 +00:00
|
|
|
|
2022-01-21 13:58:10 +00:00
|
|
|
if (mode === SECRET_COMBOBOX_MODE_DO_NOT_USE) {
|
2020-10-16 10:47:28 +00:00
|
|
|
return 'combobox';
|
2019-01-05 08:22:25 +00:00
|
|
|
}
|
2020-10-16 10:47:28 +00:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
});
|
2022-05-12 01:50:12 +00:00
|
|
|
const { prefixCls, direction, configProvider, size, getPrefixCls } = useConfigInject(
|
|
|
|
'select',
|
|
|
|
props,
|
|
|
|
);
|
2021-12-17 08:59:50 +00:00
|
|
|
const rootPrefixCls = computed(() => getPrefixCls());
|
|
|
|
const transitionName = computed(() =>
|
|
|
|
getTransitionName(rootPrefixCls.value, 'slide-up', props.transitionName),
|
|
|
|
);
|
2020-10-17 14:19:52 +00:00
|
|
|
const mergedClassName = computed(() =>
|
2021-06-10 13:39:50 +00:00
|
|
|
classNames({
|
2022-05-12 01:50:12 +00:00
|
|
|
[`${prefixCls.value}-lg`]: size.value === 'large',
|
|
|
|
[`${prefixCls.value}-sm`]: size.value === 'small',
|
2021-06-10 13:39:50 +00:00
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
[`${prefixCls.value}-borderless`]: !props.bordered,
|
|
|
|
}),
|
2020-10-17 14:19:52 +00:00
|
|
|
);
|
2022-01-21 13:58:10 +00:00
|
|
|
const triggerChange: SelectProps['onChange'] = (...args) => {
|
2021-03-16 07:39:44 +00:00
|
|
|
emit('update:value', args[0]);
|
2020-10-17 14:19:52 +00:00
|
|
|
emit('change', ...args);
|
2021-09-25 08:51:32 +00:00
|
|
|
formItemContext.onFieldChange();
|
|
|
|
};
|
2022-01-21 13:58:10 +00:00
|
|
|
const handleBlur: SelectProps['onBlur'] = e => {
|
2021-09-25 08:51:32 +00:00
|
|
|
emit('blur', e);
|
|
|
|
formItemContext.onFieldBlur();
|
2020-10-17 14:19:52 +00:00
|
|
|
};
|
2021-06-10 13:39:50 +00:00
|
|
|
expose({
|
2020-10-16 10:47:28 +00:00
|
|
|
blur,
|
2021-06-10 13:39:50 +00:00
|
|
|
focus,
|
2022-01-21 13:58:10 +00:00
|
|
|
scrollTo,
|
2020-10-16 10:47:28 +00:00
|
|
|
});
|
2022-01-21 13:58:10 +00:00
|
|
|
const isMultiple = computed(() => mode.value === 'multiple' || mode.value === 'tags');
|
2021-06-10 13:39:50 +00:00
|
|
|
return () => {
|
|
|
|
const {
|
|
|
|
notFoundContent,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 24,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
2021-09-25 08:51:32 +00:00
|
|
|
id = formItemContext.id.value,
|
2021-12-07 14:23:36 +00:00
|
|
|
placeholder = slots.placeholder?.(),
|
2021-06-10 13:39:50 +00:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { renderEmpty, getPopupContainer: getContextPopupContainer } = configProvider;
|
|
|
|
|
|
|
|
// ===================== Empty =====================
|
2021-09-25 08:51:32 +00:00
|
|
|
let mergedNotFound: any;
|
2021-06-10 13:39:50 +00:00
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
mergedNotFound = notFoundContent;
|
|
|
|
} else if (slots.notFoundContent) {
|
|
|
|
mergedNotFound = slots.notFoundContent();
|
|
|
|
} else if (mode.value === 'combobox') {
|
|
|
|
mergedNotFound = null;
|
|
|
|
} else {
|
|
|
|
mergedNotFound = renderEmpty('Select') as any;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ===================== Icons =====================
|
|
|
|
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons(
|
|
|
|
{
|
|
|
|
...props,
|
2022-01-21 13:58:10 +00:00
|
|
|
multiple: isMultiple.value,
|
2021-06-10 13:39:50 +00:00
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
},
|
|
|
|
slots,
|
|
|
|
);
|
|
|
|
|
|
|
|
const selectProps = omit(props, [
|
|
|
|
'prefixCls',
|
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'size',
|
|
|
|
'bordered',
|
|
|
|
]);
|
|
|
|
|
|
|
|
const rcSelectRtlDropDownClassName = classNames(dropdownClassName, {
|
|
|
|
[`${prefixCls.value}-dropdown-${direction.value}`]: direction.value === 'rtl',
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<RcSelect
|
|
|
|
ref={selectRef}
|
|
|
|
virtual={virtual}
|
|
|
|
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
|
|
|
|
{...selectProps}
|
|
|
|
{...attrs}
|
2021-12-07 14:23:36 +00:00
|
|
|
placeholder={placeholder}
|
2021-06-10 13:39:50 +00:00
|
|
|
listHeight={listHeight}
|
|
|
|
listItemHeight={listItemHeight}
|
|
|
|
mode={mode.value}
|
|
|
|
prefixCls={prefixCls.value}
|
|
|
|
direction={direction.value}
|
|
|
|
inputIcon={suffixIcon}
|
|
|
|
menuItemSelectedIcon={itemIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
notFoundContent={mergedNotFound}
|
|
|
|
class={[mergedClassName.value, attrs.class]}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
dropdownClassName={rcSelectRtlDropDownClassName}
|
|
|
|
onChange={triggerChange}
|
2021-09-25 08:51:32 +00:00
|
|
|
onBlur={handleBlur}
|
|
|
|
id={id}
|
2021-06-10 13:39:50 +00:00
|
|
|
dropdownRender={selectProps.dropdownRender || slots.dropdownRender}
|
2021-08-11 07:22:49 +00:00
|
|
|
v-slots={{ option: slots.option }}
|
2021-12-17 08:59:50 +00:00
|
|
|
transitionName={transitionName.value}
|
2022-01-21 13:58:10 +00:00
|
|
|
children={slots.default?.()}
|
|
|
|
tagRender={props.tagRender || slots.tagRender}
|
2022-01-27 07:12:15 +00:00
|
|
|
optionLabelRender={slots.optionLabel}
|
2022-01-21 13:58:10 +00:00
|
|
|
maxTagPlaceholder={props.maxTagPlaceholder || slots.maxTagPlaceholder}
|
|
|
|
></RcSelect>
|
2021-06-10 13:39:50 +00:00
|
|
|
);
|
|
|
|
};
|
2020-10-17 14:19:52 +00:00
|
|
|
},
|
|
|
|
});
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2021-06-23 15:08:16 +00:00
|
|
|
Select.install = function (app: App) {
|
2020-06-28 15:11:31 +00:00
|
|
|
app.component(Select.name, Select);
|
2020-12-25 05:02:08 +00:00
|
|
|
app.component(Select.Option.displayName, Select.Option);
|
|
|
|
app.component(Select.OptGroup.displayName, Select.OptGroup);
|
2020-10-13 11:14:56 +00:00
|
|
|
return app;
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-06-23 13:47:53 +00:00
|
|
|
|
|
|
|
export const SelectOption = Select.Option;
|
|
|
|
export const SelectOptGroup = Select.OptGroup;
|
2020-11-01 07:03:33 +00:00
|
|
|
export default Select as typeof Select &
|
|
|
|
Plugin & {
|
|
|
|
readonly Option: typeof Option;
|
|
|
|
readonly OptGroup: typeof OptGroup;
|
|
|
|
readonly SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
|
|
|
};
|