3e9be13b19
The CSS port prefixed data-slot attribute values with hv-, so the compiled selectors never matched the DOM and the ComboBox trigger chevron never rotated. Only class names get the prefix; slot values stay byte-identical. Also commit typed text as the value when allowsCustomValue finds no record, restore the baseline search-field shell in all 24 Autocomplete demos, and correct both doc pages against the pinned upstream sources.
176 lines
3.8 KiB
Vue
176 lines
3.8 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>未找到结果</EmptyState>
|
||
</template>
|
||
</ListBox>
|
||
</AutocompleteFilter>
|
||
</AutocompletePopover>
|
||
</Autocomplete>
|
||
</template>
|