Files
heroui-vue/apps/docs/demos/autocomplete-virtualization.vue
maoyingjie 9e8d89e48d feat(collections): virtual row layout with variable heights, headings, loader and gap/padding
ListBox virtualized mode grows a full row stream (padding spacers, data-driven
heading rows, item rows, #loader row) with px semantics mapped from the pinned
ListLayout contract: rowHeight may be a per-index function falling back to
estimatedRowHeight, gap sits between content rows only, padding is end
spacers, and every input is re-read per layout pass. Scroll ownership resolves
to the list root or the nearest scrolling ancestor so popovers scroll as a
whole with a sticky search field (data-virtualized hook; the historical 320px
self-scroll rule stays for non-virtualized lists).

Table gains the public useVirtualRows/bindVirtualRows pure geometry helpers
(prefix sums + binary search, no state kernel): variable heights, estimated
fallback, gap/padding and a loader reservation that stays consistent with the
rendered spacers. headingHeight and dropIndicatorThickness remain unavailable
(no sectioned header model, no drag-and-drop) and are recorded as such.

Pinned demo values stay explicit (table 42px, list-box/autocomplete 50px);
the table demo scrolls an outer viewport div because a native table cannot be
a scroll container (verified: 300px height blown out to 45036px).
2026-10-29 16:21:09 +08:00

180 lines
4.2 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!-- 适配:源示例用 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,
createBaseContains,
Description,
EmptyState,
Label,
ListBox,
ListBoxItem,
ListBoxItemIndicator,
SearchField,
SearchFieldClearButton,
SearchFieldGroup,
SearchFieldInput,
SearchFieldSearchIcon,
useLocale,
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("");
// 与 AutocompleteFilter 默认谓词共用同一实现(locale 敏感、大小写与变音折叠,
// 例如 cafe 能匹配 Café);本示例自行过滤以驱动虚拟滚动,故关闭集合二次过滤。
const { locale } = useLocale();
const contains = computed(() => createBaseContains(locale.value));
const filteredUsers = computed(() => {
const query = searchQuery.value.trim();
if (!query) return allUsers;
return allUsers.filter(
(user) =>
contains.value(user.name, query) || contains.value(user.email, 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>
<!-- The popover scrolls as a whole (pinned layout): the virtual
window binds to that scrolling ancestor, keeping the search
field sticky at its top. -->
<ListBox :items="filteredUsers" :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>