feat: migrate ListBox with virtual collection and accessible parts

This commit is contained in:
maoyingjie
2026-10-23 15:02:30 +08:00
parent dea5ded716
commit 65db60a7ac
39 changed files with 2694 additions and 12 deletions
@@ -0,0 +1,69 @@
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;
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;
}