5e688680e9
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).
176 lines
3.9 KiB
Vue
176 lines
3.9 KiB
Vue
<!-- 适配:源示例用 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>
|