mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
Fixes https://github.com/carbon-design-system/carbon-icons-svelte/issues/207 `carbon-icons-svelte@13` and `carbon-pictograms-svelte@13` now only support TypeScript for Svelte 4/5. The new `Component` type is incompatible with the `icon` prop in `carbon-components-svelte`, causing a type error with Svelte 5, as `typeof SvelteComponent` doesn't match the new `Component` type. Since `Component` isn't available in Svelte 3/4, this PR changes the `icon` prop type to `any` for compatibility across Svelte 3, 4, and 5.
114 lines
2.4 KiB
TypeScript
114 lines
2.4 KiB
TypeScript
import type { SvelteComponentTyped } from "svelte";
|
|
import type { SvelteHTMLElements } from "svelte/elements";
|
|
|
|
export type TreeNodeId = string | number;
|
|
|
|
export interface TreeNode {
|
|
id: TreeNodeId;
|
|
text: any;
|
|
icon?: any;
|
|
disabled?: boolean;
|
|
nodes?: TreeNode[];
|
|
}
|
|
|
|
type $RestProps = SvelteHTMLElements["ul"];
|
|
|
|
type $Props = {
|
|
/**
|
|
* Provide an array of nodes to render
|
|
* @default []
|
|
*/
|
|
nodes?: Array<TreeNode>;
|
|
|
|
/**
|
|
* Set the current active node id
|
|
* Only one node can be active
|
|
* @default ""
|
|
*/
|
|
activeId?: TreeNodeId;
|
|
|
|
/**
|
|
* Set the node ids to be selected
|
|
* @default []
|
|
*/
|
|
selectedIds?: ReadonlyArray<TreeNodeId>;
|
|
|
|
/**
|
|
* Set the node ids to be expanded
|
|
* @default []
|
|
*/
|
|
expandedIds?: ReadonlyArray<TreeNodeId>;
|
|
|
|
/**
|
|
* Specify the TreeView size
|
|
* @default "default"
|
|
*/
|
|
size?: "default" | "compact";
|
|
|
|
/**
|
|
* Specify the label text
|
|
* @default ""
|
|
*/
|
|
labelText?: string;
|
|
|
|
/**
|
|
* Set to `true` to visually hide the label text
|
|
* @default false
|
|
*/
|
|
hideLabel?: boolean;
|
|
|
|
[key: `data-${string}`]: any;
|
|
};
|
|
|
|
export type TreeViewProps = Omit<$RestProps, keyof $Props> & $Props;
|
|
|
|
export default class TreeView extends SvelteComponentTyped<
|
|
TreeViewProps,
|
|
{
|
|
select: CustomEvent<TreeNode & { expanded: boolean; leaf: boolean }>;
|
|
toggle: CustomEvent<TreeNode & { expanded: boolean; leaf: boolean }>;
|
|
focus: CustomEvent<TreeNode & { expanded: boolean; leaf: boolean }>;
|
|
keydown: WindowEventMap["keydown"];
|
|
},
|
|
{
|
|
default: {
|
|
node: {
|
|
id: TreeNodeId;
|
|
text: string;
|
|
expanded: boolean;
|
|
leaf: boolean;
|
|
disabled: boolean;
|
|
selected: boolean;
|
|
};
|
|
};
|
|
labelText: {};
|
|
}
|
|
> {
|
|
/**
|
|
* Programmatically expand all nodes
|
|
*/
|
|
expandAll: () => void;
|
|
|
|
/**
|
|
* Programmatically collapse all nodes
|
|
*/
|
|
collapseAll: () => void;
|
|
|
|
/**
|
|
* Programmatically expand a subset of nodes.
|
|
* Expands all nodes if no argument is provided
|
|
*/
|
|
expandNodes: (filterId?: (node: TreeNode) => boolean) => void;
|
|
|
|
/**
|
|
* Programmatically collapse a subset of nodes.
|
|
* Collapses all nodes if no argument is provided
|
|
*/
|
|
collapseNodes: (filterId?: (node: TreeNode) => boolean) => void;
|
|
|
|
/**
|
|
* Programmatically show a node by `id`.
|
|
* The matching node will be expanded, selected, and focused
|
|
*/
|
|
showNode: (id: TreeNodeId) => void;
|
|
}
|