import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; export type ComboBoxItem = { id: Id; text: string; /** Whether the item is disabled */ disabled?: boolean; /** Icon component shown left of the item text */ icon?: any; /** Group label. Items sharing a label render together under a non-selectable header, ungrouped items first and groups in order of first appearance. A group with no items matching the filter is hidden. */ group?: string; }; type $RestProps = SvelteHTMLElements["input"]; type $Props = ComboBoxItem> = { /** * Set the combobox items. * @default [] */ items?: ReadonlyArray; /** * Override the display of a combobox item. * @default (item) => item.text ?? item.id */ itemToString?: (item: Item) => string; /** * Set the selected item by value id. * Follows the field when the owning form resets. */ selectedId?: Item["id"] | undefined; /** * Specify the selected combobox value. * Follows the field when the owning form resets. * @default "" */ value?: string; /** * Specify the direction of the combobox dropdown menu. * @default "bottom" */ direction?: "bottom" | "top"; /** * Set the size of the combobox. * Inherits the parent `Form` size when unset. * Also accepts Carbon v11's `"md"` (the 40px default, which overrides a * `Form` size). `"lg"` and `"xl"` are both 48px. */ size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined; /** * Set to `true` to disable the combobox * @default false */ disabled?: boolean; /** * Specify the label text of the combobox * @default "" */ labelText?: string; /** * Set to `true` to visually hide the label text * @default false */ hideLabel?: boolean; /** * Specify the placeholder text * @default "" */ placeholder?: string; /** * Specify the helper text * @default "" */ helperText?: string; /** * Specify the invalid state text * @default "" */ invalidText?: string; /** * Set to `true` to indicate an invalid state * @default false */ invalid?: boolean; /** * Set to `true` to indicate a warning state * @default false */ warn?: boolean; /** * Specify the warning state text * @default "" */ warnText?: string; /** * Set to `true` to open the combobox menu dropdown. * @default false */ open?: boolean; /** * Set to `true` to use the read-only variant * @default false */ readonly?: boolean; /** * Specify the assistive text announced to screen readers when read-only. * Exposed because VoiceOver does not announce `aria-readonly`. * @default "Read-only" */ readonlyText?: string; /** * Set to `true` to use the fluid variant. * Inherited from the parent `FluidForm` context, * so it does not need to be set when used inside `FluidForm`. * @default false */ fluid?: boolean; /** * Set to `true` to render condensed menu items in the fluid variant. * Menu items use the default height instead of the taller fluid height. * Only applies when the fluid variant is active. * @default false */ condensed?: boolean; /** * Set to `true` to allow custom values that are not in the items list. * By default, user-entered text is cleared when the combobox loses focus without selecting an item. * When enabled, custom text is preserved. * @default false */ allowCustomValue?: boolean; /** * Set to `true` to clear the input value when opening the dropdown. * This allows users to see all available items instead of only filtered results. * The original value is restored if the dropdown is closed without making a selection. * @default false */ clearFilterOnOpen?: boolean; /** * Set to `true` to select all text in the input when it receives focus (e.g. on tab or click). * @default false */ selectTextOnFocus?: boolean; /** * Set to `true` to reopen the dropdown menu after clearing the selection. * This allows users to immediately see all available items after clearing. * @default false */ openOnClear?: boolean; /** * Set to `true` to enable autocomplete with typeahead * @default false */ typeahead?: boolean; /** * Control whether the first matching item is automatically highlighted as the user types. * - `"none"`: No auto-highlighting (default). The user must use arrow keys to highlight items. * - `"first-match"`: Automatically highlight the first non-disabled filtered item on each input change. * @default "none" */ autoHighlight?: "none" | "first-match"; /** * Determine if an item should be filtered given the current combobox value. * When `typeahead` is enabled and no custom function is provided, * the default case-insensitive prefix matching is used. * When a custom function is provided, it is used even with `typeahead`. * @default () => true */ shouldFilterItem?: (item: Item, value: string) => boolean; /** * Set the filtering strategy used while the menu is open. * - `"remove"`: unmount non-matching options and recreate them when they * match again. * - `"hide"`: keep all options mounted and hide non-matching ones with the * `hidden` attribute. * * `"hide"` falls back to `"remove"` when virtualization is enabled, which * also keeps `wrapOptions` correct on a windowed list: a hidden option has no * rendered height, so measuring one would put a zero into offsets it does not * occupy. It also falls back when items are grouped, so a group whose items * all fail the filter loses its header. * @default "remove" */ filterMode?: "remove" | "hide"; /** * Override the chevron icon label based on the open state. * Defaults to "Open menu" when closed and "Close menu" when open. */ translateWithId?: ((id: import("../ListBox/ListBoxMenuIcon.svelte").ListBoxMenuIconTranslationId) => string) | undefined; /** * Override the label of the clear button when the input has a selection. * Defaults to "Clear selected item" since a combo box can only have one selection. */ translateWithIdSelection?: ((id: "clearSelection") => string) | undefined; /** * Specify the assistive text announced through the status live region when * a selection is cleared via the clear button, the Escape key, or `clear()`. * @default "Selection cleared" */ selectionClearedText?: string; /** * Build the assistive message announced through the status live region when * typing changes how many options match. Only used while a filter is active * (`typeahead` or a custom `shouldFilterItem`). * @default (count) => count === 0 ? "No results" : `${count} result${count === 1 ? "" : "s"} available` */ filterResultsText?: (count: number) => string; /** * Specify the message shown in the menu when no items match the filter. * Pass a function to build it from the typed value. * Set to `""` to show no message; `filterResultsText` still announces * the result count. * @default "No results" */ emptyText?: string | ((value: string) => string); /** * Set an id for the list box component * @default uniqueId() */ id?: string; /** * Specify a name attribute for the input. */ name?: string | undefined; /** * Set to `true` to require a value. Sets `required` on the input, so a * wrapping `
` blocks submission while it is empty, and the field * shows its invalid state with `requiredInvalidText` instead of the * browser's error bubble. * @default false */ required?: boolean; /** * Specify the invalid state text shown when a required combo box blocks * submission. Cleared once the input has a value. `invalid` and * `invalidText` take precedence. * @default "Select an item" */ requiredInvalidText?: string; /** * Obtain a reference to the input HTML element. * @default null */ ref?: null | HTMLInputElement; /** * Obtain a reference to the list HTML element. * @default null */ listRef?: null | HTMLDivElement; /** * Enable virtualization for large lists. Virtualization renders only the items currently visible in the viewport, improving performance for large lists. * * By default, virtualization is automatically enabled for lists with more than 100 items. * * Set `virtualize={false}` to explicitly disable virtualization, even for large lists. * * Set `virtualize={true}` to explicitly enable virtualization with default settings. * * Provide an object to customize virtualization behavior: * - `itemHeight` (default: size-based, or 64px for fluid unless `condensed`): Height of each item in pixels. Override when custom slots change row height. Under `wrapOptions`, heights are measured from the rendered options and this serves as the starting estimate for ones not yet measured. * - `containerHeight` (default: 300): The maximum height in pixels of the dropdown container. * - `overscan` (default: 3): The number of extra items to render above and below the viewport for smoother scrolling. Higher values may cause more flickering during very fast scrolling. * - `threshold` (default: 100): The minimum number of items required before virtualization activates. Lists with fewer items will render all items normally without virtualization. * - `maxItems` (default: undefined): The maximum number of items to render. When undefined, all visible items are rendered. * - `optimizeFastScroll` (default: false): Keep the rendered options in view during very fast scrolling, such as a scrollbar drag, instead of showing blank space until new options render. The options then follow the scroll a frame behind, and pause while the main thread is busy. */ virtualize?: undefined | boolean | { itemHeight?: number, containerHeight?: number, overscan?: number, threshold?: number, maxItems?: number, optimizeFastScroll?: boolean }; /** * Set to `true` to show a loading row after the options while the menu is * open, such as while fetching more items on `scrollend`. Marks the menu * `aria-busy` and hides the empty state; the options stay interactive. * @default false */ loading?: boolean; /** * Specify the text of the loading row * @default "Loading..." */ loadingText?: string; /** * Wait this many milliseconds after `loading` turns `true` before showing * the loading row. Once shown, it stays for at least 500 ms, so fast work * shows nothing and slow work never flashes. The menu sets `aria-busy` and * holds back the empty state as soon as `loading` is `true`. * Set to `0` to show it at once. * @default 0 */ loadingDelay?: number; /** * Set to `true` to let an option's label wrap onto as many lines as it needs * instead of being truncated with an ellipsis. * @default false */ wrapOptions?: boolean; /** * Set to `true` to always render the menu in a portal, or `false` to * keep it in place. Left unset, the menu moves to a portal inside a * Modal, or when a box around it (`overflow` other than `visible`) * would cut it off. */ portalMenu?: boolean | undefined; decorator?: (this: void) => void; empty?: (this: void, ...args: [{ value: string }]) => void; group?: (this: void, ...args: [{ group: string; items: ReadonlyArray; }]) => void; icon?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; /** Content at the end of each item. Replaces the checkmark on the selected item. */ iconEnd?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; /** * @deprecated use the `iconEnd` slot */ iconRight?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; labelChildren?: (this: void) => void; children?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; [key: `data-${string}`]: unknown; }; export type ComboBoxProps = ComboBoxItem> = Omit<$RestProps, keyof $Props> & $Props; export default class ComboBox = ComboBoxItem> extends SvelteComponentTyped< ComboBoxProps, { blur: WindowEventMap["blur"]; clear: CustomEvent; close: CustomEvent<{ trigger: "escape-key" | "outside-click" | "select"; }>; focus: WindowEventMap["focus"]; input: WindowEventMap["input"]; keydown: WindowEventMap["keydown"]; keyup: WindowEventMap["keyup"]; open: CustomEvent<{ trigger: "click" | "keydown" | "input" | "programmatic"; }>; paste: WindowEventMap["paste"]; scroll: WindowEventMap["scroll"]; /** * Dispatched when the menu is scrolled near the bottom (load-more signal). * Not the browser's native `scrollend` (scroll stopped). */ scrollend: CustomEvent<{ scrollTop: number; scrollHeight: number; clientHeight: number }>; select: CustomEvent<{ selectedId: Item["id"]; selectedItem: Item; }>; }, { default: { item: Item; index: number; selected: boolean; highlighted: boolean; }; decorator: Record; empty: { value: string }; group: { group: string; items: ReadonlyArray; }; icon: { item: Item; index: number; selected: boolean; highlighted: boolean; }; /** Content at the end of each item. Replaces the checkmark on the selected item. */ iconEnd: { item: Item; index: number; selected: boolean; highlighted: boolean; }; /** * @deprecated use the `iconEnd` slot */ iconRight: { item: Item; index: number; selected: boolean; highlighted: boolean; }; labelChildren: Record; } > { /** * Clear the combo box programmatically. * By default, focuses the combo box after clearing. Set `options.focus` to `false` to prevent focusing. * Set `options.open` to `true` to keep the dropdown open after clearing. * @example * ```svelte * * * * ``` */ clear: (options?: { focus?: boolean; open?: boolean; }) => Promise; }