2021-06-26 01:35:40 +00:00
|
|
|
import type { App, Plugin, VNode, ExtractPropTypes } from 'vue';
|
|
|
|
import { defineComponent, inject, provide } from 'vue';
|
2020-10-16 10:47:28 +00:00
|
|
|
import Select, { SelectProps } from '../select';
|
2019-01-12 03:33:27 +00:00
|
|
|
import Input from '../input';
|
|
|
|
import InputElement from './InputElement';
|
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-09-30 02:47:18 +00:00
|
|
|
import { defaultConfigProvider } from '../config-provider';
|
2020-07-01 16:24:44 +00:00
|
|
|
import { getComponent, getOptionProps, isValidElement, getSlot } from '../_util/props-util';
|
2020-10-17 14:19:52 +00:00
|
|
|
import Omit from 'omit.js';
|
|
|
|
import warning from '../_util/warning';
|
2021-06-23 13:47:53 +00:00
|
|
|
import Option from './Option';
|
|
|
|
import OptGroup from './OptGroup';
|
2018-02-27 11:08:49 +00:00
|
|
|
|
2020-11-14 05:56:39 +00:00
|
|
|
function isSelectOptionOrSelectOptGroup(child: any): boolean {
|
|
|
|
return child?.type?.isSelectOption || child?.type?.isSelectOptGroup;
|
2020-07-01 16:24:44 +00:00
|
|
|
}
|
2018-02-27 11:08:49 +00:00
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
const autoCompleteProps = {
|
2020-10-17 14:19:52 +00:00
|
|
|
...SelectProps(),
|
2018-05-06 12:37:39 +00:00
|
|
|
dataSource: PropTypes.array,
|
2020-10-13 02:43:39 +00:00
|
|
|
dropdownMenuStyle: PropTypes.style,
|
2020-10-12 10:46:05 +00:00
|
|
|
optionLabelProp: PropTypes.string,
|
2020-10-10 10:16:28 +00:00
|
|
|
dropdownMatchSelectWidth: PropTypes.looseBool,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-02-27 11:08:49 +00:00
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
export type AutoCompleteProps = Partial<ExtractPropTypes<typeof autoCompleteProps>>;
|
|
|
|
|
|
|
|
export const AutoCompleteOption = Option;
|
|
|
|
|
|
|
|
export const AutoCompleteOptGroup = OptGroup;
|
|
|
|
|
2020-10-12 10:46:05 +00:00
|
|
|
const AutoComplete = defineComponent({
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'AAutoComplete',
|
2020-07-01 16:24:44 +00:00
|
|
|
inheritAttrs: false,
|
2018-02-27 11:08:49 +00:00
|
|
|
props: {
|
2021-06-23 13:47:53 +00:00
|
|
|
...autoCompleteProps,
|
2018-02-27 11:08:49 +00:00
|
|
|
prefixCls: PropTypes.string.def('ant-select'),
|
2021-05-24 08:34:52 +00:00
|
|
|
showSearch: PropTypes.looseBool,
|
2018-02-27 11:08:49 +00:00
|
|
|
transitionName: PropTypes.string.def('slide-up'),
|
|
|
|
choiceTransitionName: PropTypes.string.def('zoom'),
|
2020-10-10 10:16:28 +00:00
|
|
|
autofocus: PropTypes.looseBool,
|
|
|
|
backfill: PropTypes.looseBool,
|
2018-02-27 11:08:49 +00:00
|
|
|
optionLabelProp: PropTypes.string.def('children'),
|
2020-10-10 10:16:28 +00:00
|
|
|
filterOption: PropTypes.oneOfType([PropTypes.looseBool, PropTypes.func]).def(false),
|
|
|
|
defaultActiveFirstOption: PropTypes.looseBool.def(true),
|
2018-02-27 11:08:49 +00:00
|
|
|
},
|
2020-11-07 13:19:50 +00:00
|
|
|
emits: ['change', 'select', 'focus', 'blur'],
|
2021-06-23 13:47:53 +00:00
|
|
|
Option,
|
|
|
|
OptGroup,
|
2020-10-17 14:19:52 +00:00
|
|
|
setup(props, { slots }) {
|
|
|
|
warning(
|
2021-06-08 06:18:54 +00:00
|
|
|
!(props.dataSource !== undefined || 'dataSource' in slots),
|
2020-10-17 14:19:52 +00:00
|
|
|
'AutoComplete',
|
|
|
|
'`dataSource` is deprecated, please use `options` instead.',
|
|
|
|
);
|
2019-07-04 10:12:52 +00:00
|
|
|
return {
|
2020-09-30 02:47:18 +00:00
|
|
|
configProvider: inject('configProvider', defaultConfigProvider),
|
2020-10-12 10:46:05 +00:00
|
|
|
popupRef: null,
|
2020-10-13 09:46:52 +00:00
|
|
|
select: null,
|
2019-07-04 10:12:52 +00:00
|
|
|
};
|
|
|
|
},
|
2020-07-01 16:24:44 +00:00
|
|
|
created() {
|
|
|
|
provide('savePopupRef', this.savePopupRef);
|
|
|
|
},
|
2018-02-27 11:08:49 +00:00
|
|
|
methods: {
|
2020-11-13 02:51:12 +00:00
|
|
|
savePopupRef(ref: VNode) {
|
2019-07-04 10:12:52 +00:00
|
|
|
this.popupRef = ref;
|
|
|
|
},
|
2020-11-13 02:51:12 +00:00
|
|
|
saveSelect(node: VNode) {
|
2020-07-01 16:24:44 +00:00
|
|
|
this.select = node;
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
getInputElement() {
|
2020-07-01 16:24:44 +00:00
|
|
|
const children = getSlot(this);
|
2019-12-06 09:11:17 +00:00
|
|
|
const element = children.length ? children[0] : <Input lazy={false} />;
|
2020-10-17 14:19:52 +00:00
|
|
|
return <InputElement {...element.props}>{element}</InputElement>;
|
2018-02-27 11:08:49 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
focus() {
|
2020-07-01 16:24:44 +00:00
|
|
|
if (this.select) {
|
|
|
|
this.select.focus();
|
2018-05-18 13:09:52 +00:00
|
|
|
}
|
2018-02-27 11:08:49 +00:00
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
blur() {
|
2020-07-01 16:24:44 +00:00
|
|
|
if (this.select) {
|
|
|
|
this.select.blur();
|
2018-05-18 13:09:52 +00:00
|
|
|
}
|
2018-02-27 11:08:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-10-17 14:19:52 +00:00
|
|
|
const { size, prefixCls: customizePrefixCls, dataSource } = this;
|
2020-11-14 05:56:39 +00:00
|
|
|
let optionChildren: VNode[];
|
2020-10-24 01:35:59 +00:00
|
|
|
const { getPrefixCls } = this.configProvider;
|
2019-03-11 11:58:32 +00:00
|
|
|
const prefixCls = getPrefixCls('select', customizePrefixCls);
|
2020-11-14 05:56:39 +00:00
|
|
|
const { class: className } = this.$attrs;
|
2018-02-27 11:08:49 +00:00
|
|
|
const cls = {
|
2020-11-14 05:56:39 +00:00
|
|
|
[className as string]: !!className,
|
2018-02-27 11:08:49 +00:00
|
|
|
[`${prefixCls}-lg`]: size === 'large',
|
|
|
|
[`${prefixCls}-sm`]: size === 'small',
|
|
|
|
[`${prefixCls}-show-search`]: true,
|
|
|
|
[`${prefixCls}-auto-complete`]: true,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2021-06-24 01:42:27 +00:00
|
|
|
let childArray = getSlot(this, 'dataSource');
|
|
|
|
if ('options' in this.$slots) {
|
|
|
|
childArray = getSlot(this, 'options');
|
|
|
|
}
|
2020-07-01 16:24:44 +00:00
|
|
|
if (childArray.length && isSelectOptionOrSelectOptGroup(childArray[0])) {
|
2020-10-17 14:19:52 +00:00
|
|
|
optionChildren = childArray;
|
2018-02-27 11:08:49 +00:00
|
|
|
} else {
|
2020-10-17 14:19:52 +00:00
|
|
|
optionChildren = dataSource
|
2020-10-12 10:46:05 +00:00
|
|
|
? dataSource.map((item: any) => {
|
2019-01-12 03:33:27 +00:00
|
|
|
if (isValidElement(item)) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
switch (typeof item) {
|
|
|
|
case 'string':
|
2020-10-17 14:19:52 +00:00
|
|
|
return (
|
|
|
|
<Option key={item} value={item}>
|
|
|
|
{item}
|
|
|
|
</Option>
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
case 'object':
|
2020-10-17 14:19:52 +00:00
|
|
|
return (
|
|
|
|
<Option key={item.value} value={item.value}>
|
|
|
|
{item.text}
|
|
|
|
</Option>
|
|
|
|
);
|
2019-01-12 03:33:27 +00:00
|
|
|
default:
|
|
|
|
throw new Error(
|
|
|
|
'AutoComplete[dataSource] only supports type `string[] | Object[]`.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: [];
|
2018-02-27 11:08:49 +00:00
|
|
|
}
|
2018-02-28 11:07:04 +00:00
|
|
|
const selectProps = {
|
2020-11-13 02:51:12 +00:00
|
|
|
...Omit(getOptionProps(this), ['dataSource', 'optionLabelProp']),
|
2020-07-01 16:24:44 +00:00
|
|
|
...this.$attrs,
|
|
|
|
mode: Select.SECRET_COMBOBOX_MODE_DO_NOT_USE,
|
2020-10-17 14:19:52 +00:00
|
|
|
// optionLabelProp,
|
2020-07-01 16:24:44 +00:00
|
|
|
getInputElement: this.getInputElement,
|
|
|
|
notFoundContent: getComponent(this, 'notFoundContent'),
|
2020-10-17 14:19:52 +00:00
|
|
|
// placeholder: '',
|
2018-02-28 11:07:04 +00:00
|
|
|
class: cls,
|
2020-07-01 16:24:44 +00:00
|
|
|
ref: this.saveSelect,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2020-10-17 14:19:52 +00:00
|
|
|
return <Select {...selectProps}>{optionChildren}</Select>;
|
2018-02-27 11:08:49 +00:00
|
|
|
},
|
2020-10-12 10:46:05 +00:00
|
|
|
});
|
2018-02-27 11:08:49 +00:00
|
|
|
|
2018-09-19 05:21:57 +00:00
|
|
|
/* istanbul ignore next */
|
2021-06-23 15:08:16 +00:00
|
|
|
AutoComplete.install = function (app: App) {
|
2020-07-01 16:24:44 +00:00
|
|
|
app.component(AutoComplete.name, AutoComplete);
|
2021-06-23 13:47:53 +00:00
|
|
|
app.component(AutoComplete.Option.displayName, AutoComplete.Option);
|
|
|
|
app.component(AutoComplete.OptGroup.displayName, AutoComplete.OptGroup);
|
2020-10-13 11:14:56 +00:00
|
|
|
return app;
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-09-19 05:21:57 +00:00
|
|
|
|
2020-11-01 07:03:33 +00:00
|
|
|
export default AutoComplete as typeof AutoComplete &
|
|
|
|
Plugin & {
|
|
|
|
readonly Option: typeof Option;
|
|
|
|
readonly OptGroup: typeof OptGroup;
|
|
|
|
};
|