AutoComplete自动完成

AutoComplete 自动完成

输入框自动完成功能。

何时使用

  • 需要一个输入框而不是选择器。

  • 需要输入建议/辅助提示。

和 Sel​ect 的区别是:

  • AutoComplete 是一个带提示的文本输入框,用户可以自由输入,关键词是辅助输入。

  • Sel​ect 是在限定的可选项中进行选择,关键词是选择。

代码演示

基本使用

基本使用。通过 options 设置自动完成的数据源。

<​template>  <​a-auto-complete    v-model:value="value"    :options="options"    style="width: 200px"    placeholder="input here"    @sel​ect="onSelect"    @search="onSearch"  /><​/template><​script lang="ts">import { defineComponent, ref, watch } from 'vue';interface MockVal {  value: string;}const mockVal = (str: string, repeat = 1): MockVal => {  return {    value: str.repeat(repeat),  };};export default defineComponent({  setup() {    const value = ref('');    const options = ref<​MockVal[]>([]);    const onSearch = (searchText: string) => {      console.log('searchText');      options.value = !searchText        ? []        : [mockVal(searchText), mockVal(searchText, 2), mockVal(searchText, 3)];    };    const onSelect = (value: string) => {      console.log('onSelect', value);    };    watch(value, () => {      console.log('value', value.value);    });    return {      value,      options,      onSearch,      onSelect,    };  },});<​/script>

自定义输入组件

自定义输入组件。

<​template>  <​a-auto-complete    v-model:value="value"    :options="options"    style="width: 200px"    @search="handleSearch"    @sel​ect="onSelect"  >    <​a-textarea      placeholder="input here"      class="custom"      style="height: 50px"      @keypress="handleKeyPress"    />  <​/a-auto-complete><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref('');    const options = ref<{ value: string }[]>([]);    const onSelect = (value: string) => {      console.log('onSelect', value);    };    const handleSearch = (value: string) => {      options.value = !value        ? []        : [{ value }, { value: value + value }, { value: value + value + value }];    };    const handleKeyPress = (ev: KeyboardEvent) => {      console.log('handleKeyPress', ev);    };    return {      value,      options,      onSelect,      handleSearch,      handleKeyPress,    };  },});<​/script>

查询模式 - 确定类目

查询模式 - 确定类目。

<​template>  <​div class="certain-category-search-wrapper" style="width: 250px">    <​a-auto-complete      v-model:value="value"      class="certain-category-search"      dropdown-class-name="certain-category-search-dropdown"      :dropdown-match-sel​ect-width="false"      :dropdown-style="{ width: '300px' }"      size="large"      style="width: 100%"      placeholder="input here"      option-label-prop="value"    >      <​template #dataSource>        <​a-sel​ect-opt-group v-for="group in dataSource" :key="group.title">          <​template #label>            <​span>              {{ group.title }}              <​a                style="float: right"                href="https://www.google.com/search?q=antd"                target="_blank"                rel="noopener noreferrer"              >                more              <​/a>            <​/span>          <​/template>          <​a-sel​ect-option v-for="opt in group.children" :key="opt.title" :value="opt.title">            {{ opt.title }}            <​span class="certain-search-item-count">{{ opt.count }} people<​/span>          <​/a-sel​ect-option>        <​/a-sel​ect-opt-group>        <​a-sel​ect-option key="all" disabled class="show-all">          <​a            href="https://www.google.com/search?q=ant-design-vue"            target="_blank"            rel="noopener noreferrer"          >            View all results          <​/a>        <​/a-sel​ect-option>      <​/template>      <​a-input>        <​template #suffix><​search-outlined class="certain-category-icon" /><​/template>      <​/a-input>    <​/a-auto-complete>  <​/div><​/template><​script lang="ts">import { SearchOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';const dataSource = [  {    title: 'Libraries',    children: [      {        title: 'AntDesign',        count: 10000,      },      {        title: 'AntDesign UI',        count: 10600,      },    ],  },  {    title: 'Solutions',    children: [      {        title: 'AntDesign UI FAQ',        count: 60100,      },      {        title: 'AntDesign FAQ',        count: 30010,      },    ],  },  {    title: 'Articles',    children: [      {        title: 'AntDesign design language',        count: 100000,      },    ],  },];export default defineComponent({  setup() {    return {      value: ref(''),      dataSource,    };  },  components: {    SearchOutlined,  },});<​/script><​style>.certain-category-search-dropdown .ant-sel​ect-dropdown-menu-item-group-title {  color: #666;  font-weight: bold;}.certain-category-search-dropdown .ant-sel​ect-dropdown-menu-item-group {  border-bottom: 1px solid #f6f6f6;}.certain-category-search-dropdown .ant-sel​ect-dropdown-menu-item {  padding-left: 16px;}.certain-category-search-dropdown .ant-sel​ect-dropdown-menu-item.show-all {  text-align: center;  cursor: default;}.certain-category-search-dropdown .ant-sel​ect-dropdown-menu {  max-height: 300px;}<​/style><​style scoped>.certain-category-search-wrapper  :deep(.certain-category-search.ant-sel​ect-auto-complete)  .ant-input-affix-wrapper  .ant-input-suffix {  right: 12px;}.certain-category-search-wrapper :deep(.certain-search-item-count) {  position: absolute;  color: #999;  right: 16px;}.certain-category-search-wrapper  :deep(.certain-category-search.ant-sel​ect-focused)  .certain-category-icon {  color: #108ee9;}.certain-category-search-wrapper :deep(.certain-category-icon) {  color: #6e6e6e;  transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);  font-size: 16px;}<​/style>

自定义选项

也可以直接传递 #dataSource 的Option。

<​template>  <​a-auto-complete    v-model:value="value"    style="width: 200px"    placeholder="input here"    @search="handleSearch"  >    <​template #dataSource>      <​a-sel​ect-option v-for="email in result" :key="email">        {{ email }}      <​/a-sel​ect-option>    <​/template>  <​/a-auto-complete><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref('');    const result = ref<​string[]>([]);    const handleSearch = (val: string) => {      let res: string[];      if (!val || val.indexOf('@') >= 0) {        res = [];      } else {        res = ['gmail.com', '163.com', 'qq.com'].map(domain => `${val}@${domain}`);      }      result.value = res;    };    return {      value,      result,      handleSearch,    };  },});<​/script>

不区分大小写

不区分大小写的 AutoComplete。

<​template>  <​a-auto-complete    v-model:value="value"    :options="options"    style="width: 200px"    placeholder="input here"    :filter-option="filterOption"  /><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';interface Option {  value: string;}export default defineComponent({  setup() {    const filterOption = (input: string, option: Option) => {      return option.value.toUpperCase().indexOf(input.toUpperCase()) >= 0;    };    return {      value: ref(''),      options: ref<​Option[]>([        { value: 'Burns Bay Road' },        { value: 'Downing Street' },        { value: 'Wall Street' },      ]),      filterOption,    };  },});<​/script>

查询模式 - 不确定类目

查询模式 - 不确定类目。