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:
@@ -1,16 +1,35 @@
|
||||
import type { ComputedRef, InjectionKey } from "vue";
|
||||
import type { ComputedRef, InjectionKey, Ref } from "vue";
|
||||
import type { CollectionKey } from "./keys";
|
||||
export interface CollectionRecord {
|
||||
key: CollectionKey;
|
||||
text: string;
|
||||
disabled: boolean;
|
||||
}
|
||||
/** Filtering and virtual focus are optional: absent means the previous behaviour. */
|
||||
export interface CollectionView {
|
||||
/**
|
||||
* Filter over the full data set. Filtered-out records stay mounted and keep
|
||||
* their text/selection, but leave display and keyboard navigation.
|
||||
*/
|
||||
filter?: (record: CollectionRecord) => boolean;
|
||||
/** Keep DOM focus outside the collection and track `activeKey` instead. */
|
||||
virtualFocus?: () => boolean;
|
||||
/** Active key for virtual focus; the owning composite drives it. */
|
||||
activeKey?: Ref<CollectionKey | undefined>;
|
||||
}
|
||||
export interface OwnedCollection {
|
||||
items: () => CollectionRecord[];
|
||||
focus: (edge: "first" | "last" | "selected") => Promise<void>;
|
||||
/** Mounted DOM id of an option, for `aria-activedescendant`. */
|
||||
optionId?: (key: CollectionKey) => string | undefined;
|
||||
/**
|
||||
* Make `key` the revealed row: scroll a virtualized window first, then wait
|
||||
* for the row to mount. Never moves DOM focus.
|
||||
*/
|
||||
reveal?: (key: CollectionKey) => Promise<void>;
|
||||
}
|
||||
/** A field composite may own a ListBox, without adding another value store. */
|
||||
export interface CollectionOwner {
|
||||
export interface CollectionOwner extends CollectionView {
|
||||
keys: ComputedRef<readonly CollectionKey[]>;
|
||||
mode: ComputedRef<"single" | "multiple">;
|
||||
disabled: ComputedRef<boolean>;
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -73,10 +73,16 @@ export default defineComponent({
|
||||
const state = () => ({
|
||||
isSelected: context.selected(props.id),
|
||||
isDisabled: disabled.value,
|
||||
isFocused: context.focused.value === props.id,
|
||||
isFocusVisible:
|
||||
context.focused.value === props.id &&
|
||||
(context.keyboard.value || !!element.value?.matches(":focus-visible")),
|
||||
// Under virtual focus the DOM focus stays in the composite's input, so the
|
||||
// active key is what marks the row as focused.
|
||||
isFocused: context.virtual()
|
||||
? context.active.value === props.id
|
||||
: context.focused.value === props.id,
|
||||
isFocusVisible: context.virtual()
|
||||
? context.keyboard.value && context.active.value === props.id
|
||||
: context.focused.value === props.id &&
|
||||
(context.keyboard.value ||
|
||||
!!element.value?.matches(":focus-visible")),
|
||||
isPressed:
|
||||
(pressed.value || context.pressed.value === props.id) &&
|
||||
!disabled.value,
|
||||
@@ -149,6 +155,7 @@ export default defineComponent({
|
||||
return () => {
|
||||
const current = state();
|
||||
const position = context.position(props.id);
|
||||
const visible = context.visible(props.id);
|
||||
const children = text.prepare(slots.default?.(current) ?? [], true);
|
||||
return h(
|
||||
props.as,
|
||||
@@ -160,7 +167,12 @@ export default defineComponent({
|
||||
variant: props.variant,
|
||||
}).item(),
|
||||
tabindex:
|
||||
!disabled.value && context.active.value === props.id ? 0 : -1,
|
||||
!disabled.value &&
|
||||
!context.virtual() &&
|
||||
context.active.value === props.id
|
||||
? 0
|
||||
: -1,
|
||||
hidden: visible ? undefined : true,
|
||||
"aria-selected":
|
||||
context.mode.value === "none" ? undefined : current.isSelected,
|
||||
"aria-disabled": disabled.value || undefined,
|
||||
@@ -174,6 +186,7 @@ export default defineComponent({
|
||||
text.describedBy(),
|
||||
),
|
||||
"data-slot": "list-box-item",
|
||||
"data-filtered": visible ? undefined : true,
|
||||
"data-disabled": disabled.value || undefined,
|
||||
"data-selected": current.isSelected || undefined,
|
||||
"data-focused": current.isFocused || undefined,
|
||||
|
||||
@@ -56,6 +56,11 @@ export interface ListBoxContext {
|
||||
activate: (key: ListBoxKey, event: MouseEvent | KeyboardEvent) => void;
|
||||
focus: (key: ListBoxKey) => Promise<void>;
|
||||
virtualized: () => boolean;
|
||||
/** Keep DOM focus outside the collection; only the active key moves. */
|
||||
virtual: () => boolean;
|
||||
/** Whether the owner filter currently shows this record. */
|
||||
visible: (key: ListBoxKey) => boolean;
|
||||
optionId: (key: ListBoxKey) => string | undefined;
|
||||
restoreFocus: () => void;
|
||||
position: (key: ListBoxKey) => { index: number; size: number } | undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user