AutoComplete自动完成·续1
<template> <div class="global-search-wrapper" style="width: 300px"> <a-auto-complete v-model:value="value" class="global-search" size="large" style="width: 100%" option-label-prop="title" @select="onSelect" @search="handleSearch" > <template #dataSource> <a-select-option v-for="item in dataSource" :key="item.category" :title="item.category"> Found {{ item.query }} on <a :href="`https://s.taobao.com/search?q=${item.query}`" target="_blank" rel="noopener noreferrer" > {{ item.category }} </a> <span class="global-search-item-count">{{ item.count }} results</span> </a-select-option> </template> <a-input-search size="large" placeholder="input here" enterButton></a-input-search> </a-auto-complete> </div></template><script lang="ts">import { defineComponent, ref } from 'vue';interface Option { query: string; category: string; count: number;}export default defineComponent({ setup() { const value = ref(''); const dataSource = ref<Option[]>([]); const onSelect = (value: string) => { console.log('onSelect', value); }; const getRandomInt = (max: number, min = 0) => { return Math.floor(Math.random() * (max - min + 1)) + min; }; const searchResult = (query: string): Option[] => { return new Array(getRandomInt(5)) .join('.') .split('.') .map((item, idx) => ({ query, category: `${query}${idx}`, count: getRandomInt(200, 100), })); }; const handleSearch = (val: string) => { dataSource.value = val ? searchResult(val) : []; }; return { value, dataSource, onSelect, handleSearch, }; },});</script><style>.global-search-wrapper { padding-right: 50px;}.global-search { width: 100%;}.global-search.ant-select-auto-complete .ant-select-selection--single { margin-right: -46px;}.global-search.ant-select-auto-complete .ant-input-affix-wrapper .ant-input:not(:last-child) { padding-right: 62px;}.global-search.ant-select-auto-complete .ant-input-affix-wrapper .ant-input-suffix button { border-top-left-radius: 0; border-bottom-left-radius: 0;}.global-search-item { display: flex;}.global-search-item-desc { flex: auto; text-overflow: ellipsis; overflow: hidden;}.global-search-item-count { flex: none;}</style>
API
<a-auto-complete v-model:value="value" :options="options" />
| allowClear |
支持清除, 单选模式有效 |
boolean |
false |
|
|
|
|
|
|
| autofocus |
自动获取焦点 |
boolean |
false |
|
|
|
|
|
|
| backfill |
使用键盘选择选项的时候把选中项回填到输入框中 |
boolean |
false |
|
|
|
|
|
|
| #default (自定义输入框) |
自定义输入框 |
HTMLInputElement / HTMLTextAreaElement |
<Input /> |
|
|
|
|
|
|
| options | 自动完成的数据源 | slot | DataSourceItemType[] | | |
|---|---|---|---|---|
| dropdownMenuStyle |
dropdown 菜单自定义样式 |
object |
|
1.5.0 |
|
|
|
|
|
| defaultActiveFirstOption |
是否默认高亮第一个选项。 |
boolean |
true |
|
|
|
|
|
|
| disabled |
是否禁用 |
boolean |
false |
|
|
|
|
|
|
| filterOption |
是否根据输入项进行筛选。当其为一个函数时,会接收 inputValue option 两个参数,当 option 符合筛选条件时,应返回 true,反之则返回 false。 |
boolean or function(inputValue, option) |
true |
|
|
|
|
|
|
| optionLabelProp |
回填到选择框的 Option 的属性值,默认是 Option 的子元素。比如在子元素需要高亮效果时,此值可以设为 value。 |
string |
children |
|
|
|
|
|
|
| placeholder | 输入框提示 | string | slot | - | |
|---|---|---|---|---|
| v-model:value | 指定当前选中的条目 | string|string[]|{ key: string, label: string|vNodes }|Array<{ key: string, label: string|vNodes }> | 无 | |
|---|---|---|---|---|
| defaultOpen |
是否默认展开下拉菜单 |
boolean |
- |
|
|
|
|
|
|
事件
| change |
选中 option,或 input 的 value 变化时,调用此函数 |
function(value) |
|
|
|
|
|
| focus |
获得焦点时的回调 |
function() |
|
|
|
|
|
| search |
搜索补全项的时候调用 |
function(value) |
|
|
|
|
|
| select |
被选中时调用,参数为选中项的 value 值 |
function(value, option) |
|
|
|
|
|
| dropdownVisibleChange |
展开下拉菜单的回调 |
function(open) |
|
|
|
|
|
方法