"use client"; import { type ComboboxImplementation, type ConfigurableComboboxOptions, useCombobox, } from "./useCombobox.js"; /** * @since 6.0.0 */ export interface SelectComboboxOptions< ComboboxEl extends HTMLElement = HTMLInputElement, PopupEl extends HTMLElement = HTMLElement, > extends ConfigurableComboboxOptions { value: string; values: readonly string[]; } /** * @since 6.0.0 */ export type SelectComboboxImplementation< ComboboxEl extends HTMLElement = HTMLInputElement, PopupEl extends HTMLElement = HTMLElement, > = ComboboxImplementation; /** * @since 6.0.0 */ export function useSelectCombobox< ComboboxEl extends HTMLElement = HTMLInputElement, PopupEl extends HTMLElement = HTMLElement, >( options: SelectComboboxOptions ): SelectComboboxImplementation { const { value, values, ...comboboxOptions } = options; return useCombobox({ ...comboboxOptions, searchable: true, extendKeyDown(movementData) { const { event, show, focusLastRef, visible } = movementData; if (visible) { return; } switch (event.key) { case " ": case "Home": case "End": event.preventDefault(); event.stopPropagation(); focusLastRef.current = event.key === "End"; show(); break; } }, getEnterDefaultFocusedIndex(options) { const { focusLast } = options; if (focusLast && !value) { return values.length - 1; } return Math.max(0, values.indexOf(value)); }, }); }