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
+81 -16
View File
@@ -4,7 +4,15 @@
* owner protocol (filter + virtual focus). It proves the protocol; it is NOT
* the public ComboBox.
*/
import { computed, nextTick, provide, ref, shallowRef } from "vue";
import {
computed,
nextTick,
provide,
ref,
shallowRef,
useId,
watchEffect,
} from "vue";
import { ListBox, ListBoxItem } from "@heroui-vue/components";
import {
collectionOwnerKey,
@@ -26,6 +34,8 @@ const props = withDefaults(
/** When false the parent rejects every selection proposal. */
acceptSelection?: boolean;
filterEnabled?: boolean;
/** Render the collection through ListBox's virtualized items mode. */
virtualized?: boolean;
}>(),
{
options: () => [
@@ -37,6 +47,7 @@ const props = withDefaults(
],
acceptSelection: true,
filterEnabled: true,
virtualized: false,
},
);
@@ -45,8 +56,11 @@ const emit = defineEmits<{
proposal: [key: CollectionKey | null];
}>();
const labelId = "b02a-label";
const listId = "b02a-list";
// Instance-unique ids: several harnesses coexist on the verification page.
const uid = useId();
const labelId = `${uid}-label`;
const inputId = `${uid}-input`;
const listId = `${uid}-list`;
const input = ref<HTMLInputElement>();
const inputValue = ref("");
const accepted = ref<CollectionKey | null>(null);
@@ -54,6 +68,8 @@ const activeKey = ref<CollectionKey>();
const activeId = ref<string>();
const open = ref(false);
const composing = ref(false);
/** Shared modality: ListBox clears it on pointer presses, keys restore it. */
const keyboardIntent = ref(true);
const collection = shallowRef<OwnedCollection>();
const records = computed<CollectionRecord[]>(() =>
@@ -108,9 +124,9 @@ const owner: CollectionOwner = {
labelledBy: () => labelId,
ariaLabel: () => undefined,
request,
action: () => {
open.value = false;
},
// The harness stays open after a pick so the active-key/id sync is observable;
// closing behaviour belongs to the real ComboBox.
action: () => {},
leave: () => {
open.value = false;
},
@@ -123,18 +139,42 @@ const owner: CollectionOwner = {
filter: filterRecord,
virtualFocus: () => true,
activeKey,
keyboardIntent,
};
provide(collectionOwnerKey, owner);
/**
* The id is only usable when the list is open and the row is mounted, visible
* and not filtered out; otherwise the attribute must disappear.
*/
function resolveActiveId(): string | undefined {
if (!open.value) return undefined;
const key = activeKey.value;
if (key === undefined) return undefined;
const id = collection.value?.optionId?.(key);
if (!id) return undefined;
const element =
typeof document === "undefined" ? null : document.getElementById(id);
if (!element || element.hasAttribute("data-filtered")) return undefined;
return id;
}
// One derivation for every path that can move the active key: keyboard,
// pointer clicks inside the list, filtering and open/close.
watchEffect(() => {
void eligible.value.length;
void collection.value;
activeId.value = resolveActiveId();
});
async function setActive(key: CollectionKey | undefined) {
activeKey.value = key;
if (key === undefined) {
activeId.value = undefined;
return;
if (key !== undefined) {
// Reveal first (mounts a virtualized row); the effect above re-reads the id
// once the row is actually mounted.
await collection.value?.reveal?.(key);
}
// Reveal first (mounts a virtualized row), then read the mounted id.
await collection.value?.reveal?.(key);
activeId.value = collection.value?.optionId?.(key);
activeId.value = resolveActiveId();
}
function moveActive(delta: 1 | -1) {
@@ -161,6 +201,7 @@ function onInput() {
function keydown(event: KeyboardEvent) {
// IME: never confirm while composing (Safari reports keyCode 229).
if (event.isComposing || composing.value || event.keyCode === 229) return;
keyboardIntent.value = true;
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
if (!open.value) open.value = true;
@@ -199,18 +240,18 @@ defineExpose({
<template>
<div class="flex w-[256px] flex-col gap-1">
<label :id="labelId" class="text-sm font-medium" for="b02a-input">
<label :id="labelId" class="text-sm font-medium" :for="inputId">
动物(B02a 协议验证)
</label>
<input
id="b02a-input"
:id="inputId"
ref="input"
:value="inputValue"
aria-autocomplete="list"
:aria-activedescendant="activeId"
:aria-controls="listId"
:aria-expanded="open"
aria-labelledby="b02a-label"
:aria-labelledby="labelId"
autocomplete="off"
class="rounded-field border border-field-border bg-field px-3 py-2 text-sm"
data-test="harness-input"
@@ -228,7 +269,28 @@ defineExpose({
已隐藏:{{ hiddenCount }}
</p>
<div v-show="open" data-test="popover">
<ListBox :id="listId">
<ListBox
v-if="virtualized"
:id="listId"
:items="options"
:row-height="32"
:virtualized="true"
class="max-h-[128px] overflow-y-auto"
>
<template #default="{ item }">
<ListBoxItem
:id="item.id"
:key="item.id"
:is-disabled="item.disabled"
>
{{ item.text }}
</ListBoxItem>
</template>
<template #empty>
<p data-test="empty">没有匹配项</p>
</template>
</ListBox>
<ListBox v-else :id="listId">
<ListBoxItem
v-for="option in options"
:id="option.id"
@@ -237,6 +299,9 @@ defineExpose({
>
{{ option.text }}
</ListBoxItem>
<template #empty>
<p data-test="empty">没有匹配项</p>
</template>
</ListBox>
</div>
</div>