feat(autocomplete): migrate Autocomplete with popover-owned filtering

Port the fixed-baseline Autocomplete onto the B02a collection protocol. The
trigger is the single Tab entry; opening moves focus to the search box inside the
popover, which owns filtering, aria-activedescendant and keyboard navigation
while the list only tracks an active row.

- Seven parts: root (field, value/open models, form value, owner wiring),
  trigger (anchor + activation), value (scoped slot with selectedItems and
  selectedText instead of copied item subtrees), indicator, clear button,
  popover (measured trigger width), filter (owns the search text and composes the
  inner SearchFieldInput into the combobox control).
- Filter exposes input-value/update:inputValue/inputChange, matching ComboBox.
- Confirming the active row in multiple mode now toggles it instead of replacing
  the accepted set (ComboBox shared the same bug).
- ComboBox shows the accepted label on the first render via a display value that
  falls back to the selected record, so preselected fields are readable before
  the selection watcher runs (including SSR).
- Popovers measure the anchor with a ResizeObserver because Reka does not publish
  the anchor size; the caller's root class is merged again so the trigger fills
  the field width.
- 24 baseline previews, Chinese doc page, navigation, 17 behaviour tests, 5
  demo/document contracts and an independent tarball consumption script for both
  pickers.

Not included: the full bundler/NodeNext type matrix (B02d).
This commit is contained in:
maoyingjie
2026-10-25 17:20:17 +08:00
parent 46e4df7e85
commit 5e688680e9
53 changed files with 5730 additions and 11 deletions
@@ -0,0 +1,49 @@
<script setup lang="ts">
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
</script>
<template>
<Autocomplete
allows-empty-collection
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,153 @@
<!-- 适配:源示例请求 SWAPI,这里用本地异步源演示相同状态(取消旧请求、加载中、空状态)。 -->
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Spinner,
} from "@heroui-vue/components";
type Character = {
id: string;
name: string;
};
const SEARCH_DELAY = 300;
const characters: Character[] = [
{ id: "luke-skywalker", name: "卢克·天行者" },
{ id: "princess-leia", name: "莱娅公主" },
{ id: "han-solo", name: "汉·索罗" },
{ id: "darth-vader", name: "达斯·维达" },
{ id: "yoda", name: "尤达" },
{ id: "obi-wan-kenobi", name: "欧比旺·克诺比" },
{ id: "palpatine", name: "帕尔帕廷" },
{ id: "lando-calrissian", name: "兰多·卡瑞辛" },
{ id: "chewbacca", name: "楚巴卡" },
{ id: "r2-d2", name: "R2-D2" },
{ id: "c-3po", name: "C-3PO" },
{ id: "boba-fett", name: "波巴·费特" },
];
const query = ref("");
const results = ref<Character[]>([]);
const loading = ref(false);
let controller: AbortController | undefined;
function match(text: string) {
const search = text.trim().toLocaleLowerCase();
return characters.filter(
(character) =>
!search || character.name.toLocaleLowerCase().includes(search),
);
}
function runSearch(text: string) {
// 每次查询一个 AbortController:新输入会取消上一个请求,迟到的结果不再写回。
controller?.abort();
const current = new AbortController();
controller = current;
loading.value = true;
const timer = setTimeout(() => {
if (current.signal.aborted) return;
results.value = match(text);
if (controller === current) loading.value = false;
}, SEARCH_DELAY);
current.signal.addEventListener("abort", () => clearTimeout(timer), {
once: true,
});
}
// 受控文本:AutocompleteFilter 通过 input-value / input-change 暴露搜索文本。
function setQuery(value: string) {
query.value = value;
runSearch(value);
}
onMounted(() => runSearch(""));
onBeforeUnmount(() => {
controller?.abort();
controller = undefined;
});
// 结果集已由异步源过滤,集合不再按输入文本二次过滤。
const keepResults = () => true;
</script>
<template>
<Autocomplete
allows-empty-collection
class="w-[256px]"
placeholder="搜索…"
selection-mode="single"
>
<Label>搜索《星球大战》角色</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter
:filter="keepResults"
:input-value="query"
@input-change="setQuery"
>
<SearchField
auto-focus
aria-label="搜索角色"
class="sticky top-0 z-10"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索角色…" />
<Spinner
size="sm"
:class="[
'absolute end-2 top-1/2 -translate-y-1/2',
{ 'pointer-events-none opacity-0': !loading },
]"
/>
<SearchFieldClearButton
:class="{ 'pointer-events-none opacity-0': loading }"
/>
</SearchFieldGroup>
</SearchField>
<ListBox :items="results" class="max-h-[420px] overflow-y-auto">
<template #default="{ item }">
<ListBoxItem :id="item.id" :text-value="item.name">
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
</template>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,78 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
type TagKey,
} from "@heroui-vue/components";
const items = [
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "florida", name: "Florida" },
{ id: "new-york", name: "New York" },
{ id: "illinois", name: "Illinois" },
{ id: "pennsylvania", name: "Pennsylvania" },
];
const selected = ref<TagKey[]>(["california", "texas"]);
</script>
<template>
<div class="space-y-2">
<Autocomplete
v-model="selected"
class="w-[256px]"
placeholder="选择州"
selection-mode="multiple"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<p class="text-sm text-muted">
已选:{{ selected.length > 0 ? selected.join(", ") : "无" }}
</p>
</div>
</template>
@@ -0,0 +1,82 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Button,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const items = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const isOpen = ref(false);
</script>
<template>
<div class="space-y-4">
<Autocomplete
:is-open="isOpen"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
@open-change="isOpen = $event"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<Button @click="isOpen = !isOpen">
{{ isOpen ? "关闭" : "打开" }} 自动完成
</Button>
<p class="text-sm text-muted">
自动完成处于{{ isOpen ? "打开" : "关闭" }}状态
</p>
</div>
</template>
@@ -0,0 +1,79 @@
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const states = [
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "florida", name: "Florida" },
{ id: "new-york", name: "New York" },
{ id: "illinois", name: "Illinois" },
{ id: "pennsylvania", name: "Pennsylvania" },
];
const state = ref<string | null>("california");
const selectedState = computed(() =>
states.find((item) => item.id === state.value),
);
</script>
<template>
<div class="space-y-2">
<Autocomplete
v-model="state"
class="w-[256px]"
placeholder="选择一个州"
selection-mode="single"
>
<Label>州(受控)</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in states"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<p class="text-sm text-muted">已选:{{ selectedState?.name || "无" }}</p>
</div>
</template>
@@ -0,0 +1,75 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
import { IconSelector } from "@tabler/icons-vue";
const items = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const selectedKey = ref<string | null>(null);
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator>
<IconSelector class="size-3" aria-hidden="true" />
</AutocompleteIndicator>
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,126 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const teammates = [
{ id: "sarah", name: "Sarah Chen", role: "产品设计" },
{ id: "marcus", name: "Marcus Lee", role: "工程" },
{ id: "priya", name: "Priya Patel", role: "数据科学" },
{ id: "jordan", name: "Jordan Kim", role: "客户成功" },
{ id: "alex", name: "Alex Rivera", role: "市场营销" },
];
const selectedKeys = ref<TagKey[]>([]);
const teammateOf = (key: unknown) =>
teammates.find((teammate) => teammate.id === key);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<div class="w-full max-w-sm pt-6">
<Autocomplete
v-model="selectedKeys"
class="w-full gap-1.5"
placeholder="搜索团队成员..."
selection-mode="multiple"
>
<Label class="font-medium text-foreground">负责人</Label>
<Description>任务更新时将通知以下人员。</Description>
<AutocompleteTrigger
class="rounded-xl border border-border/80 bg-surface shadow-sm ring-1 ring-accent/5 transition-[box-shadow,border-color] focus-within:border-accent/25 focus-within:ring-2 focus-within:ring-accent/15 dark:border-border/90 dark:bg-surface dark:ring-accent/10 dark:focus-within:border-accent/35"
>
<AutocompleteValue>
<template
#default="{ defaultChildren, isPlaceholder, selectedItems }"
>
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="teammateOf(item.key)?.name"
class="border border-accent/15 bg-accent/8 text-foreground dark:border-accent/25 dark:bg-accent/12"
>
{{ teammateOf(item.key)?.name }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover
class="overflow-hidden rounded-xl border border-border/80 bg-surface shadow-xl ring-1 ring-black/3 dark:border-border/90 dark:bg-surface dark:ring-white/5"
>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="按姓名或角色搜索"
name="search"
variant="secondary"
>
<SearchFieldGroup
class="border-b border-separator bg-surface-secondary/50"
>
<SearchFieldSearchIcon class="text-muted" />
<SearchFieldInput placeholder="按姓名或角色搜索..." />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox class="p-1.5">
<ListBoxItem
v-for="teammate in teammates"
:id="teammate.id"
:key="teammate.id"
:text-value="`${teammate.name} ${teammate.role}`"
class="rounded-lg data-[focused=true]:bg-accent/10 data-[selected=true]:bg-accent/5 dark:data-[focused=true]:bg-accent/15 dark:data-[selected=true]:bg-accent/8"
>
<div class="flex flex-col gap-0.5">
<span class="text-sm font-medium text-foreground">
{{ teammate.name }}
</span>
<span class="text-xs text-muted">{{ teammate.role }}</span>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到团队成员</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</div>
</template>
@@ -0,0 +1,98 @@
<script setup lang="ts">
import {
Autocomplete,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const currencies = [
{ code: "USD", id: "usd", name: "美元", symbol: "$" },
{ code: "EUR", id: "eur", name: "欧元", symbol: "€" },
{ code: "GBP", id: "gbp", name: "英镑", symbol: "£" },
{ code: "JPY", id: "jpy", name: "日元", symbol: "¥" },
{ code: "CHF", id: "chf", name: "瑞士法郎", symbol: "₣" },
];
const currencyOf = (key: unknown) =>
currencies.find((currency) => currency.id === key);
</script>
<template>
<Autocomplete
class="w-[256px]"
default-value="usd"
placeholder="选择货币"
selection-mode="single"
>
<Label>货币</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<span
v-if="isPlaceholder || !currencyOf(selectedItems[0]?.key)"
class="flex min-w-0 items-center gap-1.5"
>
{{ defaultChildren }}
</span>
<span v-else class="flex min-w-0 items-center gap-1.5">
<span class="font-medium">
{{ currencyOf(selectedItems[0]?.key)?.symbol }}
</span>
<span>{{ currencyOf(selectedItems[0]?.key)?.code }}</span>
<span class="truncate text-muted">
{{ currencyOf(selectedItems[0]?.key)?.name }}
</span>
</span>
</template>
</AutocompleteValue>
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索货币"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索货币…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="currency in currencies"
:id="currency.id"
:key="currency.id"
:text-value="`${currency.code} ${currency.name}`"
>
<div class="flex flex-col">
<Label>{{ currency.code }}</Label>
<Description>{{ currency.name }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
+97
View File
@@ -0,0 +1,97 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const states = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const selectedKeys = ref<TagKey[]>([]);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<Autocomplete
v-model="selectedKeys"
class="w-[256px]"
placeholder="选择州/省"
selection-mode="multiple"
>
<Label>计划前往的州</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索选项">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="state in states"
:id="state.id"
:key="state.id"
:text-value="state.name"
>
{{ state.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
+119
View File
@@ -0,0 +1,119 @@
<script setup lang="ts">
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const items = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const countries = [
{ id: "argentina", name: "Argentina" },
{ id: "venezuela", name: "Venezuela" },
{ id: "japan", name: "Japan" },
{ id: "france", name: "France" },
{ id: "italy", name: "Italy" },
{ id: "spain", name: "Spain" },
];
</script>
<template>
<div class="flex flex-col gap-4">
<Autocomplete
is-disabled
class="w-[256px]"
default-value="california"
placeholder="请选择一项"
selection-mode="single"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<Autocomplete
is-disabled
class="w-[256px]"
:default-value="['argentina', 'japan', 'france']"
placeholder="选择国家"
selection-mode="multiple"
>
<Label>计划前往的国家</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索国家">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索国家…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="country in countries"
:id="country.id"
:key="country.id"
:text-value="country.name"
>
{{ country.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</div>
</template>
@@ -0,0 +1,116 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const emails = [
{
email: "alice@example.com",
id: "alice@example.com",
name: "Alice Johnson",
},
{ email: "bob@example.com", id: "bob@example.com", name: "Bob Smith" },
{
email: "charlie@example.com",
id: "charlie@example.com",
name: "Charlie Brown",
},
{ email: "diana@example.com", id: "diana@example.com", name: "Diana Prince" },
{ email: "eve@example.com", id: "eve@example.com", name: "Eve Wilson" },
];
const selectedKeys = ref<TagKey[]>([]);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<Autocomplete
v-model="selectedKeys"
class="w-[256px]"
placeholder="添加收件人"
selection-mode="multiple"
>
<Label>收件人</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索邮箱"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索邮箱…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="entry in emails"
:id="entry.id"
:key="entry.id"
:text-value="entry.email"
>
<div class="flex flex-col">
<Label>{{ entry.name }}</Label>
<Description>{{ entry.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到收件人</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,76 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Surface,
} from "@heroui-vue/components";
const items = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const selectedKey = ref<string | null>(null);
</script>
<template>
<Surface class="w-[380px] space-y-4 rounded-3xl p-6">
<Autocomplete
v-model="selectedKey"
full-width
placeholder="请选择一项"
selection-mode="single"
variant="secondary"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</Surface>
</template>
@@ -0,0 +1,114 @@
<script setup lang="ts">
import { onBeforeUnmount, ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
type City = {
name: string;
country: string;
};
const allCities: City[] = [
{ country: "美国", name: "纽约" },
{ country: "美国", name: "洛杉矶" },
{ country: "美国", name: "芝加哥" },
{ country: "英国", name: "伦敦" },
{ country: "法国", name: "巴黎" },
{ country: "日本", name: "东京" },
{ country: "澳大利亚", name: "悉尼" },
{ country: "加拿大", name: "多伦多" },
{ country: "德国", name: "柏林" },
{ country: "西班牙", name: "马德里" },
];
const selectedKey = ref<string | null>(null);
const loading = ref(false);
let timer: ReturnType<typeof setTimeout> | undefined;
const contains = (text: string, input: string) =>
text.toLocaleLowerCase().includes(input.toLocaleLowerCase());
// 源示例在过滤回调里模拟异步搜索:先标记加载中,300ms 后结束。
function customFilter(text: string, input: string) {
if (!input) return true;
loading.value = true;
clearTimeout(timer);
timer = setTimeout(() => {
loading.value = false;
}, 300);
return contains(text, input);
}
onBeforeUnmount(() => clearTimeout(timer));
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="搜索城市"
selection-mode="single"
>
<Label>城市</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter :filter="customFilter">
<SearchField
auto-focus
aria-label="搜索城市"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索城市…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="city in allCities"
:id="city.name"
:key="city.name"
:text-value="`${city.name} ${city.country}`"
>
<div class="flex flex-col">
<Label>{{ city.name }}</Label>
<Description>{{ city.country }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">
{{ loading ? "搜索中…" : "未找到城市" }}
</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,98 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const states = [
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "florida", name: "Florida" },
{ id: "new-york", name: "New York" },
{ id: "illinois", name: "Illinois" },
{ id: "pennsylvania", name: "Pennsylvania" },
];
const selectedKeys = ref<TagKey[]>([]);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<Autocomplete
v-model="selectedKeys"
class="w-[256px]"
placeholder="选择州/省"
selection-mode="multiple"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索选项">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="state in states"
:id="state.id"
:key="state.id"
:text-value="state.name"
>
{{ state.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,83 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Surface,
} from "@heroui-vue/components";
const items = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const selectedKey = ref<string | null>(null);
</script>
<template>
<Surface class="w-[320px] rounded-3xl p-6">
<Autocomplete
v-model="selectedKey"
class="w-full"
placeholder="选择一个"
selection-mode="single"
variant="secondary"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索州名"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</Surface>
</template>
+137
View File
@@ -0,0 +1,137 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Button,
EmptyState,
FieldError,
Form,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const states = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const countries = [
{ id: "usa", name: "United States" },
{ id: "canada", name: "Canada" },
{ id: "mexico", name: "Mexico" },
{ id: "uk", name: "United Kingdom" },
{ id: "france", name: "France" },
{ id: "germany", name: "Germany" },
];
const message = ref<string | null>(null);
function onSubmit(event: Event) {
event.preventDefault();
const formData = new FormData(event.currentTarget as HTMLFormElement);
message.value = `表单提交成功!州:${formData.get("state")},国家:${formData.get("country")}`;
}
</script>
<template>
<Form class="flex w-[256px] flex-col gap-4" @submit="onSubmit">
<Autocomplete
is-required
class="w-full"
name="state"
placeholder="请选择一项"
selection-mode="single"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="state in states"
:id="state.id"
:key="state.id"
:text-value="state.name"
>
{{ state.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
<FieldError />
</Autocomplete>
<Autocomplete
is-required
class="w-full"
name="country"
placeholder="选择一个国家"
selection-mode="single"
>
<Label>国家</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索国家">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索国家…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="country in countries"
:id="country.id"
:key="country.id"
:text-value="country.name"
>
{{ country.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
<FieldError />
</Autocomplete>
<Button type="submit">提交</Button>
<p v-if="message" role="status" class="text-sm text-success">
{{ message }}
</p>
</Form>
</template>
@@ -0,0 +1,107 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const tags = [
{ id: "react", name: "React" },
{ id: "typescript", name: "TypeScript" },
{ id: "javascript", name: "JavaScript" },
{ id: "nodejs", name: "Node.js" },
{ id: "python", name: "Python" },
{ id: "vue", name: "Vue" },
{ id: "angular", name: "Angular" },
{ id: "nextjs", name: "Next.js" },
];
const selectedKeys = ref<TagKey[]>([]);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<Autocomplete
v-model="selectedKeys"
class="w-[256px]"
placeholder="选择标签"
selection-mode="multiple"
>
<Label>标签</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索标签"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索标签…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="tag in tags"
:id="tag.id"
:key="tag.id"
:text-value="tag.name"
>
{{ tag.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到标签</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,158 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Avatar,
AvatarFallback,
AvatarImage,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
fallback: "B",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
fallback: "F",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
fallback: "M",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
{
id: "4",
name: "John",
email: "john@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
},
{
id: "5",
name: "Jane",
email: "jane@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
},
];
const selectedKeys = ref<TagKey[]>(["1", "2"]);
const userOf = (key: unknown) => users.find((user) => user.id === key);
function onRemoveTags(keys: TagKey[]) {
selectedKeys.value = selectedKeys.value.filter((key) => !keys.includes(key));
}
</script>
<template>
<Autocomplete
v-model="selectedKeys"
class="w-[256px]"
placeholder="选择队友"
selection-mode="multiple"
>
<Label>用户</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
<Avatar class="size-4" size="sm">
<AvatarImage :src="userOf(item.key)?.avatarUrl ?? ''" />
<AvatarFallback>
{{ userOf(item.key)?.fallback }}
</AvatarFallback>
</Avatar>
<span>{{ item.textValue }}</span>
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索用户"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索用户…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="user in users"
:id="user.id"
:key="user.id"
:text-value="user.name"
>
<Avatar size="sm">
<AvatarImage :src="user.avatarUrl" />
<AvatarFallback>{{ user.fallback }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,146 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Avatar,
AvatarFallback,
AvatarImage,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const users = [
{
id: "1",
name: "Bob",
email: "bob@heroui.com",
fallback: "B",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/blue.jpg",
},
{
id: "2",
name: "Fred",
email: "fred@heroui.com",
fallback: "F",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/green.jpg",
},
{
id: "3",
name: "Martha",
email: "martha@heroui.com",
fallback: "M",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/purple.jpg",
},
{
id: "4",
name: "John",
email: "john@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/red.jpg",
},
{
id: "5",
name: "Jane",
email: "jane@heroui.com",
fallback: "J",
avatarUrl:
"https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/avatars/orange.jpg",
},
];
const selectedKey = ref<string | null>(null);
const userOf = (key: unknown) => users.find((user) => user.id === key);
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="选择用户"
selection-mode="single"
>
<Label>用户</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template #default="{ defaultChildren, isPlaceholder, selectedItems }">
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<template v-else-if="selectedItems.length > 1">
已选择 {{ selectedItems.length }} 位用户
</template>
<div v-else class="flex items-center gap-2">
<Avatar class="size-4" size="sm">
<AvatarImage
:src="userOf(selectedItems[0]?.key)?.avatarUrl ?? ''"
/>
<AvatarFallback>
{{ userOf(selectedItems[0]?.key)?.fallback }}
</AvatarFallback>
</Avatar>
<span>{{ userOf(selectedItems[0]?.key)?.name }}</span>
</div>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField
auto-focus
aria-label="搜索用户"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索用户…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="user in users"
:id="user.id"
:key="user.id"
:text-value="user.name"
>
<Avatar size="sm">
<AvatarImage :src="user.avatarUrl" />
<AvatarFallback>{{ user.fallback }}</AvatarFallback>
</Avatar>
<div class="flex flex-col">
<Label>{{ user.name }}</Label>
<Description>{{ user.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
+261
View File
@@ -0,0 +1,261 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Tag,
TagGroup,
TagGroupList,
type TagKey,
} from "@heroui-vue/components";
const items = [
{ id: "option1", name: "选项 1" },
{ id: "option2", name: "选项 2" },
{ id: "option3", name: "选项 3" },
{ id: "option4", name: "选项 4" },
];
const selectedKey1 = ref<string | null>(null);
const selectedKey2 = ref<string | null>(null);
const selectedKeys1 = ref<TagKey[]>([]);
const selectedKeys2 = ref<TagKey[]>([]);
function onRemoveTags1(keys: TagKey[]) {
selectedKeys1.value = selectedKeys1.value.filter(
(key) => !keys.includes(key),
);
}
function onRemoveTags2(keys: TagKey[]) {
selectedKeys2.value = selectedKeys2.value.filter(
(key) => !keys.includes(key),
);
}
</script>
<template>
<div class="flex flex-col gap-8">
<div class="flex flex-col gap-4">
<h3 class="text-lg font-semibold">单选变体</h3>
<div class="flex flex-col gap-4">
<Autocomplete
v-model="selectedKey1"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
variant="primary"
>
<Label>主色(primary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索选项">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<Autocomplete
v-model="selectedKey2"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
variant="secondary"
>
<Label>次色(secondary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索选项">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</div>
</div>
<div class="flex flex-col gap-4">
<h3 class="text-lg font-semibold">多选变体</h3>
<div class="flex flex-col gap-4">
<Autocomplete
v-model="selectedKeys1"
class="w-[256px]"
placeholder="选择多项"
selection-mode="multiple"
variant="primary"
>
<Label>主色(primary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template
#default="{ defaultChildren, isPlaceholder, selectedItems }"
>
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup v-else size="sm" @remove="onRemoveTags1">
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索选项">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
<Autocomplete
v-model="selectedKeys2"
class="w-[256px]"
placeholder="选择多项"
selection-mode="multiple"
variant="secondary"
>
<Label>次色(secondary)变体</Label>
<AutocompleteTrigger>
<AutocompleteValue>
<template
#default="{ defaultChildren, isPlaceholder, selectedItems }"
>
<template v-if="isPlaceholder || selectedItems.length === 0">
{{ defaultChildren }}
</template>
<TagGroup
v-else
size="sm"
variant="surface"
@remove="onRemoveTags2"
>
<TagGroupList>
<Tag
v-for="item in selectedItems"
:id="item.key"
:key="item.key"
:text-value="item.textValue"
>
{{ item.textValue }}
</Tag>
</TagGroupList>
</TagGroup>
</template>
</AutocompleteValue>
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索选项">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</div>
</div>
</div>
</template>
@@ -0,0 +1,175 @@
<!-- 适配:源示例用 Virtualizer + ListLayout 包裹 ListBox,本库 ListBox 直接提供 items + virtualized + row-height 虚拟滚动。 -->
<script setup lang="ts">
import { computed, ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
type ListBoxKey,
} from "@heroui-vue/components";
type User = {
id: number;
name: string;
email: string;
};
const firstNames = [
"Emma",
"Liam",
"Olivia",
"Noah",
"Ava",
"James",
"Sophia",
"Oliver",
"Isabella",
"Lucas",
"Mia",
"Ethan",
"Charlotte",
"Mason",
"Amelia",
"Logan",
"Harper",
"Alexander",
"Ella",
"Benjamin",
];
const lastNames = [
"Smith",
"Johnson",
"Williams",
"Brown",
"Jones",
"Garcia",
"Miller",
"Davis",
"Rodriguez",
"Martinez",
"Anderson",
"Taylor",
"Thomas",
"Jackson",
"White",
"Harris",
"Clark",
"Lewis",
"Robinson",
"Walker",
];
function generateUsers(count: number): User[] {
const users: User[] = [];
for (let index = 0; index < count; index++) {
const first = firstNames[index % firstNames.length]!;
const last =
lastNames[Math.floor(index / firstNames.length) % lastNames.length]!;
users.push({
id: index + 1,
name: `${first} ${last}`,
email: `${first.toLowerCase()}.${last.toLowerCase()}@acme.com`,
});
}
return users;
}
const allUsers = generateUsers(1000);
const selectedKey = ref<ListBoxKey | null>(null);
const searchQuery = ref("");
const filteredUsers = computed(() => {
const query = searchQuery.value.trim().toLocaleLowerCase();
if (!query) return allUsers;
return allUsers.filter(
(user) =>
user.name.toLocaleLowerCase().includes(query) ||
user.email.toLocaleLowerCase().includes(query),
);
});
function setSearchQuery(value: string) {
searchQuery.value = value;
}
// 过滤已在上面的 computed 中完成(姓名或邮箱),集合不再叠加文本过滤。
const keepResults = () => true;
</script>
<template>
<Autocomplete
v-model="selectedKey"
allows-empty-collection
class="w-[300px]"
placeholder="选择用户"
selection-mode="single"
>
<Label>用户</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter
:filter="keepResults"
:input-value="searchQuery"
@input-change="setSearchQuery"
>
<SearchField
auto-focus
aria-label="搜索用户"
class="sticky top-0 z-10"
name="search"
variant="secondary"
>
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索用户…" />
<SearchFieldClearButton />
</SearchFieldGroup>
</SearchField>
<ListBox
:items="filteredUsers"
class="max-h-[320px] overflow-y-auto"
:row-height="50"
virtualized
>
<template #default="{ item }">
<ListBoxItem :id="item.id" :text-value="item.name" class="h-full">
<div class="flex flex-col">
<Label>{{ item.name }}</Label>
<Description>{{ item.email }}</Description>
</div>
<ListBoxItemIndicator />
</ListBoxItem>
</template>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,74 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const items = [
{ id: "florida", name: "Florida" },
{ id: "delaware", name: "Delaware" },
{ id: "california", name: "California" },
{ id: "texas", name: "Texas" },
{ id: "new-york", name: "New York" },
{ id: "washington", name: "Washington" },
];
const selectedKey = ref<string | null>(null);
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="请选择一项"
selection-mode="single"
>
<Label>州</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索州名">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索州名…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="item in items"
:id="item.id"
:key="item.id"
:text-value="item.name"
>
{{ item.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
<Description>请选择你的居住州</Description>
</Autocomplete>
</template>
@@ -0,0 +1,73 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
} from "@heroui-vue/components";
const animals = [
{ id: "dog", name: "狗" },
{ id: "cat", name: "猫" },
{ id: "bird", name: "鸟" },
{ id: "kangaroo", name: "袋鼠" },
{ id: "elephant", name: "象" },
{ id: "tiger", name: "老虎" },
];
const selectedKey = ref<string | null>(null);
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
:disabled-keys="['cat', 'kangaroo']"
placeholder="选择一种动物"
selection-mode="single"
>
<Label>动物</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索动物">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索动物…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxItem
v-for="animal in animals"
:id="animal.id"
:key="animal.id"
:text-value="animal.name"
>
{{ animal.name }}
<ListBoxItemIndicator />
</ListBoxItem>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>
@@ -0,0 +1,116 @@
<script setup lang="ts">
import { ref } from "vue";
import {
Autocomplete,
AutocompleteClearButton,
AutocompleteFilter,
AutocompleteIndicator,
AutocompletePopover,
AutocompleteTrigger,
AutocompleteValue,
EmptyState,
Header,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
ListBoxSection,
SearchField,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
Separator,
} from "@heroui-vue/components";
const selectedKey = ref<string | null>(null);
</script>
<template>
<Autocomplete
v-model="selectedKey"
class="w-[256px]"
placeholder="选择一个国家"
selection-mode="single"
>
<Label>国家</Label>
<AutocompleteTrigger>
<AutocompleteValue />
<AutocompleteClearButton />
<AutocompleteIndicator />
</AutocompleteTrigger>
<AutocompletePopover>
<AutocompleteFilter>
<SearchField aria-label="搜索国家">
<SearchFieldGroup>
<SearchFieldSearchIcon />
<SearchFieldInput placeholder="搜索国家…" />
</SearchFieldGroup>
</SearchField>
<ListBox>
<ListBoxSection>
<Header>北美洲</Header>
<ListBoxItem id="usa" text-value="美国">
美国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="canada" text-value="加拿大">
加拿大
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="mexico" text-value="墨西哥">
墨西哥
<ListBoxItemIndicator />
</ListBoxItem>
</ListBoxSection>
<Separator />
<ListBoxSection>
<Header>欧洲</Header>
<ListBoxItem id="uk" text-value="英国">
英国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="france" text-value="法国">
法国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="germany" text-value="德国">
德国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="spain" text-value="西班牙">
西班牙
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="italy" text-value="意大利">
意大利
<ListBoxItemIndicator />
</ListBoxItem>
</ListBoxSection>
<Separator />
<ListBoxSection>
<Header>亚洲</Header>
<ListBoxItem id="japan" text-value="日本">
日本
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="china" text-value="中国">
中国
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="india" text-value="印度">
印度
<ListBoxItemIndicator />
</ListBoxItem>
<ListBoxItem id="south-korea" text-value="韩国">
韩国
<ListBoxItemIndicator />
</ListBoxItem>
</ListBoxSection>
<template #empty>
<EmptyState class="p-1">未找到结果</EmptyState>
</template>
</ListBox>
</AutocompleteFilter>
</AutocompletePopover>
</Autocomplete>
</template>