fix(collection): close the six review gaps in filtering and virtual focus

- Keep the composite input focused on real pointer presses: cancel the default
  focus transfer for plain row presses only, leaving nested controls and already
  cancelled callers alone.
- Drive the virtual window, total height, reveal index and aria positions from
  the visible subset, and render the empty channel when filtering removes every
  row. Non-virtualized rows stay mounted and are hidden, so their text and
  accepted selection survive.
- Scroll mounted rows for reveal/focus outside virtualization, wait for a
  virtualized row to mount (frame-based, bounded), and drop reveal requests that
  filtering, closing or unmounting made obsolete.
- Stop overwriting caller `hidden`: mergeProps deletes an earlier key when a
  later value is undefined, so the filter now only adds `hidden` when the caller
  did not set it.
- Share keyboard modality through an optional `keyboardIntent` ref so a pointer
  press no longer leaves the active row without data-focus-visible.
- Give the verification harness instance-unique ids and derive
  aria-activedescendant from open state, mounted id and filter visibility.

Adds the reviewer probe plus eight regressions, and a virtualized harness
instance for the browser smoke of filtering plus window/height/positions.
This commit is contained in:
maoyingjie
2026-10-25 16:28:33 +08:00
parent 1b972a78ba
commit 50ad3ca180
9 changed files with 511 additions and 55 deletions
@@ -185,7 +185,8 @@ const selection = useCollectionSelection({
const focused = ref<ListBoxKey>();
const lastFocused = ref<ListBoxKey>();
const pressed = ref<ListBoxKey>();
const keyboard = ref(true);
// Pointer presses clear keyboard intent; the composite sets it again on keys.
const keyboard = owner?.keyboardIntent ?? ref(true);
const active = computed(() => {
if (virtualFocus.value && owner?.activeKey) return owner.activeKey.value;
if (!mounted.value) return undefined;
@@ -197,7 +198,18 @@ const active = computed(() => {
items[0]?.key
);
});
const source = computed(() => (props.items ? [...props.items] : []));
/**
* Items as the caller passed them, restricted to the visible subset. The full
* data set stays available through `known()` for text and accepted values.
*/
const visibleItems = computed(() => {
const items = props.items ? [...props.items] : [];
const filter = ownerFilter.value;
if (!items.length || !filter) return items;
const visible = visibleKeys.value;
return items.filter((item) => visible.has(item.id));
});
const source = computed(() => visibleItems.value);
// VueUse owns measurement, scroll range and cleanup; one logical collection
// remains available for select-all/typeahead even while most rows are unmounted.
const {
@@ -231,28 +243,54 @@ function optionId(key: ListBoxKey): string | undefined {
?.element();
return element?.id || undefined;
}
function scrollRowIntoView(key: ListBoxKey) {
if (!props.virtualized) return;
const index = source.value.findIndex((item) => item.id === key);
if (index < 0) return;
const rowTop = index * props.rowHeight;
const element = root.value;
if (
element &&
(rowTop < element.scrollTop ||
rowTop + props.rowHeight > element.scrollTop + element.clientHeight)
)
scrollTo(index);
const nextFrame = (): Promise<void> =>
typeof requestAnimationFrame === "function"
? new Promise((resolve) => requestAnimationFrame(() => resolve()))
: nextTick();
/** A virtualized row mounts after the window moves; wait for it, bounded. */
async function waitForMountedRow(key: ListBoxKey) {
for (let attempt = 0; attempt < 3; attempt++) {
if (registry.ordered().some((member) => member.key() === key)) return;
await nextFrame();
}
}
/**
* Bring a row into view without taking DOM focus. Virtualized lists move the
* window by index; ordinary lists scroll the mounted row (the row itself may
* sit inside any scroll container supplied by the caller).
*/
async function revealRow(key: ListBoxKey) {
if (props.virtualized) {
const index = source.value.findIndex((item) => item.id === key);
if (index < 0) return;
const rowTop = index * props.rowHeight;
const element = root.value;
if (
element &&
(rowTop < element.scrollTop ||
rowTop + props.rowHeight > element.scrollTop + element.clientHeight)
)
scrollTo(index);
await waitForMountedRow(key);
return;
}
await nextTick();
const element = registry
.ordered()
.find((member) => member.key() === key)
?.element();
element?.scrollIntoView?.({ block: "nearest" });
}
let revealRequest = 0;
/** Reveal a row without taking DOM focus (virtual focus / activedescendant). */
async function reveal(key: ListBoxKey) {
if (disposing || disabled.value) return;
if (!eligible().some((item) => item.key === key)) return;
const request = ++revealRequest;
lastFocused.value = key;
scrollRowIntoView(key);
// Wait one tick so a virtualized row is mounted before the consumer reads
// its element id for aria-activedescendant.
await nextTick();
await revealRow(key);
// A newer request, filtering, close or unmount makes this one obsolete.
if (disposing || request !== revealRequest) return;
}
async function focus(key: ListBoxKey) {
if (
@@ -263,14 +301,15 @@ async function focus(key: ListBoxKey) {
return;
const request = ++focusRequest;
lastFocused.value = key;
scrollRowIntoView(key);
if (virtualFocus.value) {
if (owner?.activeKey && owner.activeKey.value !== key)
owner.activeKey.value = key;
await nextTick();
const revealToken = ++revealRequest;
await revealRow(key);
if (disposing || revealToken !== revealRequest) return;
return;
}
await nextTick();
await revealRow(key);
if (disposing || request !== focusRequest || disabled.value) return;
const element = registry
.available()
@@ -493,9 +532,9 @@ provide(listBoxKey, {
restoreFocus,
position: (key) => {
if (!props.virtualized || !data.value) return undefined;
const items = ownerFilter.value ? visibleKnown() : data.value;
const items = visibleItems.value;
return {
index: items.findIndex((item) => item.key === key) + 1,
index: items.findIndex((item) => item.id === key) + 1,
size: items.length,
};
},
@@ -574,24 +613,28 @@ defineExpose({
<template>
<component :is="as" ref="root" v-bind="rootAttrs()">
<slot v-if="!items" :item="undefined!" :index="-1" />
<slot v-else-if="!items.length" name="empty" />
<div
v-else-if="virtualized"
v-bind="wrapperProps"
role="presentation"
data-slot="list-box-virtual-content"
>
<template v-else>
<!-- Filtered-empty still needs a visible channel; non-virtualized rows
keep their DOM and are only hidden by data-filtered. -->
<slot v-if="!visibleItems.length" name="empty" />
<div
v-for="entry in windowItems"
:key="entry.data.id"
v-if="virtualized"
v-bind="wrapperProps"
role="presentation"
:style="{ height: `${rowHeight}px` }"
data-slot="list-box-virtual-content"
>
<slot :item="entry.data" :index="entry.index" />
<div
v-for="entry in windowItems"
:key="entry.data.id"
role="presentation"
:style="{ height: `${rowHeight}px` }"
>
<slot :item="entry.data" :index="entry.index" />
</div>
</div>
</div>
<template v-else v-for="(item, index) in items" :key="item.id">
<slot :item="item" :index="index" />
<template v-else v-for="(item, index) in items" :key="item.id">
<slot :item="item" :index="index" />
</template>
</template>
</component>
</template>