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:
@@ -84,6 +84,10 @@ const disabled = useInheritedDisabled(
|
||||
);
|
||||
provide(disabledContextKey, disabled);
|
||||
const dir = computed(() => props.dir ?? config?.dir.value ?? "ltr");
|
||||
// Optional owner-driven view: filtering and virtual focus stay off unless the
|
||||
// composite asks for them, so standalone ListBox and Select are unchanged.
|
||||
const virtualFocus = computed(() => !!owner?.virtualFocus?.());
|
||||
const ownerFilter = computed(() => owner?.filter);
|
||||
const registry = useCollectionRegistry<ListBoxMember>();
|
||||
const mounted = ref(false);
|
||||
let disconnectOwner: (() => void) | undefined;
|
||||
@@ -92,6 +96,8 @@ onMounted(() => {
|
||||
if (owner)
|
||||
disconnectOwner = owner.connect({
|
||||
items: known,
|
||||
optionId,
|
||||
reveal,
|
||||
focus: async (edge) => {
|
||||
const available = eligible();
|
||||
const key =
|
||||
@@ -144,7 +150,16 @@ const known = () => {
|
||||
}))
|
||||
);
|
||||
};
|
||||
const eligible = () => known().filter((item) => !item.disabled);
|
||||
/** Full data set with the owner filter applied: display and navigation only. */
|
||||
const visibleKnown = () => {
|
||||
const filter = ownerFilter.value;
|
||||
const items = known();
|
||||
return filter ? items.filter((item) => filter(item)) : items;
|
||||
};
|
||||
const visibleKeys = computed(
|
||||
() => new Set(visibleKnown().map((item) => item.key)),
|
||||
);
|
||||
const eligible = () => visibleKnown().filter((item) => !item.disabled);
|
||||
const blockedKeys = computed(() =>
|
||||
known()
|
||||
.filter((item) => item.disabled)
|
||||
@@ -172,6 +187,7 @@ const lastFocused = ref<ListBoxKey>();
|
||||
const pressed = ref<ListBoxKey>();
|
||||
const keyboard = ref(true);
|
||||
const active = computed(() => {
|
||||
if (virtualFocus.value && owner?.activeKey) return owner.activeKey.value;
|
||||
if (!mounted.value) return undefined;
|
||||
const items = eligible();
|
||||
return (
|
||||
@@ -207,6 +223,37 @@ onBeforeUnmount(() => {
|
||||
disposing = true;
|
||||
focusRequest++;
|
||||
});
|
||||
/** Mounted DOM id of an option; undefined while a virtualized row is unmounted. */
|
||||
function optionId(key: ListBoxKey): string | undefined {
|
||||
const element = registry
|
||||
.ordered()
|
||||
.find((member) => member.key() === key)
|
||||
?.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);
|
||||
}
|
||||
/** 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;
|
||||
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();
|
||||
}
|
||||
async function focus(key: ListBoxKey) {
|
||||
if (
|
||||
disposing ||
|
||||
@@ -216,16 +263,12 @@ async function focus(key: ListBoxKey) {
|
||||
return;
|
||||
const request = ++focusRequest;
|
||||
lastFocused.value = key;
|
||||
if (props.virtualized) {
|
||||
const index = source.value.findIndex((item) => item.id === key);
|
||||
const rowTop = index * props.rowHeight;
|
||||
const element = root.value;
|
||||
if (
|
||||
element &&
|
||||
(rowTop < element.scrollTop ||
|
||||
rowTop + props.rowHeight > element.scrollTop + element.clientHeight)
|
||||
)
|
||||
scrollTo(index);
|
||||
scrollRowIntoView(key);
|
||||
if (virtualFocus.value) {
|
||||
if (owner?.activeKey && owner.activeKey.value !== key)
|
||||
owner.activeKey.value = key;
|
||||
await nextTick();
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
if (disposing || request !== focusRequest || disabled.value) return;
|
||||
@@ -274,6 +317,7 @@ let search = "",
|
||||
let pendingSpace: ListBoxKey | undefined;
|
||||
function keydown(event: KeyboardEvent) {
|
||||
if (
|
||||
virtualFocus.value ||
|
||||
event.defaultPrevented ||
|
||||
event.isComposing ||
|
||||
event.keyCode === 229 ||
|
||||
@@ -406,6 +450,7 @@ function keydown(event: KeyboardEvent) {
|
||||
}
|
||||
}
|
||||
function keyup(event: KeyboardEvent) {
|
||||
if (virtualFocus.value) return;
|
||||
if (event.key !== " ") return;
|
||||
const key = pendingSpace;
|
||||
pendingSpace = undefined;
|
||||
@@ -442,14 +487,18 @@ provide(listBoxKey, {
|
||||
activate,
|
||||
focus,
|
||||
virtualized: () => props.virtualized,
|
||||
virtual: () => virtualFocus.value,
|
||||
visible: (key) => visibleKeys.value.has(key),
|
||||
optionId,
|
||||
restoreFocus,
|
||||
position: (key) =>
|
||||
props.virtualized && data.value
|
||||
? {
|
||||
index: data.value.findIndex((item) => item.key === key) + 1,
|
||||
size: data.value.length,
|
||||
}
|
||||
: undefined,
|
||||
position: (key) => {
|
||||
if (!props.virtualized || !data.value) return undefined;
|
||||
const items = ownerFilter.value ? visibleKnown() : data.value;
|
||||
return {
|
||||
index: items.findIndex((item) => item.key === key) + 1,
|
||||
size: items.length,
|
||||
};
|
||||
},
|
||||
});
|
||||
function rootAttrs() {
|
||||
if (mode.value === "single" && selection.keys.value.length > 1)
|
||||
@@ -483,6 +532,7 @@ function rootAttrs() {
|
||||
"aria-multiselectable":
|
||||
mode.value === "none" ? undefined : mode.value === "multiple",
|
||||
tabindex:
|
||||
virtualFocus.value ||
|
||||
disabled.value ||
|
||||
(mounted.value &&
|
||||
registry.available().some((member) => member.key() === active.value))
|
||||
|
||||
Reference in New Issue
Block a user