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';
|
2021-09-25 08:51:32 +00:00
|
|
|
import RcSelect, { selectProps as vcSelectProps, Option, OptGroup } from '../vc-select';
|
2021-06-26 01:35:40 +00:00
|
|
|
import type { OptionProps as OptionPropsType } from '../vc-select/Option';
|
2020-10-16 10:47:28 +00:00
|
|
|
import getIcons from './utils/iconUtil';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { tuple } from '../_util/type';
|
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';
|
2020-10-16 10:47:28 +00:00
|
|
|
|
|
|
|
type RawValue = string | number;
|
|
|
|
|
2020-10-17 14:19:52 +00:00
|
|
|
export type OptionProps = OptionPropsType;
|
2020-10-16 10:47:28 +00:00
|
|
|
|
|
|
|
export type OptionType = typeof Option;
|
|
|
|
|
|
|
|
export interface LabeledValue {
|
|
|
|
key?: string;
|
|
|
|
value: RawValue;
|
2021-09-25 08:51:32 +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 = () => ({
|
|
|
|
...omit(vcSelectProps<SelectValue>(), ['inputIcon', 'mode', 'getInputElement', '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,
|
2020-10-17 14:19:52 +00:00
|
|
|
size: PropTypes.oneOf(tuple('small', 'middle', 'large', 'default')),
|
|
|
|
mode: PropTypes.oneOf(tuple('multiple', 'tags', 'SECRET_COMBOBOX_MODE_DO_NOT_USE')),
|
2020-10-16 10:47:28 +00:00
|
|
|
bordered: PropTypes.looseBool.def(true),
|
|
|
|
transitionName: PropTypes.string.def('slide-up'),
|
|
|
|
choiceTransitionName: PropTypes.string.def(''),
|
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>>>;
|
|
|
|
|
2020-10-12 10:46:05 +00:00
|
|
|
const Select = defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ASelect',
|
2020-10-16 10:47:28 +00:00
|
|
|
Option,
|
|
|
|
OptGroup,
|
|
|
|
inheritAttrs: false,
|
2021-09-25 08:51:32 +00:00
|
|
|
props: selectProps(),
|
2020-10-16 10:47:28 +00:00
|
|
|
SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE',
|
2021-09-25 08:51:32 +00:00
|
|
|
emits: ['change', 'update:value', 'blur'],
|
2021-08-11 07:22:49 +00:00
|
|
|
slots: [
|
|
|
|
'notFoundContent',
|
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'dropdownRender',
|
|
|
|
'option',
|
|
|
|
],
|
2021-06-10 13:39:50 +00:00
|
|
|
setup(props, { attrs, emit, slots, expose }) {
|
2021-09-25 08:51:32 +00:00
|
|
|
const selectRef = ref();
|
|
|
|
const formItemContext = useInjectFormItemContext();
|
2020-10-16 10:47:28 +00:00
|
|
|
const focus = () => {
|
|
|
|
if (selectRef.value) {
|
|
|
|
selectRef.value.focus();
|
2019-04-20 08:23:50 +00:00
|
|
|
}
|
2020-10-16 10:47:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const blur = () => {
|
|
|
|
if (selectRef.value) {
|
|
|
|
selectRef.value.blur();
|
2018-02-23 08:14:59 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
if (mode === Select.SECRET_COMBOBOX_MODE_DO_NOT_USE) {
|
|
|
|
return 'combobox';
|
2019-01-05 08:22:25 +00:00
|
|
|
}
|
2020-10-16 10:47:28 +00:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
});
|
2021-06-10 13:39:50 +00:00
|
|
|
const { prefixCls, direction, configProvider } = useConfigInject('select', props);
|
2020-10-17 14:19:52 +00:00
|
|
|
const mergedClassName = computed(() =>
|
2021-06-10 13:39:50 +00:00
|
|
|
classNames({
|
|
|
|
[`${prefixCls.value}-lg`]: props.size === 'large',
|
|
|
|
[`${prefixCls.value}-sm`]: props.size === 'small',
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
[`${prefixCls.value}-borderless`]: !props.bordered,
|
|
|
|
}),
|
2020-10-17 14:19:52 +00:00
|
|
|
);
|
|
|
|
const triggerChange = (...args: any[]) => {
|
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();
|
|
|
|
};
|
|
|
|
const handleBlur = (e: InputEvent) => {
|
|
|
|
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,
|
2020-10-16 10:47:28 +00:00
|
|
|
});
|
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-06-10 13:39:50 +00:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { renderEmpty, getPopupContainer: getContextPopupContainer } = configProvider;
|
|
|
|
|
|
|
|
const isMultiple = mode.value === 'multiple' || mode.value === 'tags';
|
|
|
|
|
|
|
|
// ===================== 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,
|
|
|
|
multiple: isMultiple,
|
|
|
|
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}
|
|
|
|
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-06-10 13:39:50 +00:00
|
|
|
>
|
|
|
|
{slots.default?.()}
|
|
|
|
</RcSelect>
|
|
|
|
);
|
|
|
|
};
|
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';
|
|
|
|
};
|