import { SvelteComponentTyped } from "svelte"; import type { SvelteHTMLElements } from "svelte/elements"; export type TreeNode = { id: Id; text: any; icon?: any; /** Whether the node is disabled */ disabled?: boolean; /** Optional URL the node links to */ href?: string; /** Optional link target (e.g., "_blank") */ target?: string; /** Whether the node has children that have not been loaded yet. Renders an expander even without a `nodes` array; expanding fires `toggle` so children can be loaded lazily. */ hasChildren?: boolean; nodes?: TreeNode[]; }; export type ShowNodeOptions = { /** Whether to expand the node and its ancestors (default: true) */ expand?: boolean; /** Whether to select the node (default: true) */ select?: boolean; /** Whether to focus the node (default: true) */ focus?: boolean; }; export type TreeViewExpandedChange = { /** The full set of expanded node ids after the change */ expandedIds: ReadonlyArray; /** Node ids expanded since the previous change */ added: Array; /** Node ids collapsed since the previous change */ removed: Array; }; export type TreeViewSelectionChange = { /** The full set of selected node ids after the change */ selectedIds: ReadonlyArray; /** Node ids selected since the previous change */ added: Array; /** Node ids deselected since the previous change */ removed: Array; }; export type TreeViewCheckChange = { /** The full set of checked node ids after the change */ checkedIds: ReadonlyArray; /** Node ids checked since the previous change */ added: Array; /** Node ids unchecked since the previous change */ removed: Array; /** The partially checked node ids after the change */ indeterminateIds: ReadonlyArray; }; export type CarbonTreeViewContext = TreeNode> = { treeId: import("svelte/store").Writable; /** * In server markup, the row that gets the tab stop once the tree mounts * (the first enabled one), so the tree is reachable by keyboard before * scripts run. In the browser the roving tab stop below takes over. */ serverTabStopId: string | number | undefined; activeNodeId: import("svelte/store").Writable; selectedNodeIds: import("svelte/store").Writable>; expandedNodeIds: import("svelte/store").Writable>; selectedIdSet: import("svelte/store").Writable>; checkedIdSet: import("svelte/store").Writable>; expandedIdSet: import("svelte/store").Writable>; indeterminateIdSet: import("svelte/store").Writable>; multiselect: import("svelte/store").Writable; selectionMode: import("svelte/store").Writable<"highlight" | "checkbox">; clickNode: (node: Node, event?: Event) => void; selectNode: (node: Node) => void; expandNode: (node: Node, expanded: boolean) => void; focusNode: (node: Node) => void; toggleNode: (node: Node) => void; /** * The key pressed, with ArrowLeft and ArrowRight swapped in RTL, so * "ArrowRight" always means toward the children. */ treeKey: (event: KeyboardEvent) => string; isInitialRender: () => boolean; }; type $RestProps = SvelteHTMLElements["ul"]; type $Props = TreeNode> = { /** * Provide an array of nodes to render. * @default [] */ nodes?: ReadonlyArray; /** * Set the current active node id. * Only one node can be active. * @default "" */ activeId?: Node["id"]; /** * Set the node ids to be selected. * @default [] */ selectedIds?: ReadonlyArray; /** * Set the node ids to be expanded. * @default [] */ expandedIds?: ReadonlyArray; /** * Specify the TreeView size. * Also accepts Carbon v11's `"sm"` (`"default"`, 32px) and `"xs"` * (`"compact"`, 24px). * @default "default" */ size?: "default" | "compact" | "sm" | "xs"; /** * Specify the label text * @default "" */ labelText?: string; /** * Set to `true` to visually hide the label text * @default false */ hideLabel?: boolean; /** * Set an id for the tree element. * The label, node label, subtree, and checkbox ids derive from it, * for example `{id}-label` and `{id}-{node.id}-subtree`. * @default uniqueId() */ id?: string; /** * Set to `true` to automatically collapse sibling nodes when expanding a node. * When enabled, only one node at each level can be expanded at a time. * @default false */ autoCollapse?: boolean; /** * Set to `true` to enable multi-select mode. * Supports Ctrl/Cmd+Click (toggle) and Shift+Click (range select). * @default false */ multiselect?: boolean; /** * When `multiselect` is true, `multiselectMode` controls how many nodes a selection gesture includes: * - `'node'`: only the clicked/active node (default) * - `'shallow'`: the node plus its direct non-disabled children * - `'deep'`: the node plus all non-disabled descendants * @default "node" */ multiselectMode?: 'node' | 'shallow' | 'deep'; /** * Specify how a selection is presented. * `"checkbox"` renders a tri-state checkbox on every node and implies * multi-selection; `multiselect` and `multiselectMode` are ignored in that mode. * @default "highlight" */ selectionMode?: "highlight" | "checkbox"; /** * In `selectionMode="checkbox"`, `checkMode` controls how far a check propagates: * - `'node'`: only the clicked node; parents and children never follow each other * - `'deep'`: the node plus all non-disabled descendants, with ancestors deriving * an indeterminate state (default) * @default "deep" */ checkMode?: 'node' | 'deep'; /** * The node ids that are checked in `selectionMode="checkbox"`. Distinct from * `selectedIds`, which tracks row highlighting and is unused in checkbox mode. * @default [] */ checkedIds?: ReadonlyArray; /** * The node ids that are partially checked in `selectionMode="checkbox"`. * Derived from `checkedIds`; writes to this prop are overwritten. * @default [] */ indeterminateIds?: ReadonlyArray; /** * Enable virtualization for large trees. Virtualization renders only the * rows currently visible in the viewport, improving performance for large * datasets. * * Virtualization is opt-in. TreeView cannot tell from `nodes` alone whether * the tree will stay small or grow through expansion, so it does not * auto-enable based on row count. Set `virtualize={true}` to enable with * default settings, or pass a configuration object to customize. Leave * `undefined` for the default recursive rendering. * * Row height is derived from `size` (32px default, 24px compact). * * Collapsed subtrees mount lazily on first expansion and stay mounted. * Virtualization is per subtree. Enable it when a node may hold a large * child list. */ virtualize?: undefined | boolean | { maxVisibleRows?: number, containerHeight?: number | string, overscan?: number, }; /** * Reference to the scrollable container when `virtualize` is enabled. * `null` otherwise. * @default null */ scrollContainerRef?: null | HTMLUListElement; childNodes?: (this: void, ...args: [{ node: Node & { expanded: boolean; leaf: boolean; selected: boolean; checked: boolean; indeterminate: boolean; } }]) => void; labelChildren?: (this: void) => void; children?: (this: void, ...args: [{ node: Node & { expanded: boolean; leaf: boolean; selected: boolean; checked: boolean; indeterminate: boolean; } }]) => void; [key: `data-${string}`]: unknown; }; export type TreeViewProps = TreeNode> = Omit<$RestProps, keyof $Props> & $Props; export default class TreeView = TreeNode> extends SvelteComponentTyped< TreeViewProps, { check: CustomEvent; "check:change": CustomEvent>; focus: CustomEvent; keydown: WindowEventMap["keydown"]; select: CustomEvent; "select:change": CustomEvent>; toggle: CustomEvent; "toggle:change": CustomEvent>; }, { default: { node: Node & { expanded: boolean; leaf: boolean; selected: boolean; checked: boolean; indeterminate: boolean; } }; childNodes: { node: Node & { expanded: boolean; leaf: boolean; selected: boolean; checked: boolean; indeterminate: boolean; } }; labelChildren: Record; } > { /** * Programmatically expand all expandable nodes (those with loaded children * or `hasChildren: true`). Leaf ids are omitted — they do not affect * expansion. * @example * ```svelte * * * ``` */ expandAll: () => void; /** * Programmatically collapse all nodes * @example * ```svelte * * * ``` */ collapseAll: () => void; /** * Programmatically expand a subset of nodes. * Expands all nodes if no argument is provided. * Filter function should return `true` for nodes to expand. If not provided, expands all nodes. * @example * ```svelte * * * ``` */ expandNodes: (filterNode?: (node: Node) => boolean) => void; /** * Programmatically collapse a subset of nodes. * Collapses all nodes if no argument is provided. * Filter function should return `true` for nodes to collapse. If not provided, collapses all nodes. * @example * ```svelte * * * ``` */ collapseNodes: (filterNode?: (node: Node) => boolean) => void; /** * Programmatically show a node by `id`. * By default, the matching node will be expanded, selected, and focused. * Use the options parameter to customize this behavior. * @example * ```svelte * * * * ``` */ showNode: (id: Node["id"], options?: ShowNodeOptions) => void; /** * Look up a node by `id` without re-implementing tree traversal. * Returns `null` if no node with the given `id` exists. * @example * ```svelte * * * ``` */ getNode: (id: Node["id"]) => Node | null; /** * Look up multiple nodes by `id`. Ids without a matching node are omitted. * @example * ```svelte * * * ``` */ getNodes: (ids: ReadonlyArray) => Array; }