import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; export type MultiSelectItemText = string; export type MultiSelectItem = { id: Id; text: MultiSelectItemText; /** Whether the item is disabled */ disabled?: boolean; /** Whether this item acts as a "select all" toggle */ isSelectAll?: boolean; /** Group label. Items sharing a label render together under a non-selectable header, groups in order of first appearance and `sortItem` sorting within each group. */ group?: string; }; export type CarbonMultiSelectContext = { declareRef: (data: { key: "field" | "selection"; ref: HTMLDivElement | HTMLButtonElement }) => void; /** * Clears a ref when the child that declared it unmounts, unless another * child has declared a newer one for the same key since. */ releaseRef: (data: { key: "field" | "selection"; ref: HTMLDivElement | HTMLButtonElement }) => void; }; type $RestProps = SvelteHTMLElements["input"] & SvelteHTMLElements["button"]; type $Props = MultiSelectItem> = { /** * Set the multiselect items. * @default [] */ items?: ReadonlyArray; /** * Override the display of a multiselect item. * @default (item) => item.text ?? item.id */ itemToString?: (item: Item) => string | Item["id"]; /** * Override `name`/`value` for the hidden inputs that mirror the current * selection for native form submission, and `title`/`labelText` for the * visible option checkbox. `name`/`value` do not reach the option * checkbox itself -- it is decorative and excluded from form * participation, even while the menu is open. * @default (_item) => ({}) */ itemToInput?: (item: Item) => { name?: string; labelText?: string; title?: string; value?: string }; /** * Set the selected ids. * @default [] */ selectedIds?: ReadonlyArray; /** * Specify the multiselect value. * @default "" */ value?: string; /** * Set the size of the multiselect. * 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; /** * Specify the type of multiselect. * @default "default" */ type?: "default" | "inline"; /** * Specify the direction of the multiselect dropdown menu. * @default "bottom" */ direction?: "bottom" | "top"; /** * Specify the selection feedback after selecting items. * @default "top-after-reopen" */ selectionFeedback?: "top" | "fixed" | "top-after-reopen"; /** * Cap how many items can be selected at once. * Unset or a non-positive value means unlimited. * When set, select-all is unavailable and unchecked items disable once the cap is reached. */ maxSelectedItems?: number | undefined; /** * How the field shows the selection. `"count"` shows only the count tag * in the field. `"tags"` also lists each selected item as a dismissible * tag below the field, so the selection stays visible while the menu is * closed. * @default "count" */ selectionDisplay?: "count" | "tags"; /** * With `selectionDisplay="tags"`, `"wrap"` shows every selected tag on as * many rows as needed, and `"collapse"` keeps one row and collapses the * rest into a "+N" button. The button opens the hidden tags in a popover, * where they can still be removed. * @default "wrap" */ tagOverflow?: "wrap" | "collapse"; /** * With `selectionDisplay="tags"`, the accessible name prefix of each * tag's remove button. * @default "Remove" */ removeTagText?: string; /** * Set to `true` to disable the dropdown * @default false */ disabled?: boolean; /** * Set to `true` to filter items * @default false */ filterable?: boolean; /** * Override the filtering logic. * The default matches items whose `text` contains the typed value, * ignoring case and leading or trailing whitespace. * @default (item, value) => item.text.toLowerCase().includes(value.trim().toLowerCase()) */ filterItem?: (item: Item, value: string) => boolean; /** * Set to `true` to select all text in the filter input when it receives * focus (e.g. on tab or click). Only applies when `filterable` is `true`. * @default false */ selectTextOnFocus?: boolean; /** * Set to `true` to open the dropdown. * @default false */ open?: boolean; /** * 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`. * Cannot be combined with the inline variant. * @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; /** * Specify the locale * @default "en" */ locale?: string; /** * Specify the placeholder text * @default "" */ placeholder?: string; /** * Override the sorting logic. * The default sorting compare the item text value. * Set to `false` to skip sorting and keep the original `items` order. * @default (a, b) => getSortCollator().compare(a.text, b.text) */ sortItem?: ((a: Item, b: Item) => number) | (() => void) | false; /** * 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" and "Clear all items" if more than one item is selected. */ translateWithIdSelection?: ((id: import("../ListBox/ListBoxSelection.svelte").ListBoxSelectionTranslationId) => string) | undefined; /** * Specify the label text * @default "" */ labelText?: string; /** * Set to `true` to pass the item to `itemToString` in the checkbox * @default false */ useTitleInItem?: boolean; /** * Set to `true` to indicate an invalid state * @default false */ invalid?: boolean; /** * Specify the invalid state text * @default "" */ invalidText?: string; /** * Set to `true` to indicate a warning state * @default false */ warn?: boolean; /** * Specify the warning state text * @default "" */ warnText?: string; /** * Specify the helper text * @default "" */ helperText?: string; /** * Specify the list box label * @default "" */ label?: string; /** * Set to `true` to visually hide the label text * @default false */ hideLabel?: boolean; /** * Set an id for the list box component * @default uniqueId() */ id?: string; /** * 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; /** * Specify the assistive text describing options disabled by `maxSelectedItems`. * Announced so screen reader users learn why an option refuses selection, * not just that it is disabled. * @default "Maximum items selected" */ maxSelectedText?: string; /** * Specify the assistive text advertising the keyboard shortcut that clears * the selection. Appended to the field's visually-hidden description * whenever there is a selection to clear. * @default "To clear the selection, press Delete or Backspace" */ clearSelectionText?: string; /** * Specify the assistive text for how many items are selected. Part of the * field's visually-hidden description, and of `readonlyText` when read-only. * @default (count) => `${count} selected` */ selectionCountText?: (count: number) => string; /** * Specify the assistive text announced through the status live region when * the selection is cleared via the keyboard or the clear button. * @default "All items cleared" */ selectionClearedText?: string; /** * 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; /** * Build the assistive message announced through the status live region when * typing in the filterable variant changes how many options match. * The count excludes any "select all" item. * @default (count) => count === 0 ? "No results" : `${count} result${count === 1 ? "" : "s"} available` */ filterResultsText?: (count: number) => string; /** * Specify the message shown in the open menu when no items match the * filter, or, without `filterable`, when `items` is empty. * Defaults to "No results" when `filterable` is `true` and "No options" * otherwise. Pass a function to build it from the typed value. * Set to `""` to show no message. When filterable, `filterResultsText` * still announces the result count; otherwise the status live region * announces this message, also when the `empty` slot replaces it. */ emptyText?: undefined | string | ((value: string) => string); /** * Default group name for the hidden inputs that mirror the current * selection for native form submission (`FormData`). Used per item * unless `itemToInput` returns its own `name`. Each input's value * defaults to the item `id` unless `itemToInput` returns a `value`. * Not applied to the * filterable text input, which is a query box, not the form value. * Omitted from submission when the multi-select is disabled. */ name?: string | undefined; /** * Set to `true` to require at least one selection. A wrapping `
` * blocks submission while no enabled item is selected, 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 multi-select * blocks submission. Cleared once an item is selected. `invalid` and * `invalidText` take precedence. * @default "Select at least one item" */ requiredInvalidText?: string; /** * Obtain a reference to the input HTML element. * @default null */ inputRef?: null | HTMLInputElement; /** * Obtain a reference to the outer div element. * @default null */ multiSelectRef?: null | HTMLDivElement; /** * Obtain a reference to the field box element. * @default null */ fieldRef?: null | HTMLButtonElement; /** * Obtain a reference to the selection element. * @default null */ selectionRef?: null | HTMLDivElement; /** * Id of the highlighted ListBoxMenuItem. * @default null */ highlightedId?: null | Item["id"]; /** * The post-sort, post-selection-feedback list of items rendered by the dropdown. * Bind to read the resolved order without recomputing it from `items`, `selectedIds`, and `sortItem`. * @default [] */ sortedItems?: ReadonlyArray; /** * 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; /** * Obtain a reference to the list HTML element. * @default null */ listRef?: null | HTMLDivElement; decorator?: (this: void) => void; empty?: (this: void, ...args: [{ value: string }]) => void; group?: (this: void, ...args: [{ group: string; items: ReadonlyArray; }]) => void; labelChildren?: (this: void) => void; tag?: (this: void, ...args: [{ item: Item; index: number; props: { filter: boolean; disabled: boolean; value: Item["id"]; title: string; maxWidth: string; }; }]) => void; children?: (this: void, ...args: [{ item: Item; index: number; selected: boolean; highlighted: boolean; }]) => void; [key: `data-${string}`]: unknown; }; export type MultiSelectProps = MultiSelectItem> = Omit<$RestProps, keyof $Props> & $Props; export default class MultiSelect = MultiSelectItem> extends SvelteComponentTyped< MultiSelectProps, { blur: FocusEvent | CustomEvent; clear: CustomEvent; close: CustomEvent<{ trigger: "escape-key" | "outside-click" }>; 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"]; scrollend: CustomEvent<{ scrollTop: number; scrollHeight: number; clientHeight: number }>; select: CustomEvent<{ selectedIds: Item["id"][]; selected: Item[]; unselected: Item[]; }>; }, { default: { item: Item; index: number; selected: boolean; highlighted: boolean; }; decorator: Record; empty: { value: string }; group: { group: string; items: ReadonlyArray; }; labelChildren: Record; tag: { item: Item; index: number; props: { filter: boolean; disabled: boolean; value: Item["id"]; title: string; maxWidth: string; }; }; } > { /** * Clear the multiselect selection programmatically. * By default, focuses the multiselect after clearing. Set `options.focus` to `false` to prevent focusing. * Set `options.open` to `true` to open the dropdown menu after clearing. * @example * ```svelte * * * ``` */ clear: (options?: { focus?: boolean; open?: boolean; }) => Promise; }