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

155 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-11-01 07:03:33 +00:00
import { App, defineComponent, inject, provide, Plugin } 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';
const { Option, OptGroup } = Select;
2018-02-27 11:08:49 +00:00
2020-10-12 10:46:05 +00:00
function isSelectOptionOrSelectOptGroup(child: any): Boolean {
2020-07-01 16:24:44 +00:00
return child && child.type && (child.type.isSelectOption || child.type.isSelectOptGroup);
}
2018-02-27 11:08:49 +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
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: {
...AutoCompleteProps,
prefixCls: PropTypes.string.def('ant-select'),
showSearch: PropTypes.looseBool.def(false),
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'],
2018-04-08 13:17:20 +00:00
Option: { ...Option, name: 'AAutoCompleteOption' },
OptGroup: { ...OptGroup, name: 'AAutoCompleteOptGroup' },
setup(props, { slots }) {
warning(
!('dataSource' in props || '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: {
2020-10-12 10:46:05 +00:00
savePopupRef(ref: any) {
2019-07-04 10:12:52 +00:00
this.popupRef = ref;
},
2020-10-12 10:46:05 +00:00
saveSelect(node: any) {
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;
let optionChildren: any;
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-10-13 09:46:52 +00:00
const { class: className } = this.$attrs as any;
2018-02-27 11:08:49 +00:00
const cls = {
2020-07-01 16:24:44 +00:00
[className]: !!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
};
2020-07-01 16:24:44 +00:00
const childArray = getSlot(this, 'dataSource');
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'] as any),
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 */
2020-10-12 10:46:05 +00:00
AutoComplete.install = function(app: App) {
2020-07-01 16:24:44 +00:00
app.component(AutoComplete.name, AutoComplete);
app.component(AutoComplete.Option.name, AutoComplete.Option);
app.component(AutoComplete.OptGroup.name, 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;
};