carbon-components-svelte
Version:
Svelte implementation of the Carbon Design System
352 lines (299 loc) • 9.12 kB
TypeScript
import { SvelteComponentTyped } from "svelte";
import type { SvelteHTMLElements } from "svelte/elements";
export type MultiSelectItemText = string;
export type MultiSelectItem<Id = any> = {
id: Id;
text: MultiSelectItemText;
/** Whether the item is disabled */
disabled?: boolean;
/** Whether this item acts as a "select all" toggle */
isSelectAll?: boolean;
};
export type CarbonMultiSelectContext = {
declareRef: (data: {
key: "field" | "selection";
ref: HTMLDivElement | HTMLButtonElement
}) => void;
};
type $RestProps = SvelteHTMLElements["input"];
type $Props<Item extends MultiSelectItem<any> = MultiSelectItem<any>> = {
/**
* Set the multiselect items.
* @default []
*/
items?: ReadonlyArray<Item>;
/**
* Override the display of a multiselect item.
*/
itemToString?: (item: Item) => string | Item["id"];
/**
* Override the item name, title, labelText, or value passed to the user-selectable checkbox input as well as the hidden inputs.
*/
itemToInput?: (item: Item) => {
name?: string;
labelText?: any;
title?: string;
value?: string
};
/**
* Set the selected ids.
* @default []
*/
selectedIds?: ReadonlyArray<Item["id"]>;
/**
* Specify the multiselect value.
* @default ""
*/
value?: string;
/**
* Set the size of the multiselect.
* @default undefined
*/
size?: "xs" | "sm" | "lg" | "xl";
/**
* 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";
/**
* 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 filtering is an exact string comparison.
*/
filterItem?: (item: Item, value: string) => boolean;
/**
* Set to `true` to open the dropdown.
* @default false
*/
open?: boolean;
/**
* Set to `true` to enable the light variant
* @default false
*/
light?: 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.
*/
sortItem?: ((a: Item, b: Item) => number) | (() => void);
/**
* Override the chevron icon label based on the open state.
* Defaults to "Open menu" when closed and "Close menu" when open.
* @default undefined
*/
translateWithId?: (id: import("../ListBox/ListBoxMenuIcon.svelte").ListBoxMenuIconTranslationId) => string;
/**
* 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.
* @default undefined
*/
translateWithIdSelection?: (id: import("../ListBox/ListBoxSelection.svelte").ListBoxSelectionTranslationId) => string;
/**
* 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 `ccs-${Math.random().toString(36)}`
*/
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 a name attribute for the select.
* @default undefined
*/
name?: 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<Item & { checked: boolean }>;
/**
* 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.
* - `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.
* @default undefined
*/
virtualize?: undefined | boolean | { itemHeight?: number, containerHeight?: number, overscan?: number, threshold?: number, maxItems?: number };
/**
* Set to `true` to render the dropdown menu in a portal,
* allowing it to escape containers with `overflow: hidden`.
* When inside a Modal, defaults to `true` unless explicitly set to `false`.
* @default undefined
*/
portalMenu?: boolean | undefined;
/**
* Obtain a reference to the list HTML element.
* @default null
*/
listRef?: null | HTMLDivElement;
labelChildren?: (this: void) => void;
children?: (this: void, ...args: [{
item: Item;
index: number;
selected: boolean;
highlighted: boolean;
}]) => void;
[key: `data-${string}`]: unknown;
};
export type MultiSelectProps<Item extends MultiSelectItem<any> = MultiSelectItem<any>> = Omit<$RestProps, keyof $Props<Item>> & $Props<Item>;
export default class MultiSelect<Item extends MultiSelectItem<any> = MultiSelectItem<any>> extends SvelteComponentTyped<
MultiSelectProps<Item>,
{
blur: FocusEvent | CustomEvent<FocusEvent>;
clear: CustomEvent<null>;
close: CustomEvent<{ trigger: "escape-key" | "outside-click" }>;
focus: WindowEventMap["focus"];
input: WindowEventMap["input"];
keydown: WindowEventMap["keydown"];
keyup: WindowEventMap["keyup"];
paste: WindowEventMap["paste"];
scroll: WindowEventMap["scroll"];
select: CustomEvent<{
selectedIds: Item["id"][];
selected: Item[];
unselected: Item[];
}>;
},
{
default: {
item: Item;
index: number;
selected: boolean;
highlighted: boolean;
};
labelChildren: Record<string, never>;
}
> {}