fix(list-box): preserve typed virtual keys and add HD-09 review repros
This commit is contained in:
@@ -24,7 +24,7 @@ import { useInheritedDisabled, disabledContextKey } from "../field/disabled";
|
||||
import { useCollectionSelection } from "../collection/selection";
|
||||
import { useCollectionRegistry } from "../collection/registry";
|
||||
import { collectionOwnerKey } from "../collection/owner";
|
||||
import { assertKey } from "../collection/keys";
|
||||
import { assertKey, encodeKey } from "../collection/keys";
|
||||
import {
|
||||
listBoxKey,
|
||||
type ListBoxDataItem,
|
||||
@@ -284,7 +284,7 @@ const virtualRows = computed<VirtualRow[]>(() => {
|
||||
(typeof props.rowHeight === "function"
|
||||
? props.rowHeight(index)
|
||||
: props.rowHeight) ?? fallbackRowHeight.value;
|
||||
push({ key: `item-${item.id}`, kind: "item", height, item, index });
|
||||
push({ key: `item-${encodeKey(item.id)}`, kind: "item", height, item, index });
|
||||
});
|
||||
if (slots.loader)
|
||||
push({ key: "loader", kind: "loader", height: props.loaderHeight });
|
||||
|
||||
Vendored
+96
-3
@@ -1,5 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
ref,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import {
|
||||
Button,
|
||||
Description,
|
||||
@@ -48,6 +56,31 @@ const filteredItems = computed(() => {
|
||||
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(() => {
|
||||
@@ -133,6 +166,13 @@ onUnmounted(() => detach?.());
|
||||
<Button size="sm" variant="secondary" @click="selectFarItem">
|
||||
选中并定位第 601 项
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
@click="estimatedRows = !estimatedRows"
|
||||
>
|
||||
切换未知行高
|
||||
</Button>
|
||||
</div>
|
||||
<ListBox
|
||||
id="virtual-listbox"
|
||||
@@ -141,7 +181,7 @@ onUnmounted(() => detach?.());
|
||||
v-model="selected"
|
||||
selection-mode="multiple"
|
||||
virtualized
|
||||
:row-height="rowHeight"
|
||||
:row-height="resolvedRowHeight"
|
||||
:estimated-row-height="fallbackRow"
|
||||
:heading-height="headingHeight"
|
||||
:loader-height="40"
|
||||
@@ -158,11 +198,14 @@ onUnmounted(() => detach?.());
|
||||
<ListBoxItem
|
||||
:id="item.id"
|
||||
:text-value="item.textValue"
|
||||
style="height: 100%"
|
||||
: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>
|
||||
@@ -180,7 +223,57 @@ onUnmounted(() => detach?.());
|
||||
</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">
|
||||
|
||||
@@ -274,3 +274,42 @@ describe("ListBox virtual row stream", () => {
|
||||
expect(option.getAttribute("aria-setsize")).toBe("40");
|
||||
});
|
||||
});
|
||||
|
||||
it("review: numeric and string keys preserve local row identity on reorder", async () => {
|
||||
const Stateful = defineComponent({
|
||||
props: {
|
||||
item: {
|
||||
type: Object as PropType<{ id: string | number }>,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
setup(props) {
|
||||
const born = `${typeof props.item.id}:${props.item.id}`;
|
||||
return () =>
|
||||
h(
|
||||
ListBoxItem,
|
||||
{ id: props.item.id },
|
||||
() => `${typeof props.item.id}:${props.item.id}/${born}`,
|
||||
);
|
||||
},
|
||||
});
|
||||
const items = [{ id: 0 }, { id: "0" }];
|
||||
const w = mount(ListBox, {
|
||||
props: { items, virtualized: true, rowHeight: 50 },
|
||||
slots: {
|
||||
default: ({ item }: { item: { id: string | number } }) =>
|
||||
h(Stateful, { item }),
|
||||
},
|
||||
attachTo: document.body,
|
||||
});
|
||||
wrappers.push(w);
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
await w.setProps({ items: [...items].reverse() });
|
||||
await nextTick();
|
||||
await nextTick();
|
||||
expect(w.findAll('[role="option"]').map((x) => x.text())).toEqual([
|
||||
"string:0/string:0",
|
||||
"number:0/number:0",
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user