2021-09-25 16:51:32 +08:00
|
|
|
import type { App, PropType, Plugin, ExtractPropTypes } from 'vue';
|
2021-06-26 09:35:40 +08:00
|
|
|
import { computed, defineComponent, ref } from 'vue';
|
2020-10-16 18:47:28 +08:00
|
|
|
import classNames from '../_util/classNames';
|
2021-09-25 16:51:32 +08:00
|
|
|
import RcSelect, { selectProps as vcSelectProps, Option, OptGroup } from '../vc-select';
|
2021-06-26 09:35:40 +08:00
|
|
|
import type { OptionProps as OptionPropsType } from '../vc-select/Option';
|
2020-10-16 18:47:28 +08:00
|
|
|
import getIcons from './utils/iconUtil';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { tuple } from '../_util/type';
|
2021-06-10 21:39:50 +08:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
2021-09-25 16:51:32 +08:00
|
|
|
import omit from '../_util/omit';
|
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
2020-10-16 18:47:28 +08:00
|
|
|
|
|
|
|
type RawValue = string | number;
|
|
|
|
|
2020-10-17 22:19:52 +08:00
|
|
|
export type OptionProps = OptionPropsType;
|
2020-10-16 18:47:28 +08:00
|
|
|
|
|
|
|
export type OptionType = typeof Option;
|
|
|
|
|
|
|
|
export interface LabeledValue {
|
|
|
|
key?: string;
|
|
|
|
value: RawValue;
|
2021-09-25 16:51:32 +08:00
|
|
|
label: any;
|
2020-10-16 18:47:28 +08:00
|
|
|
}
|
2021-06-22 10:47:33 +08:00
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[] | undefined;
|
2020-10-16 18:47:28 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
export const selectProps = () => ({
|
|
|
|
...omit(vcSelectProps<SelectValue>(), ['inputIcon', 'mode', 'getInputElement', 'backfill']),
|
2020-10-16 18:47:28 +08:00
|
|
|
value: {
|
2020-10-17 22:19:52 +08:00
|
|
|
type: [Array, Object, String, Number] as PropType<SelectValue>,
|
2020-10-16 18:47:28 +08:00
|
|
|
},
|
|
|
|
defaultValue: {
|
2020-10-17 22:19:52 +08:00
|
|
|
type: [Array, Object, String, Number] as PropType<SelectValue>,
|
2020-10-16 18:47:28 +08:00
|
|
|
},
|
2021-09-25 16:51:32 +08:00
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
suffixIcon: PropTypes.any,
|
|
|
|
itemIcon: PropTypes.any,
|
2020-10-17 22:19:52 +08:00
|
|
|
size: PropTypes.oneOf(tuple('small', 'middle', 'large', 'default')),
|
|
|
|
mode: PropTypes.oneOf(tuple('multiple', 'tags', 'SECRET_COMBOBOX_MODE_DO_NOT_USE')),
|
2020-10-16 18:47:28 +08:00
|
|
|
bordered: PropTypes.looseBool.def(true),
|
2021-11-28 10:00:17 +08:00
|
|
|
transitionName: PropTypes.string.def('ant-slide-up'),
|
2020-10-16 18:47:28 +08:00
|
|
|
choiceTransitionName: PropTypes.string.def(''),
|
2020-10-17 22:19:52 +08:00
|
|
|
});
|
2018-02-23 16:14:59 +08:00
|
|
|
|
2021-09-25 16:51:32 +08:00
|
|
|
export type SelectProps = Partial<ExtractPropTypes<ReturnType<typeof selectProps>>>;
|
|
|
|
|
2020-10-12 18:46:05 +08:00
|
|
|
const Select = defineComponent({
|
2018-04-08 21:17:20 +08:00
|
|
|
name: 'ASelect',
|
2020-10-16 18:47:28 +08:00
|
|
|
Option,
|
|
|
|
OptGroup,
|
|
|
|
inheritAttrs: false,
|
2021-09-25 16:51:32 +08:00
|
|
|
props: selectProps(),
|
2020-10-16 18:47:28 +08:00
|
|
|
SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE',
|
2021-09-25 16:51:32 +08:00
|
|
|
emits: ['change', 'update:value', 'blur'],
|
2021-08-11 15:22:49 +08:00
|
|
|
slots: [
|
|
|
|
'notFoundContent',
|
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'dropdownRender',
|
|
|
|
'option',
|
2021-12-07 22:23:36 +08:00
|
|
|
'placeholder',
|
2021-08-11 15:22:49 +08:00
|
|
|
],
|
2021-06-10 21:39:50 +08:00
|
|
|
setup(props, { attrs, emit, slots, expose }) {
|
2021-09-25 16:51:32 +08:00
|
|
|
const selectRef = ref();
|
|
|
|
const formItemContext = useInjectFormItemContext();
|
2020-10-16 18:47:28 +08:00
|
|
|
const focus = () => {
|
|
|
|
if (selectRef.value) {
|
|
|
|
selectRef.value.focus();
|
2019-04-20 16:23:50 +08:00
|
|
|
}
|
2020-10-16 18:47:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const blur = () => {
|
|
|
|
if (selectRef.value) {
|
|
|
|
selectRef.value.blur();
|
2018-02-23 16:14:59 +08:00
|
|
|
}
|
2020-10-16 18:47:28 +08:00
|
|
|
};
|
2020-03-07 19:45:13 +08:00
|
|
|
|
2020-10-17 22:19:52 +08:00
|
|
|
const mode = computed(() => {
|
|
|
|
const { mode } = props;
|
2019-01-05 16:22:25 +08:00
|
|
|
|
2020-10-16 18:47:28 +08:00
|
|
|
if ((mode as any) === 'combobox') {
|
|
|
|
return undefined;
|
2019-01-05 16:22:25 +08:00
|
|
|
}
|
2020-10-16 18:47:28 +08:00
|
|
|
|
|
|
|
if (mode === Select.SECRET_COMBOBOX_MODE_DO_NOT_USE) {
|
|
|
|
return 'combobox';
|
2019-01-05 16:22:25 +08:00
|
|
|
}
|
2020-10-16 18:47:28 +08:00
|
|
|
|
|
|
|
return mode;
|
|
|
|
});
|
2021-06-10 21:39:50 +08:00
|
|
|
const { prefixCls, direction, configProvider } = useConfigInject('select', props);
|
2020-10-17 22:19:52 +08:00
|
|
|
const mergedClassName = computed(() =>
|
2021-06-10 21:39:50 +08: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 22:19:52 +08:00
|
|
|
);
|
|
|
|
const triggerChange = (...args: any[]) => {
|
2021-03-16 15:39:44 +08:00
|
|
|
emit('update:value', args[0]);
|
2020-10-17 22:19:52 +08:00
|
|
|
emit('change', ...args);
|
2021-09-25 16:51:32 +08:00
|
|
|
formItemContext.onFieldChange();
|
|
|
|
};
|
|
|
|
const handleBlur = (e: InputEvent) => {
|
|
|
|
emit('blur', e);
|
|
|
|
formItemContext.onFieldBlur();
|
2020-10-17 22:19:52 +08:00
|
|
|
};
|
2021-06-10 21:39:50 +08:00
|
|
|
expose({
|
2020-10-16 18:47:28 +08:00
|
|
|
blur,
|
2021-06-10 21:39:50 +08:00
|
|
|
focus,
|
2020-10-16 18:47:28 +08:00
|
|
|
});
|
2021-06-10 21:39:50 +08:00
|
|
|
return () => {
|
|
|
|
const {
|
|
|
|
notFoundContent,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 24,
|
|
|
|
getPopupContainer,
|
|
|
|
dropdownClassName,
|
|
|
|
virtual,
|
|
|
|
dropdownMatchSelectWidth,
|
2021-09-25 16:51:32 +08:00
|
|
|
id = formItemContext.id.value,
|
2021-12-07 22:23:36 +08:00
|
|
|
placeholder = slots.placeholder?.(),
|
2021-06-10 21:39:50 +08:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const { renderEmpty, getPopupContainer: getContextPopupContainer } = configProvider;
|
|
|
|
|
|
|
|
const isMultiple = mode.value === 'multiple' || mode.value === 'tags';
|
|
|
|
|
|
|
|
// ===================== Empty =====================
|
2021-09-25 16:51:32 +08:00
|
|
|
let mergedNotFound: any;
|
2021-06-10 21:39:50 +08: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}
|
2021-12-07 22:23:36 +08:00
|
|
|
placeholder={placeholder}
|
2021-06-10 21:39:50 +08: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 16:51:32 +08:00
|
|
|
onBlur={handleBlur}
|
|
|
|
id={id}
|
2021-06-10 21:39:50 +08:00
|
|
|
dropdownRender={selectProps.dropdownRender || slots.dropdownRender}
|
2021-08-11 15:22:49 +08:00
|
|
|
v-slots={{ option: slots.option }}
|
2021-06-10 21:39:50 +08:00
|
|
|
>
|
|
|
|
{slots.default?.()}
|
|
|
|
</RcSelect>
|
|
|
|
);
|
|
|
|
};
|
2020-10-17 22:19:52 +08:00
|
|
|
},
|
|
|
|
});
|
2018-09-19 13:21:57 +08:00
|
|
|
/* istanbul ignore next */
|
2021-06-23 23:08:16 +08:00
|
|
|
Select.install = function (app: App) {
|
2020-06-28 23:11:31 +08:00
|
|
|
app.component(Select.name, Select);
|
2020-12-25 13:02:08 +08:00
|
|
|
app.component(Select.Option.displayName, Select.Option);
|
|
|
|
app.component(Select.OptGroup.displayName, Select.OptGroup);
|
2020-10-13 19:14:56 +08:00
|
|
|
return app;
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2021-06-23 21:47:53 +08:00
|
|
|
|
|
|
|
export const SelectOption = Select.Option;
|
|
|
|
export const SelectOptGroup = Select.OptGroup;
|
2020-11-01 15:03:33 +08: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';
|
|
|
|
};
|