feat(collection): add optional filtering and virtual focus for composite pickers

ComboBox and Autocomplete need two capabilities the collection protocol did not
express: filtering a collection by an external input value, and keeping DOM focus
in the composite's input while an active option is tracked for
aria-activedescendant.

Both are opt-in members on CollectionOwner plus optionId/reveal on the owned
collection. Standalone ListBox and Select provide none of them and keep their
previous behaviour: DOM focus still moves into the list, one roving tab stop, no
row filtering.

Filtered rows stay mounted with data-filtered (display:none) so their text and
accepted selection survive; only display, aria positions and keyboard navigation
see the visible subset. Under virtual focus the list never takes DOM focus,
never exposes a roving tab stop, and leaves keyboard handling to the composite;
reveal() scrolls a virtualized window and waits for the row to mount before the
composite reads its id.

Includes the playground verification page, a ten-case suite and the B02a section
of the migration report.
This commit is contained in:
maoyingjie
2026-10-25 14:53:49 +08:00
parent fa73cafd20
commit 1b972a78ba
11 changed files with 849 additions and 30 deletions
+243
View File
@@ -0,0 +1,243 @@
<script setup lang="ts">
/**
* B02a verification harness: a minimal combobox that consumes the optional
* owner protocol (filter + virtual focus). It proves the protocol; it is NOT
* the public ComboBox.
*/
import { computed, nextTick, provide, ref, shallowRef } from "vue";
import { ListBox, ListBoxItem } from "@heroui-vue/components";
import {
collectionOwnerKey,
type CollectionOwner,
type CollectionRecord,
type OwnedCollection,
} from "../../src/components/collection/owner";
import type { CollectionKey } from "../../src/components/collection/keys";
interface Option {
id: CollectionKey;
text: string;
disabled?: boolean;
}
const props = withDefaults(
defineProps<{
options?: readonly Option[];
/** When false the parent rejects every selection proposal. */
acceptSelection?: boolean;
filterEnabled?: boolean;
}>(),
{
options: () => [
{ id: "cat", text: "猫" },
{ id: "dog", text: "狗" },
{ id: "bird", text: "鸟" },
{ id: "fish", text: "鱼" },
{ id: "hamster", text: "仓鼠" },
],
acceptSelection: true,
filterEnabled: true,
},
);
const emit = defineEmits<{
"update:modelValue": [key: CollectionKey | null];
proposal: [key: CollectionKey | null];
}>();
const labelId = "b02a-label";
const listId = "b02a-list";
const input = ref<HTMLInputElement>();
const inputValue = ref("");
const accepted = ref<CollectionKey | null>(null);
const activeKey = ref<CollectionKey>();
const activeId = ref<string>();
const open = ref(false);
const composing = ref(false);
const collection = shallowRef<OwnedCollection>();
const records = computed<CollectionRecord[]>(() =>
props.options.map((option) => ({
key: option.id,
text: option.text,
disabled: !!option.disabled,
})),
);
/** Filtering affects display and navigation only; accepted values stay. */
function filterRecord(record: CollectionRecord) {
if (!props.filterEnabled) return true;
const needle = inputValue.value.trim();
return !needle || record.text.includes(needle);
}
const eligible = computed(() =>
records.value.filter((record) => !record.disabled && filterRecord(record)),
);
/** Read from the full data set: a filtered-out selection keeps its label. */
const acceptedText = computed(
() => records.value.find((item) => item.key === accepted.value)?.text ?? null,
);
const hiddenCount = computed(
() => records.value.filter((record) => !filterRecord(record)).length,
);
function request(keys: CollectionKey[]) {
const next = keys[0] ?? null;
emit("proposal", next);
if (!props.acceptSelection) return;
accepted.value = next;
const record = records.value.find((item) => item.key === next);
if (record) inputValue.value = record.text;
emit("update:modelValue", next);
}
const owner: CollectionOwner = {
keys: computed(() => (accepted.value === null ? [] : [accepted.value])),
mode: computed(() => "single" as const),
disabled: computed(() => false),
readOnly: computed(() => false),
disabledKeys: computed(() =>
records.value
.filter((record) => record.disabled)
.map((record) => record.key),
),
id: listId,
labelledBy: () => labelId,
ariaLabel: () => undefined,
request,
action: () => {
open.value = false;
},
leave: () => {
open.value = false;
},
connect: (next: OwnedCollection) => {
collection.value = next;
return () => {
if (collection.value === next) collection.value = undefined;
};
},
filter: filterRecord,
virtualFocus: () => true,
activeKey,
};
provide(collectionOwnerKey, owner);
async function setActive(key: CollectionKey | undefined) {
activeKey.value = key;
if (key === undefined) {
activeId.value = undefined;
return;
}
// Reveal first (mounts a virtualized row), then read the mounted id.
await collection.value?.reveal?.(key);
activeId.value = collection.value?.optionId?.(key);
}
function moveActive(delta: 1 | -1) {
const items = eligible.value;
if (!items.length) {
void setActive(undefined);
return;
}
const index = items.findIndex((item) => item.key === activeKey.value);
const next =
index < 0
? items[delta > 0 ? 0 : items.length - 1]
: items[(index + delta + items.length) % items.length];
void setActive(next.key);
}
function onInput() {
if (composing.value) return;
inputValue.value = input.value?.value ?? "";
open.value = true;
void setActive(eligible.value[0]?.key);
}
function keydown(event: KeyboardEvent) {
// IME: never confirm while composing (Safari reports keyCode 229).
if (event.isComposing || composing.value || event.keyCode === 229) return;
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
if (!open.value) open.value = true;
moveActive(event.key === "ArrowDown" ? 1 : -1);
return;
}
if (event.key === "Enter") {
event.preventDefault();
if (activeKey.value !== undefined) request([activeKey.value]);
open.value = false;
return;
}
if (event.key === "Escape" || event.key === "Tab") open.value = false;
}
function compositionStart() {
composing.value = true;
}
function compositionEnd() {
composing.value = false;
onInput();
void nextTick();
}
defineExpose({
inputValue,
accepted,
activeKey,
activeId,
open,
eligible: () => eligible.value,
hiddenCount: () => hiddenCount.value,
});
</script>
<template>
<div class="flex w-[256px] flex-col gap-1">
<label :id="labelId" class="text-sm font-medium" for="b02a-input">
动物(B02a 协议验证)
</label>
<input
id="b02a-input"
ref="input"
:value="inputValue"
aria-autocomplete="list"
:aria-activedescendant="activeId"
:aria-controls="listId"
:aria-expanded="open"
aria-labelledby="b02a-label"
autocomplete="off"
class="rounded-field border border-field-border bg-field px-3 py-2 text-sm"
data-test="harness-input"
role="combobox"
type="text"
@compositionend="compositionEnd"
@compositionstart="compositionStart"
@input="onInput"
@keydown="keydown"
/>
<p class="text-xs text-muted" data-test="accepted">
已选:{{ acceptedText ?? "无" }}
</p>
<p class="text-xs text-muted" data-test="hidden-count">
已隐藏:{{ hiddenCount }}
</p>
<div v-show="open" data-test="popover">
<ListBox :id="listId">
<ListBoxItem
v-for="option in options"
:id="option.id"
:key="option.id"
:is-disabled="option.disabled"
>
{{ option.text }}
</ListBoxItem>
</ListBox>
</div>
</div>
</template>