ant-design-vue/components/auto-complete/index.tsx

165 lines
5.0 KiB
Vue
Raw Normal View History

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';
import { defaultConfigProvider } from '../config-provider';
2020-07-01 16:24:44 +00:00
import { getComponent, getOptionProps, isValidElement, getSlot } from '../_util/props-util';
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 = {
...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,
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'),
showSearch: PropTypes.looseBool,
2018-02-27 11:08:49 +00:00
transitionName: PropTypes.string.def('slide-up'),
choiceTransitionName: PropTypes.string.def('zoom'),
autofocus: PropTypes.looseBool,
backfill: PropTypes.looseBool,
2018-02-27 11:08:49 +00:00
optionLabelProp: PropTypes.string.def('children'),
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,
setup(props, { slots }) {
warning(
2021-06-08 06:18:54 +00:00
!(props.dataSource !== undefined || 'dataSource' in slots),
'AutoComplete',
'`dataSource` is deprecated, please use `options` instead.',
);
2019-07-04 10:12:52 +00:00
return {
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: {
savePopupRef(ref: VNode) {
2019-07-04 10:12:52 +00:00
this.popupRef = ref;
},
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);
const element = children.length ? children[0] : <Input lazy={false} />;
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() {
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
};
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])) {
optionChildren = childArray;
2018-02-27 11:08:49 +00:00
} else {
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':
return (
<Option key={item} value={item}>
{item}
</Option>
);
2019-01-12 03:33:27 +00:00
case 'object':
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 = {
...Omit(getOptionProps(this), ['dataSource', 'optionLabelProp']),
2020-07-01 16:24:44 +00:00
...this.$attrs,
mode: Select.SECRET_COMBOBOX_MODE_DO_NOT_USE,
// optionLabelProp,
2020-07-01 16:24:44 +00:00
getInputElement: this.getInputElement,
notFoundContent: getComponent(this, 'notFoundContent'),
// 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
};
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
/* 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
};
2020-11-01 07:03:33 +00:00
export default AutoComplete as typeof AutoComplete &
Plugin & {
readonly Option: typeof Option;
readonly OptGroup: typeof OptGroup;
};