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 { 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; readOnly: ComputedRef; keyboard: Ref; focused: Ref; noteFocus: (key: ListBoxKey) => void; pressed: Ref; active: ComputedRef; selected: (key: ListBoxKey) => boolean; disabled: (key: ListBoxKey) => boolean; register: (member: ListBoxMember) => () => void; activate: (key: ListBoxKey, event: MouseEvent | KeyboardEvent) => void; focus: (key: ListBoxKey) => Promise; 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 = 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; }