1b972a78ba
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.
75 lines
2.7 KiB
TypeScript
75 lines
2.7 KiB
TypeScript
import { inject, type ComputedRef, type InjectionKey, type Ref } from "vue";
|
|
import type { CollectionKey } from "../collection/keys";
|
|
import type { CollectionMember } from "../collection/registry";
|
|
import type { CollectionSelectionMode } from "../collection/selection";
|
|
|
|
export type ListBoxKey = CollectionKey;
|
|
export interface ListBoxDataItem {
|
|
id: ListBoxKey;
|
|
textValue?: string;
|
|
isDisabled?: boolean;
|
|
}
|
|
/** Shared schema: generic SFC parameter extraction can erase its prop types. */
|
|
export interface ListBoxOptions<T extends ListBoxDataItem = ListBoxDataItem> {
|
|
modelValue?: readonly ListBoxKey[];
|
|
selectedKeys?: readonly ListBoxKey[];
|
|
defaultValue?: readonly ListBoxKey[];
|
|
defaultSelectedKeys?: readonly ListBoxKey[];
|
|
selectionMode?: CollectionSelectionMode;
|
|
selectionBehavior?: "toggle" | "replace";
|
|
disabledKeys?: readonly ListBoxKey[];
|
|
isDisabled?: boolean;
|
|
isReadOnly?: boolean;
|
|
disallowEmptySelection?: boolean;
|
|
orientation?: "vertical" | "horizontal";
|
|
dir?: "ltr" | "rtl";
|
|
loop?: boolean;
|
|
variant?: "default" | "danger";
|
|
as?: string;
|
|
items?: readonly T[];
|
|
virtualized?: boolean;
|
|
rowHeight?: number;
|
|
overscan?: number;
|
|
}
|
|
export interface ListBoxItemState {
|
|
isSelected: boolean;
|
|
isDisabled: boolean;
|
|
isFocused: boolean;
|
|
isFocusVisible: boolean;
|
|
isPressed: boolean;
|
|
isHovered: boolean;
|
|
}
|
|
export interface ListBoxMember extends CollectionMember {
|
|
activate: (event: MouseEvent | KeyboardEvent) => void;
|
|
}
|
|
export interface ListBoxContext {
|
|
mode: ComputedRef<CollectionSelectionMode>;
|
|
readOnly: ComputedRef<boolean>;
|
|
keyboard: Ref<boolean>;
|
|
focused: Ref<ListBoxKey | undefined>;
|
|
noteFocus: (key: ListBoxKey) => void;
|
|
pressed: Ref<ListBoxKey | undefined>;
|
|
active: ComputedRef<ListBoxKey | undefined>;
|
|
selected: (key: ListBoxKey) => boolean;
|
|
disabled: (key: ListBoxKey) => boolean;
|
|
register: (member: ListBoxMember) => () => void;
|
|
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;
|
|
}
|
|
export const listBoxKey: InjectionKey<ListBoxContext> = Symbol("hv-list-box");
|
|
export const listBoxItemKey: InjectionKey<() => ListBoxItemState> =
|
|
Symbol("hv-list-box-item");
|
|
export function useListBox() {
|
|
const context = inject(listBoxKey);
|
|
if (!context) throw new Error("ListBoxItem requires ListBox.");
|
|
return context;
|
|
}
|