2020-11-01 07:03:33 +00:00
|
|
|
import { computed, defineComponent, inject, ref, VNodeChild, App, PropType, Plugin } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import omit from 'omit.js';
|
2020-10-16 10:47:28 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-10-19 09:17:10 +00:00
|
|
|
import RcSelect, { Option, OptGroup, SelectProps as RcSelectProps, BaseProps } from '../vc-select';
|
|
|
|
import { OptionProps as OptionPropsType } from '../vc-select/Option';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-10-16 10:47:28 +00:00
|
|
|
import getIcons from './utils/iconUtil';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
|
|
|
import { tuple } from '../_util/type';
|
|
|
|
|
|
|
|
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;
|
|
|
|
label: VNodeChild;
|
|
|
|
}
|
|
|
|
export type SizeType = 'small' | 'middle' | 'large' | undefined;
|
|
|
|
export type SelectValue = RawValue | RawValue[] | LabeledValue | LabeledValue[];
|
|
|
|
|
|
|
|
export interface InternalSelectProps<VT> extends Omit<RcSelectProps<VT>, 'mode'> {
|
|
|
|
suffixIcon?: VNodeChild;
|
|
|
|
itemIcon?: VNodeChild;
|
|
|
|
size?: SizeType;
|
|
|
|
mode?: 'multiple' | 'tags' | 'SECRET_COMBOBOX_MODE_DO_NOT_USE';
|
|
|
|
bordered?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface SelectPropsTypes<VT>
|
2020-10-17 14:19:52 +00:00
|
|
|
extends Omit<
|
|
|
|
InternalSelectProps<VT>,
|
|
|
|
'inputIcon' | 'mode' | 'getInputElement' | 'backfill' | 'class' | 'style'
|
|
|
|
> {
|
2020-10-16 10:47:28 +00:00
|
|
|
mode?: 'multiple' | 'tags';
|
|
|
|
}
|
2020-10-17 14:19:52 +00:00
|
|
|
export type SelectTypes = SelectPropsTypes<SelectValue>;
|
|
|
|
export const SelectProps = () => ({
|
2020-10-22 02:06:57 +00:00
|
|
|
...(omit(BaseProps(), [
|
|
|
|
'inputIcon',
|
|
|
|
'mode',
|
|
|
|
'getInputElement',
|
|
|
|
'backfill',
|
|
|
|
'class',
|
|
|
|
'style',
|
|
|
|
]) as Omit<
|
|
|
|
ReturnType<typeof BaseProps>,
|
|
|
|
'inputIcon' | 'mode' | 'getInputElement' | 'backfill' | 'class' | 'style'
|
|
|
|
>),
|
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
|
|
|
},
|
2020-10-17 14:19:52 +00:00
|
|
|
notFoundContent: PropTypes.VNodeChild,
|
2020-10-16 10:47:28 +00:00
|
|
|
suffixIcon: PropTypes.VNodeChild,
|
|
|
|
itemIcon: PropTypes.VNodeChild,
|
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
|
|
|
|
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,
|
2020-10-17 14:19:52 +00:00
|
|
|
props: SelectProps(),
|
2020-10-16 10:47:28 +00:00
|
|
|
SECRET_COMBOBOX_MODE_DO_NOT_USE: 'SECRET_COMBOBOX_MODE_DO_NOT_USE',
|
|
|
|
emits: ['change', 'update:value'],
|
2020-10-17 14:19:52 +00:00
|
|
|
setup(props: any, { attrs, emit }) {
|
2020-10-16 10:47:28 +00:00
|
|
|
const selectRef = ref(null);
|
|
|
|
|
|
|
|
const configProvider = inject('configProvider', defaultConfigProvider);
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
2020-10-17 14:19:52 +00:00
|
|
|
const prefixCls = computed(() => {
|
|
|
|
return configProvider.getPrefixCls('select', props.prefixCls);
|
|
|
|
});
|
|
|
|
const mergedClassName = computed(() =>
|
|
|
|
classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls.value}-lg`]: props.size === 'large',
|
|
|
|
[`${prefixCls.value}-sm`]: props.size === 'small',
|
|
|
|
[`${prefixCls.value}-rtl`]: props.direction === 'rtl',
|
|
|
|
[`${prefixCls.value}-borderless`]: !props.bordered,
|
|
|
|
},
|
|
|
|
attrs.class,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const triggerChange = (...args: any[]) => {
|
|
|
|
emit('update:value', ...args);
|
|
|
|
emit('change', ...args);
|
|
|
|
};
|
2020-10-16 10:47:28 +00:00
|
|
|
return {
|
2020-10-24 01:33:37 +00:00
|
|
|
selectRef,
|
2020-10-16 10:47:28 +00:00
|
|
|
mergedClassName,
|
|
|
|
mode,
|
|
|
|
focus,
|
|
|
|
blur,
|
|
|
|
configProvider,
|
2020-10-17 14:19:52 +00:00
|
|
|
triggerChange,
|
|
|
|
prefixCls,
|
|
|
|
};
|
2019-04-20 08:23:50 +00:00
|
|
|
},
|
|
|
|
render() {
|
|
|
|
const {
|
2020-10-17 14:19:52 +00:00
|
|
|
configProvider,
|
|
|
|
mode,
|
|
|
|
mergedClassName,
|
|
|
|
triggerChange,
|
|
|
|
prefixCls,
|
|
|
|
$slots: slots,
|
|
|
|
$props,
|
|
|
|
} = this as any;
|
|
|
|
const props: SelectTypes = $props;
|
|
|
|
const {
|
2020-10-16 10:47:28 +00:00
|
|
|
notFoundContent,
|
|
|
|
listHeight = 256,
|
|
|
|
listItemHeight = 24,
|
2019-04-20 08:23:50 +00:00
|
|
|
getPopupContainer,
|
2020-10-16 10:47:28 +00:00
|
|
|
dropdownClassName,
|
|
|
|
direction,
|
|
|
|
virtual,
|
2020-10-17 14:19:52 +00:00
|
|
|
dropdownMatchSelectWidth,
|
2020-10-16 10:47:28 +00:00
|
|
|
} = props;
|
2019-01-05 08:22:25 +00:00
|
|
|
|
2020-10-17 14:19:52 +00:00
|
|
|
const { renderEmpty, getPopupContainer: getContextPopupContainer } = configProvider;
|
2018-02-23 08:14:59 +00:00
|
|
|
|
2020-10-16 10:47:28 +00:00
|
|
|
const isMultiple = mode === 'multiple' || mode === 'tags';
|
|
|
|
|
|
|
|
// ===================== Empty =====================
|
|
|
|
let mergedNotFound: VNodeChild;
|
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
mergedNotFound = notFoundContent;
|
2020-10-17 14:19:52 +00:00
|
|
|
} else if (slots.notFoundContent) {
|
|
|
|
mergedNotFound = slots.notFoundContent();
|
2020-10-16 10:47:28 +00:00
|
|
|
} else if (mode === 'combobox') {
|
|
|
|
mergedNotFound = null;
|
|
|
|
} else {
|
|
|
|
mergedNotFound = renderEmpty('Select') as any;
|
2019-04-20 08:23:50 +00:00
|
|
|
}
|
2018-12-11 03:40:27 +00:00
|
|
|
|
2020-10-16 10:47:28 +00:00
|
|
|
// ===================== Icons =====================
|
2020-10-17 14:19:52 +00:00
|
|
|
const { suffixIcon, itemIcon, removeIcon, clearIcon } = getIcons(
|
|
|
|
{
|
|
|
|
...this.$props,
|
|
|
|
multiple: isMultiple,
|
|
|
|
prefixCls,
|
|
|
|
},
|
|
|
|
slots,
|
|
|
|
);
|
2018-02-23 08:14:59 +00:00
|
|
|
|
2020-10-16 10:47:28 +00:00
|
|
|
const selectProps = omit(props, [
|
|
|
|
'prefixCls',
|
|
|
|
'suffixIcon',
|
|
|
|
'itemIcon',
|
|
|
|
'removeIcon',
|
|
|
|
'clearIcon',
|
|
|
|
'size',
|
|
|
|
'bordered',
|
|
|
|
]) as any;
|
|
|
|
|
|
|
|
const rcSelectRtlDropDownClassName = classNames(dropdownClassName, {
|
|
|
|
[`${prefixCls}-dropdown-${direction}`]: direction === 'rtl',
|
|
|
|
});
|
2020-10-17 14:19:52 +00:00
|
|
|
return (
|
|
|
|
<RcSelect
|
|
|
|
ref="selectRef"
|
|
|
|
virtual={virtual}
|
|
|
|
dropdownMatchSelectWidth={dropdownMatchSelectWidth}
|
|
|
|
{...selectProps}
|
|
|
|
{...this.$attrs}
|
|
|
|
listHeight={listHeight}
|
|
|
|
listItemHeight={listItemHeight}
|
|
|
|
mode={mode}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
direction={direction}
|
|
|
|
inputIcon={suffixIcon}
|
|
|
|
menuItemSelectedIcon={itemIcon}
|
|
|
|
removeIcon={removeIcon}
|
|
|
|
clearIcon={clearIcon}
|
|
|
|
notFoundContent={mergedNotFound}
|
|
|
|
class={mergedClassName}
|
|
|
|
getPopupContainer={getPopupContainer || getContextPopupContainer}
|
|
|
|
dropdownClassName={rcSelectRtlDropDownClassName}
|
|
|
|
onChange={triggerChange}
|
|
|
|
>
|
2020-10-24 12:36:57 +00:00
|
|
|
{slots.default?.()}
|
2020-10-17 14:19:52 +00:00
|
|
|
</RcSelect>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2020-10-12 10:46:05 +00:00
|
|
|
Select.install = function(app: App) {
|
2020-06-28 15:11:31 +00:00
|
|
|
app.component(Select.name, Select);
|
2020-10-16 10:47:28 +00:00
|
|
|
app.component('ASelectOption', Select.Option);
|
|
|
|
app.component('ASelectOptGroup', Select.OptGroup);
|
2020-10-13 11:14:56 +00:00
|
|
|
return app;
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
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';
|
|
|
|
};
|