import classNames from "classnames/dedupe"; import { Option } from "./Option"; import type { ComboboxBaseProps, ComboboxOption, ComboboxOptions, } from "./types"; import { isOptionGroup } from "./utils"; const getOptionKey = ( option: ComboboxOption, index: number, groupIndex?: number, ): string => { let key: string; if (typeof option === "object" && option.id) key = option.id; else if (typeof option === "object") key = `${option.value}::${option.label}`; else if (typeof option === "string") key = option; else key = String(index); return groupIndex !== undefined ? `${groupIndex}:${key}` : key; }; export type ListboxProps = { id: string; isOpen?: boolean; isLoading?: boolean; options: ComboboxOptions | undefined; highlightedIndex: number; highlightedGroupIndex: number; onOptionSelect: (option: T) => void; optionComponent?: ComboboxBaseProps["optionComponent"]; optionTestIdPrefix?: string; }; export const Listbox = ({ id, isOpen = false, options, highlightedIndex, highlightedGroupIndex, onOptionSelect, optionComponent, optionTestIdPrefix, }: ListboxProps) => { const classes = classNames("mobius-combobox__list", { "mobius-combobox__list--hidden": !isOpen, }); function getOptionId( option: ComboboxOption, groupIndex: number, index: number, ) { if ( typeof option === "object" && "id" in option && typeof option.id === "string" ) { return option.id; } return isOptionGroup(options) ? `${id}-option-${groupIndex}-${index}` : `${id}-option-${index}`; } // If not open, render empty listbox but keep it in DOM for accessibility if (!isOpen) { return
; } if (options && options.length === 0) { return (
No options
); } return (
{isOptionGroup(options) ? options.map((option, groupIndex) => (
    {option.options.map((groupOption, index) => (
)) : typeof options !== "undefined" ? options.map((option, index) => (
); };