395 lines
12 KiB
Vue
395 lines
12 KiB
Vue
<script setup lang="ts">
|
||
import {
|
||
computed,
|
||
defineComponent,
|
||
h,
|
||
onMounted,
|
||
onUnmounted,
|
||
ref,
|
||
type PropType,
|
||
} from "vue";
|
||
import {
|
||
Button,
|
||
Description,
|
||
Header,
|
||
Input,
|
||
Label,
|
||
ListBox,
|
||
ListBoxItem,
|
||
ListBoxItemIndicator,
|
||
Table,
|
||
TableBody,
|
||
TableCell,
|
||
TableColumn,
|
||
TableContent,
|
||
TableHeader,
|
||
TableRow,
|
||
TableScrollContainer,
|
||
bindVirtualRows,
|
||
useVirtualRows,
|
||
} from "@heroui-vue/components";
|
||
|
||
/**
|
||
* HD-09 virtual layout harness: variable row heights, heading rows, loader
|
||
* rows, gap/padding, container resizing, scroll positioning and keyboard
|
||
* navigation across unmounted windows — with selection identity kept over
|
||
* filtering and appends.
|
||
*/
|
||
const headingHeight = 32;
|
||
const fallbackRow = 48;
|
||
const rowHeight = (index: number) =>
|
||
index % 3 === 0 ? 40 : index % 3 === 1 ? 64 : 80;
|
||
|
||
const query = ref("");
|
||
const allItems = ref(
|
||
Array.from({ length: 800 }, (_, index) => ({
|
||
id: index + 1,
|
||
textValue: `条目 ${index + 1}`,
|
||
heading: index % 5 === 0 ? `分组 ${Math.floor(index / 5) + 1}` : undefined,
|
||
})),
|
||
);
|
||
const filteredItems = computed(() => {
|
||
const text = query.value.trim();
|
||
if (!text) return allItems.value;
|
||
return allItems.value.filter((item) => item.textValue.includes(text));
|
||
});
|
||
const selected = ref<(string | number)[]>([]);
|
||
const listHeight = ref(320);
|
||
const loading = ref(false);
|
||
const estimatedRows = ref(false);
|
||
const resolvedRowHeight = (index: number) =>
|
||
estimatedRows.value ? undefined : rowHeight(index);
|
||
const mixedItems = ref([
|
||
{ id: 0, textValue: "数字零" },
|
||
{ id: "0", textValue: "字符串零" },
|
||
]);
|
||
const delayedItems = ref<{ id: number; textValue: string }[]>([]);
|
||
const StatefulItem = defineComponent({
|
||
props: {
|
||
item: {
|
||
type: Object as PropType<{ id: string | number; textValue: string }>,
|
||
required: true,
|
||
},
|
||
},
|
||
setup(props) {
|
||
const born = `${typeof props.item.id}:${props.item.id}`;
|
||
return () =>
|
||
h(
|
||
ListBoxItem,
|
||
{ id: props.item.id, textValue: props.item.textValue },
|
||
() => `当前 ${typeof props.item.id}:${props.item.id} / 创建时 ${born}`,
|
||
);
|
||
},
|
||
});
|
||
function appendItems() {
|
||
loading.value = true;
|
||
setTimeout(() => {
|
||
const start = allItems.value.length;
|
||
allItems.value = [
|
||
...allItems.value,
|
||
...Array.from({ length: 100 }, (_, index) => ({
|
||
id: start + index + 1,
|
||
textValue: `条目 ${start + index + 1}`,
|
||
heading:
|
||
(start + index) % 5 === 0
|
||
? `分组 ${Math.floor((start + index) / 5) + 1}`
|
||
: undefined,
|
||
})),
|
||
];
|
||
loading.value = false;
|
||
}, 300);
|
||
}
|
||
function selectFarItem() {
|
||
selected.value = [allItems.value[600]?.id ?? 601];
|
||
listboxRef.value?.focus(selected.value[0] as never);
|
||
}
|
||
const listboxRef = ref<{ focus: (key?: never) => void } | null>(null);
|
||
|
||
// ---- variable-height table window ----
|
||
const tableCount = ref(300);
|
||
const unknownTableRows = ref(false);
|
||
const tableWidth = ref(800);
|
||
const tableRowHeight = (index: number) =>
|
||
unknownTableRows.value ? undefined : index % 2 ? 44 : 72;
|
||
const tableRows = useVirtualRows({
|
||
count: () => tableCount.value,
|
||
keys: () => Array.from({ length: tableCount.value }, (_, i) => i + 1),
|
||
rowHeight: tableRowHeight,
|
||
estimatedRowHeight: fallbackRow,
|
||
loaderHeight: 40,
|
||
gap: 4,
|
||
padding: 12,
|
||
overscan: 6,
|
||
});
|
||
const tableWindow = tableRows.window;
|
||
const tableItems = computed(() =>
|
||
Array.from({ length: tableCount.value }, (_, index) => ({
|
||
id: index + 1,
|
||
name: `行 ${index + 1}`,
|
||
})),
|
||
);
|
||
const visibleTableRows = computed(() =>
|
||
tableItems.value.slice(tableWindow.value.start, tableWindow.value.end),
|
||
);
|
||
const scrollRef = ref<HTMLElement | null>(null);
|
||
const scrollElement = () => scrollRef.value;
|
||
function jumpToRow(index: number) {
|
||
const element = scrollElement();
|
||
if (element) tableRows.scrollToIndex(element, index);
|
||
}
|
||
let detach: (() => void) | undefined;
|
||
onMounted(() => {
|
||
const element = scrollElement();
|
||
if (element)
|
||
detach = bindVirtualRows(tableRows, element, {
|
||
content: element.querySelector("tbody") ?? undefined,
|
||
});
|
||
});
|
||
onUnmounted(() => detach?.());
|
||
</script>
|
||
|
||
<template>
|
||
<main style="padding: 24px; display: grid; gap: 24px">
|
||
<section style="display: grid; gap: 8px">
|
||
<h2>可变行高 / 分组 / 加载行 / gap / padding</h2>
|
||
<div style="display: flex; gap: 8px; align-items: center">
|
||
<Label for="virtual-filter">过滤</Label>
|
||
<Input
|
||
id="virtual-filter"
|
||
:model-value="query"
|
||
placeholder="按文本过滤…"
|
||
@update:model-value="query = $event"
|
||
/>
|
||
<Button
|
||
size="sm"
|
||
variant="secondary"
|
||
@click="listHeight = listHeight === 320 ? 520 : 320"
|
||
>
|
||
容器高度 {{ listHeight }}
|
||
</Button>
|
||
<Button size="sm" variant="secondary" @click="appendItems">
|
||
追加 100 项
|
||
</Button>
|
||
<Button size="sm" variant="secondary" @click="selectFarItem">
|
||
选中并定位第 601 项
|
||
</Button>
|
||
<Button
|
||
size="sm"
|
||
variant="secondary"
|
||
@click="estimatedRows = !estimatedRows"
|
||
>
|
||
切换未知行高
|
||
</Button>
|
||
</div>
|
||
<ListBox
|
||
id="virtual-listbox"
|
||
ref="listboxRef"
|
||
:items="filteredItems"
|
||
v-model="selected"
|
||
selection-mode="multiple"
|
||
virtualized
|
||
:row-height="resolvedRowHeight"
|
||
:estimated-row-height="fallbackRow"
|
||
:heading-height="estimatedRows ? () => undefined : headingHeight"
|
||
:estimated-heading-height="32"
|
||
:loader-height="40"
|
||
:gap="8"
|
||
:padding="12"
|
||
:style="{
|
||
height: listHeight + 'px',
|
||
width: '320px',
|
||
overflowY: 'auto',
|
||
}"
|
||
aria-label="虚拟行验证列表"
|
||
>
|
||
<template #default="{ item }">
|
||
<ListBoxItem
|
||
:id="item.id"
|
||
:text-value="item.textValue"
|
||
:style="{ height: estimatedRows ? 'auto' : '100%' }"
|
||
>
|
||
<div style="display: flex; flex-direction: column">
|
||
<Label>{{ item.textValue }}</Label>
|
||
<Description>高度随索引变化</Description>
|
||
<Description v-if="estimatedRows">
|
||
{{ "这是一段需要自动换行的未知高度内容。".repeat(6) }}
|
||
</Description>
|
||
</div>
|
||
<ListBoxItemIndicator />
|
||
</ListBoxItem>
|
||
</template>
|
||
<template #loader>
|
||
<div
|
||
data-slot="virtual-loader"
|
||
style="display: flex; align-items: center; justify-content: center"
|
||
>
|
||
<Button size="sm" variant="ghost" @click="appendItems">
|
||
加载更多…
|
||
</Button>
|
||
</div>
|
||
</template>
|
||
</ListBox>
|
||
<p data-probe-selected>已选:{{ selected.join(",") || "无" }}</p>
|
||
</section>
|
||
<section>
|
||
<h2>不同类型 key 的局部状态</h2>
|
||
<Button @click="mixedItems = [...mixedItems].reverse()">
|
||
反转零标识
|
||
</Button>
|
||
<ListBox
|
||
:items="mixedItems"
|
||
virtualized
|
||
:row-height="50"
|
||
:overscan="5"
|
||
style="height: 160px; overflow-y: auto"
|
||
aria-label="零标识列表"
|
||
>
|
||
<template #default="{ item }"><StatefulItem :item="item" /></template>
|
||
</ListBox>
|
||
</section>
|
||
|
||
<section>
|
||
<Button
|
||
@click="
|
||
delayedItems = Array.from({ length: 200 }, (_, i) => ({
|
||
id: i + 1,
|
||
textValue: `延后条目 ${i + 1}`,
|
||
}))
|
||
"
|
||
>
|
||
加载父容器列表
|
||
</Button>
|
||
<div
|
||
data-probe-late-viewport
|
||
style="height: 200px; width: 320px; overflow-y: auto"
|
||
>
|
||
<div style="height: 48px; position: sticky; top: 0">固定搜索区域</div>
|
||
<ListBox
|
||
:items="delayedItems"
|
||
virtualized
|
||
:row-height="50"
|
||
aria-label="延后加载列表"
|
||
>
|
||
<template #default="{ item }">
|
||
<ListBoxItem
|
||
:id="item.id"
|
||
:text-value="item.textValue"
|
||
style="height: 50px"
|
||
>
|
||
{{ item.textValue }}
|
||
</ListBoxItem>
|
||
</template>
|
||
</ListBox>
|
||
</div>
|
||
</section>
|
||
<section style="display: grid; gap: 8px">
|
||
<h2>表格虚拟窗口(useVirtualRows)</h2>
|
||
<div style="display: flex; gap: 8px">
|
||
<Button @click="unknownTableRows = !unknownTableRows">
|
||
切换表格未知行高
|
||
</Button>
|
||
<Button @click="tableWidth = tableWidth === 800 ? 520 : 800">
|
||
表格宽度 {{ tableWidth }}
|
||
</Button>
|
||
<Button size="sm" variant="secondary" @click="jumpToRow(149)">
|
||
定位第 150 行
|
||
</Button>
|
||
<Button
|
||
size="sm"
|
||
variant="secondary"
|
||
@click="tableCount = tableCount === 300 ? 600 : 300"
|
||
>
|
||
行数 {{ tableCount }}
|
||
</Button>
|
||
</div>
|
||
<Table>
|
||
<TableScrollContainer>
|
||
<div
|
||
ref="scrollRef"
|
||
data-slot="table-virtual-viewport"
|
||
:style="{
|
||
height: '300px',
|
||
width: `${tableWidth}px`,
|
||
overflow: 'auto',
|
||
}"
|
||
>
|
||
<TableContent
|
||
aria-label="可变行高表格"
|
||
style="min-width: 520px; table-layout: fixed"
|
||
:aria-rowcount="tableCount + 1"
|
||
:row-keys="tableItems.map((row) => row.id)"
|
||
@focus-row="(_key, index) => jumpToRow(index)"
|
||
>
|
||
<TableHeader aria-rowindex="1">
|
||
<TableColumn id="name" is-row-header :min-width="160">
|
||
名称
|
||
</TableColumn>
|
||
<TableColumn id="info" :min-width="200">说明</TableColumn>
|
||
</TableHeader>
|
||
<TableBody>
|
||
<tr
|
||
aria-hidden="true"
|
||
:style="{ height: tableWindow.padTop + 'px' }"
|
||
/>
|
||
<template v-for="row in visibleTableRows" :key="row.id">
|
||
<TableRow
|
||
:row-key="row.id"
|
||
:aria-rowindex="row.id + 1"
|
||
:ref="(node) => tableRows.measureRow(row.id - 1, node)"
|
||
:height="tableRowHeight(row.id - 1)"
|
||
>
|
||
<TableCell>{{ row.name }}</TableCell>
|
||
<TableCell>
|
||
{{
|
||
unknownTableRows
|
||
? "需要测量的表格内容,宽度改变后会重新换行。".repeat(
|
||
10,
|
||
)
|
||
: "行高 44/72 交替"
|
||
}}
|
||
</TableCell>
|
||
</TableRow>
|
||
<tr
|
||
v-if="row.id < tableCount"
|
||
aria-hidden="true"
|
||
style="height: 4px"
|
||
/>
|
||
</template>
|
||
<tr
|
||
v-if="tableWindow.end < tableCount"
|
||
aria-hidden="true"
|
||
:style="{ height: tableWindow.padBottom + 'px' }"
|
||
/>
|
||
<template v-else>
|
||
<tr
|
||
v-if="tableCount"
|
||
aria-hidden="true"
|
||
style="height: 4px"
|
||
/>
|
||
<tr role="presentation">
|
||
<td colspan="2" style="padding: 0">
|
||
<div
|
||
role="status"
|
||
data-probe-table-loader
|
||
style="
|
||
height: 40px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
"
|
||
>
|
||
加载更多…
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
<tr aria-hidden="true" style="height: 12px" />
|
||
</template>
|
||
</TableBody>
|
||
</TableContent>
|
||
</div>
|
||
</TableScrollContainer>
|
||
</Table>
|
||
</section>
|
||
</main>
|
||
</template>
|