import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; export type DataTableValue = any; export type DataTableTranslationId = "expandAll" | "collapseAll" | "expandRow" | "collapseRow" | "expandColumn" | "selectAll" | "selectRow"; export interface DataTableRow { id: Id; [key: string]: DataTableValue; } /** * Path keys for sort, headers, and cells; mirrors PropertyPath / PropertyPathIgnoringIndexSignatures in ./data-table-utils.d.ts. */ export type DataTableKey = ( [keyof import('./data-table-utils.d.ts').KeysWithoutIndexSignature] extends [never] ? import('./data-table-utils.d.ts').PropertyPath : keyof import('./data-table-utils.d.ts').KeysWithoutIndexSignature extends "id" ? import('./data-table-utils.d.ts').PropertyPath : Row extends DataTableRow ? import('./data-table-utils.d.ts').PropertyPathIgnoringIndexSignatures : import('./data-table-utils.d.ts').PropertyPath ); export type DataTableSortValue = import('./data-table-utils.d.ts').DataTableSortValue; export type DataTableEmptyHeader = { key: DataTableKey | (string & {}); /** Whether the header is empty */ empty: true; /** Name of the column for screen readers. Visually hidden. Without it, the header has no accessible name. */ value?: DataTableValue; display?: (item: DataTableValue, row: Row) => DataTableValue; /** `false` disables sorting for this column even when the table is sortable; `true` enables it even when the table is not; a comparator both enables sorting and provides it. Unset inherits the table-level `sortable`. */ sort?: boolean | ((a: DataTableSortValue, b: DataTableSortValue) => number); /** Override table-level sortAlways for this column */ sortAlways?: boolean; /** Whether the column menu is enabled */ columnMenu?: boolean; /** Whether the column is skipped in render while remaining in `headers` */ columnHidden?: boolean; width?: string; minWidth?: string; /** Horizontal alignment of the column header and cells. Logical, so `end` is the right edge in LTR and the left edge in RTL. Defaults to `"start"`. */ columnAlign?: "start" | "end"; }; export type DataTableNonEmptyHeader = { key: DataTableKey; empty?: false; value: DataTableValue; display?: (item: DataTableValue, row: Row) => DataTableValue; /** `false` disables sorting for this column even when the table is sortable; `true` enables it even when the table is not; a comparator both enables sorting and provides it. Unset inherits the table-level `sortable`. */ sort?: boolean | ((a: DataTableSortValue, b: DataTableSortValue) => number); /** Override table-level sortAlways for this column */ sortAlways?: boolean; /** Whether the column menu is enabled */ columnMenu?: boolean; /** Whether the column is skipped in render while remaining in `headers` */ columnHidden?: boolean; width?: string; minWidth?: string; /** Horizontal alignment of the column header and cells. Logical, so `end` is the right edge in LTR and the left edge in RTL. Defaults to `"start"`. */ columnAlign?: "start" | "end"; }; /** * A column under a key that isn't one of `Row`'s. Its cells come from * `display`, such as a total, or from the `cell` slot, such as a link. * It has no value to sort by. */ export type DataTableComputedHeader = { key: string & {}; empty?: false; value: DataTableValue; display?: (item: DataTableValue, row: Row) => DataTableValue; /** A computed column has no value to sort by. */ sort: false; /** Whether the column menu is enabled */ columnMenu?: boolean; /** Whether the column is skipped in render while remaining in `headers` */ columnHidden?: boolean; width?: string; minWidth?: string; /** Horizontal alignment of the column header and cells. Logical, so `end` is the right edge in LTR and the left edge in RTL. Defaults to `"start"`. */ columnAlign?: "start" | "end"; }; export type DataTableHeader = DataTableNonEmptyHeader | DataTableComputedHeader | DataTableEmptyHeader; export type DataTableCell = { key: DataTableKey | (string & {}); value: DataTableValue; display?: (item: DataTableValue, row: DataTableRow) => DataTableValue; }; export interface DataTableRowClassArgs { row: Row, rowIndex: number, selected: boolean, expanded: boolean } export type DataTableRowClass = string | ((row: DataTableRowClassArgs) => string | undefined); export interface DataTableCellClassArgs { row: Row, cell: DataTableCell, rowIndex: number, cellIndex: number, selected: boolean, expanded: boolean } export type DataTableCellClass = string | ((args: DataTableCellClassArgs) => string | undefined); export type CarbonDataTableContext = { batchSelectedIds: import("svelte/store").Writable>; tableRows: import("svelte/store").Writable>; /** * Exposes the table size to slotted content (e.g. `Toolbar`) * so it can derive a matching size unless explicitly overridden. */ tableSize: import("svelte/store").Writable<"compact" | "short" | "medium" | "tall" | undefined>; resetSelectedRowIds: () => void; filterRows: (searchValue: string, customFilter?: (row: Row, value: string) => boolean) => ReadonlyArray; /** Rebuild cells for a single row after an in-place edit to `rows`. */ refreshRow: (id: Row["id"]) => void; /** * Rebuild cells for currently painted rows after batch in-place edits to * `rows`. Offscreen rows are computed when they enter the window. */ refreshCells: () => void; setColumnAILabel: (headerId: string, active: boolean) => void; }; type $RestProps = SvelteHTMLElements["div"]; type $Props = { /** * Specify the data table headers. * @default [] */ headers?: ReadonlyArray>; /** * Specify the rows the data table should render. * Keys defined in `headers` are used for the row ids. * @default [] */ rows?: ReadonlyArray; /** * Set the size of the data table. * Also accepts Carbon v11's row heights: `"xs"` (`"compact"`), `"sm"` * (`"short"`), `"md"` (`"medium"`), `"lg"` (the 48px default) and `"xl"` * (`"tall"`). */ size?: "compact" | "short" | "medium" | "tall" | "xs" | "sm" | "md" | "lg" | "xl" | undefined; /** * Specify the title of the data table * @default "" */ title?: string; /** * Specify the description of the data table * @default "" */ description?: string; /** * Specify a custom class name for each row. * Provide a function to return a class name based * on row properties, allowing conditional classes * based on selected/expanded state. * @example * ```svelte * { * return `row-${rowIndex} ${selected ? 'selected' : ''} ${expanded ? 'expanded' : ''}`; * }} /> * ``` */ rowClass?: DataTableRowClass | undefined; /** * Set an id for the table container. * The title, description, header, cell and selection ids derive from it, * for example `{id}-title` and `{id}-{header.key}`. * @default uniqueId() */ id?: string; /** * Provide a class name for every body cell, or a function * that returns one based on the row, cell, and row state. * @example * ```svelte * cell.key === "status" && cell.value === "Failed" ? "cell-failed" : undefined} /> * ``` */ cellClass?: DataTableCellClass | undefined; /** * Specify a name attribute for the input elements * in a selectable data table (radio or checkbox). * When the table is inside a form, this name will * be included in the form data on submit. * Defaults to the table `id`. */ inputName?: string | undefined; /** * Specify the id of a form element outside the table to associate the * selection inputs with. */ inputForm?: string | undefined; /** * Set to `true` to use zebra styles * @default false */ zebra?: boolean; /** * Set to `true` for the sortable variant. Individual columns can opt out (or, if this is `false`, opt in) via `header.sort`. * @default false */ sortable?: boolean; /** * Specify the header key to sort by. * @default null */ sortKey?: DataTableKey | null; /** * Specify the sort direction. * @default "none" */ sortDirection?: "none" | "ascending" | "descending"; /** * Set to `true` to only toggle between "ascending" and * "descending" sort directions, skipping "none". * @default false */ sortAlways?: boolean; /** * Specify a default sort comparator for all sortable columns. * Per-header `sort` functions take precedence over this prop. * * With a typed row generic, `a` and `b` are {@link DataTableSortValue} (the union of cell value types over every {@link DataTableKey} on `Row`). Narrow using `context.key` (typed as {@link DataTableKey}) or runtime checks. * @example * ```svelte * { * switch (key) { * case "expireDate": * return new Date(a) - new Date(b); * case "port": * return a - b; * default: * return String(a).localeCompare(String(b)); * } * }} * /> * ``` */ sort?: ((a: DataTableSortValue, b: DataTableSortValue, context: { key: DataTableKey; ascending: boolean; row_a: Row; row_b: Row }) => number) | undefined; /** * Override the text announced after `sortKey` or `sortDirection` changes. * `header` is the sorted column. For `"none"`, it is the column that was * sorted before. */ sortStatusText?: (header: DataTableHeader | undefined, direction: "none" | "ascending" | "descending") => string; /** * Set to `true` for the expandable variant. * Automatically set to `true` if `batchExpansion` is `true`. * @default false */ expandable?: boolean; /** * Set to `true` to enable batch expansion. * @default false */ batchExpansion?: boolean; /** * Specify the row ids to be expanded. * @default [] */ expandedRowIds?: ReadonlyArray; /** * Specify the ids for rows that should not be expandable. * @default [] */ nonExpandableRowIds?: ReadonlyArray; /** * Set to `true` for the radio selection variant. * Row selection is enabled regardless of `selectable`. * @default false */ radio?: boolean; /** * Set to `true` so clicking an already-selected `radio` row clears the * selection instead of leaving it selected. Only applies with `radio`. * @default false */ allowDeselect?: boolean; /** * Set to `true` for the selectable variant. * Shift-clicking a row checkbox extends selection to every row between it and the last row clicked (not supported with `radio`). * @default false */ selectable?: boolean; /** * Set to `true` to enable batch selection. * Row selection is enabled regardless of `selectable`. * @default false */ batchSelection?: boolean; /** * Specify the row ids to be selected. * @default [] */ selectedRowIds?: ReadonlyArray; /** * Specify the ids of rows that should not be selectable. * @default [] */ nonSelectableRowIds?: ReadonlyArray; /** * Specify the row ids to highlight. * Adds `bx--data-table--highlighted-row`. The highlighted row class is themed by default. * @default [] */ highlightedRowIds?: ReadonlyArray; /** * Set to `true` to enable a sticky header * @default false */ stickyHeader?: boolean; /** * Override the maximum height of the sticky header table, replacing the * default `300px`. Only applies when `stickyHeader` is `true`. Pass a number * (interpreted as `px`) or a CSS length string (e.g. `"100%"`, `"24rem"`). */ stickyHeaderMaxHeight?: number | string | undefined; /** * Set to `true` to use static width * @default false */ useStaticWidth?: boolean; /** * Set to `true` to use a fixed table layout, where a header's `width`/`minWidth` * is the column's exact rendered size instead of a floor that content can grow past. * Unset columns share the remaining space equally. * @default false */ fixedLayout?: boolean; /** * Set the filtering strategy used by `ToolbarSearch`. * - `"remove"`: remove non-matching rows from the DOM and recreate them when the * filter clears. * - `"hide"`: keep all rows mounted and hide non-matching rows with the `hidden` * attribute, preserving focus, inputs, and open menus. * * `"hide"` falls back to `"remove"` when `pageSize` is set or `virtualize` is enabled. * * Because `"hide"` keeps every row mounted, each keystroke re-renders all * rows, not just matching ones. For large row counts (hundreds or more), * prefer `"remove"`, pagination, or virtualization. * @default "remove" */ filterMode?: "remove" | "hide"; /** * Specify the number of items to display in a page * @default 0 */ pageSize?: number; /** * Set to `number` to set current page * @default 0 */ page?: number; /** * Enable virtualization for large row lists. Virtualization renders only the rows currently visible in the viewport, improving performance for large datasets. * * Virtualization is opt-in. Set `virtualize={true}` to enable with default settings, or pass a configuration object to customize. * Virtualized tables are intended for use with `stickyHeader={true}` so the header stays visible while scrolling. Pagination is ignored when virtualization is enabled. * Virtualization assumes a uniform row height; combining it with `expandable` rows is not supported and may cause incorrect scroll-spacer sizing when rows are expanded mid-list. * * Provide an object to customize virtualization behavior: * - `itemHeight` (default: 48 for medium size, adjusted for size variant): The height in pixels of each row. Specify a custom value when using custom slots with multi-line content or different heights. * - `maxVisibleRows` (default: 10): The maximum number of rows to display in the viewport. The container height will be calculated as `itemHeight * maxVisibleRows`. Overridden by `containerHeight` if explicitly provided. * - `containerHeight` (default: calculated from maxVisibleRows): The maximum height in pixels of the table body container. If not provided and `stickyHeader` is `true` with a numeric `stickyHeaderMaxHeight`, that value is used instead so the virtual window matches the visible scroll area; otherwise calculated from `itemHeight * maxVisibleRows`. A string `stickyHeaderMaxHeight` (e.g. `"50vh"`) does not feed virtualization. * - `overscan` (default: 3): The number of extra rows 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 rows required before virtualization activates. Tables with fewer rows will render all rows normally without virtualization. * - `maxItems` (default: undefined): The maximum number of rows to render. When undefined, all visible rows are rendered. */ virtualize?: undefined | boolean | { itemHeight?: number, maxVisibleRows?: number, containerHeight?: number, overscan?: number, threshold?: number, maxItems?: number }; /** * Obtain a reference to the table wrapper element. When virtualization is enabled and `stickyHeader` is false, this element is the scroll container—use `bind:scrollContainerRef` to programmatically control scroll position (e.g. `scrollContainerRef.scrollTop = 0`). * @default null */ scrollContainerRef?: null | HTMLDivElement; /** * Override the default table header translation ids. */ tableHeaderTranslateWithId?: ((id: import("./TableHeader.svelte").TableHeaderTranslationId) => string) | undefined; /** * Override the default translation ids for the expand and select controls. * @default (id) => defaultTranslations[id] */ translateWithId?: (id: DataTableTranslationId) => string; cell?: (this: void, ...args: [{ row: Row; cell: DataTableCell; rowIndex: number; cellIndex: number; rowSelected: boolean; rowExpanded: boolean; }]) => void; cellHeader?: (this: void, ...args: [{ header: DataTableNonEmptyHeader | DataTableComputedHeader }]) => void; decorator?: (this: void) => void; descriptionChildren?: (this: void, ...args: [{ props: { class: "bx--data-table-header__description" } }]) => void; expandIcon?: (this: void, ...args: [{ expanded: boolean; row: Row | undefined; props: { "aria-hidden": "true" | "false"; class: string; }; }]) => void; expandedRow?: (this: void, ...args: [{ row: Row; rowSelected: boolean; }]) => void; footerCell?: (this: void, ...args: [{ header: DataTableHeader; index: number; }]) => void; headerDecorator?: (this: void, ...args: [{ header: DataTableNonEmptyHeader | DataTableComputedHeader }]) => void; rowDecorator?: (this: void, ...args: [{ row: Row }]) => void; titleChildren?: (this: void, ...args: [{ props: { class: "bx--data-table-header__title" } }]) => void; children?: (this: void) => void; [key: `data-${string}`]: unknown; }; export type DataTableProps = Omit<$RestProps, keyof $Props> & $Props; export default class DataTable extends SvelteComponentTyped< DataTableProps, { click: CustomEvent<{ header?: DataTableHeader; row?: Row; cell?: DataTableCell; }>; "click:cell": CustomEvent<{ cell: DataTableCell; target: EventTarget; currentTarget: EventTarget; }>; "click:header": CustomEvent<{ header: DataTableHeader; /** The intended next sort direction for this click, reported regardless of whether the `sort` event was cancelled. */ sortDirection?: "ascending" | "descending" | "none"; target: EventTarget; currentTarget: EventTarget; }>; "click:header--expand": CustomEvent<{ expanded: boolean; }>; "click:header--select": CustomEvent<{ indeterminate: boolean; selected: boolean; }>; "click:row": CustomEvent<{ row: Row; target: EventTarget; currentTarget: EventTarget; }>; "click:row--expand": CustomEvent<{ expanded: boolean; row: Row; }>; "click:row--select": CustomEvent<{ selected: boolean; row: Row; }>; "mouseenter:row": CustomEvent; "mouseleave:row": CustomEvent; /** * Dispatched when the virtualized table body is scrolled near the bottom * (load-more signal). Not the browser's native `scrollend` (scroll stopped). * Only fires when `virtualize` is enabled. */ scrollend: CustomEvent<{ scrollTop: number; scrollHeight: number; clientHeight: number }>; /** Dispatched when a sortable column header would change the active sort. The event is cancelable: call `preventDefault()` to skip updating `sortKey` / `sortDirection` and skip client side sorting for that click (for example full server side sorting while still reading `detail.key` / `detail.direction` for your API). If not cancelled, the table applies the new sort and sorts the current `rows` client side. Typical uses: server side sorting, URL or query string sync, analytics, and persisting sort preferences. */ sort: CustomEvent<{ key: null; direction: "none" } | { key: DataTableKey; direction: "ascending" | "descending" }>; }, { default: Record; cell: { row: Row; cell: DataTableCell; rowIndex: number; cellIndex: number; rowSelected: boolean; rowExpanded: boolean; }; cellHeader: { header: DataTableNonEmptyHeader | DataTableComputedHeader }; decorator: Record; descriptionChildren: { props: { class: "bx--data-table-header__description" } }; expandIcon: { expanded: boolean; row: Row | undefined; props: { "aria-hidden": "true" | "false"; class: string; }; }; expandedRow: { row: Row; rowSelected: boolean; }; footerCell: { header: DataTableHeader; index: number; }; headerDecorator: { header: DataTableNonEmptyHeader | DataTableComputedHeader }; rowDecorator: { row: Row }; titleChildren: { props: { class: "bx--data-table-header__title" } }; } > { /** * Default translation ids. */ translationIds: { expandAll: "expandAll"; collapseAll: "collapseAll"; expandRow: "expandRow"; collapseRow: "collapseRow"; expandColumn: "expandColumn"; selectAll: "selectAll"; selectRow: "selectRow" }; /** * Rebuild cells for a single row after an in-place edit to `rows`. * @example * ```svelte * * dataTable.refreshRow(row.id)} /> * ``` */ refreshRow: (id: Row["id"]) => void; /** * Rebuild cells for currently painted rows after batch in-place edits to * `rows`. Offscreen rows are computed when they enter the window. * @example * ```svelte * * * ``` */ refreshCells: () => void; }