Skip to content

Commit

Permalink
feat(tree-view): make node slottable (#1843)
Browse files Browse the repository at this point in the history
Closes #1660
  • Loading branch information
metonym authored Nov 12, 2023
1 parent 417102d commit 6a55fef
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 18 deletions.
7 changes: 4 additions & 3 deletions COMPONENT_INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -4707,9 +4707,10 @@ export interface TreeNode {

### Slots

| Slot name | Default | Props | Fallback |
| :-------- | :------ | :---- | :----------------------- |
| labelText | No | -- | <code>{labelText}</code> |
| Slot name | Default | Props | Fallback |
| :-------- | :------ | :-------------------------------------------------------------------------------------------------------------------------------- | :----------------------- |
| -- | Yes | <code>{ node: { id: TreeNodeId; text: string; expanded: boolean, leaf: boolean; disabled: boolean; selected: boolean; } } </code> | <code>{node.text}</code> |
| labelText | No | -- | <code>{labelText}</code> |

### Events

Expand Down
6 changes: 6 additions & 0 deletions docs/src/COMPONENT_API.json
Original file line number Diff line number Diff line change
Expand Up @@ -14668,6 +14668,12 @@
],
"moduleExports": [],
"slots": [
{
"name": "__default__",
"default": true,
"fallback": "{node.text}",
"slot_props": "{ node: { id: TreeNodeId; text: string; expanded: boolean, leaf: boolean; disabled: boolean; selected: boolean; } }"
},
{
"name": "labelText",
"default": false,
Expand Down
19 changes: 18 additions & 1 deletion docs/src/pages/components/TreeView.svx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { InlineNotification } from "carbon-components-svelte";
import { InlineNotification, UnorderedList, ListItem } from "carbon-components-svelte";
import Preview from "../../components/Preview.svelte";
</script>

Expand All @@ -17,6 +17,23 @@ A parent node contains `children` while a leaf node does not.

<FileSource src="/framed/TreeView/TreeView" />

## Slottable node

By default, each item renders the value of `node.text`. Use the data from `let:node` directive to override the default slot.

The destructured `let:node` contains the following properties:

<UnorderedList svx-ignore style="margin-bottom: var(--cds-spacing-08)">
<ListItem><strong>id</strong>: the node id</ListItem>
<ListItem><strong>text</strong>: the node text</ListItem>
<ListItem><strong>expanded</strong>: true if the node is expanded</ListItem>
<ListItem><strong>leaf</strong>: true if the node does not have children</ListItem>
<ListItem><strong>disabled</strong>: true if the node is disabled</ListItem>
<ListItem><strong>selected</strong>: true if the node is selected</ListItem>
</UnorderedList>

<FileSource src="/framed/TreeView/TreeViewSlot" />

## Initial active node

The active node can be set through `activeId`.
Expand Down
61 changes: 61 additions & 0 deletions docs/src/pages/framed/TreeView/TreeViewSlot.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script>
import { TreeView } from "carbon-components-svelte";
let activeId = 0;
let selectedIds = [0, 7, 9];
let children = [
{ id: 0, text: "AI / Machine learning" },
{
id: 1,
text: "Analytics",
children: [
{
id: 2,
text: "IBM Analytics Engine",
children: [
{ id: 3, text: "Apache Spark" },
{ id: 4, text: "Hadoop" },
],
},
{ id: 5, text: "IBM Cloud SQL Query" },
{ id: 6, text: "IBM Db2 Warehouse on Cloud" },
],
},
{
id: 7,
text: "Blockchain",
children: [{ id: 8, text: "IBM Blockchain Platform" }],
},
{
id: 9,
text: "Databases",
children: [
{ id: 10, text: "IBM Cloud Databases for Elasticsearch" },
{ id: 11, text: "IBM Cloud Databases for Enterprise DB" },
{ id: 12, text: "IBM Cloud Databases for MongoDB" },
{ id: 13, text: "IBM Cloud Databases for PostgreSQL" },
],
},
{
id: 14,
text: "Integration",
disabled: true,
children: [{ id: 15, text: "IBM API Connect", disabled: true }],
},
];
</script>

<TreeView
labelText="Cloud Products"
activeId="{activeId}"
selectedIds="{selectedIds}"
children="{children}"
let:node
>
<span
style:color="{node.selected ? "var(--cds-interactive-04)" : "inherit"}"
style:text-decoration="{node.disabled ? "inherit" : "underline"}"
>
{node.text} (id: {node.id})
</span>
</TreeView>
7 changes: 6 additions & 1 deletion src/TreeView/TreeView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* @typedef {string | number} TreeNodeId
* @typedef {{ id: TreeNodeId; text: any; icon?: typeof import("svelte").SvelteComponent<any>; disabled?: boolean; children?: TreeNode[]; }} TreeNode
* @slot {{ node: { id: TreeNodeId; text: string; expanded: boolean, leaf: boolean; disabled: boolean; selected: boolean; } }}
* @event {TreeNode & { expanded: boolean; leaf: boolean; }} select
* @event {TreeNode & { expanded: boolean; leaf: boolean; }} toggle
* @event {TreeNode & { expanded: boolean; leaf: boolean; }} focus
Expand Down Expand Up @@ -202,5 +203,9 @@
on:keydown
on:keydown|stopPropagation="{handleKeyDown}"
>
<TreeViewNodeList root children="{children}" />
<TreeViewNodeList root children="{children}" let:node>
<slot node="{node}">
{node.text}
</slot>
</TreeViewNodeList>
</ul>
20 changes: 16 additions & 4 deletions src/TreeView/TreeViewNode.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<script>
/**
* @typedef {string | number} TreeNodeId
* @slot {{ node: { id: TreeNodeId; text: string; expanded: false, leaf: boolean; disabled: boolean; selected: boolean; } }}
*/
export let leaf = false;
Expand Down Expand Up @@ -68,7 +69,16 @@
prevActiveId = $activeNodeId;
});
$: node = { id, text, expanded: false, leaf };
$: selected = $selectedNodeIds.includes(id);
$: node = {
id,
text,
// A node cannot be expanded.
expanded: false,
leaf,
disabled,
selected,
};
$: if (refLabel) {
refLabel.style.marginLeft = `-${offset()}rem`;
refLabel.style.paddingLeft = `${offset()}rem`;
Expand All @@ -82,12 +92,12 @@
id="{id}"
tabindex="{disabled ? undefined : -1}"
aria-current="{id === $activeNodeId || undefined}"
aria-selected="{disabled ? undefined : $selectedNodeIds.includes(id)}"
aria-selected="{disabled ? undefined : selected}"
aria-disabled="{disabled}"
class:bx--tree-node="{true}"
class:bx--tree-leaf-node="{true}"
class:bx--tree-node--active="{id === $activeNodeId}"
class:bx--tree-node--selected="{$selectedNodeIds.includes(id)}"
class:bx--tree-node--selected="{selected}"
class:bx--tree-node--disabled="{disabled}"
class:bx--tree-node--with-icon="{icon}"
on:click|stopPropagation="{() => {
Expand Down Expand Up @@ -116,6 +126,8 @@
>
<div bind:this="{refLabel}" class:bx--tree-node__label="{true}">
<svelte:component this="{icon}" class="bx--tree-node__icon" />
{text}
<slot node="{node}">
{text}
</slot>
</div>
</li>
24 changes: 17 additions & 7 deletions src/TreeView/TreeViewNodeList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/**
* @typedef {string | number} TreeNodeId
* @typedef {{ id: TreeNodeId; text: string; disabled?: boolean; expanded?: boolean; }} TreeNode
* @slot {{ node: { id: TreeNodeId; text: string; expanded: boolean, leaf: boolean; disabled: boolean; selected: boolean; } }}
*/
/** @type {Array<TreeNode & { children?: TreeNode[] }>} */
Expand Down Expand Up @@ -66,25 +67,30 @@
{#if root}
{#each children as child (child.id)}
{#if Array.isArray(child.children)}
<svelte:self {...child} />
<svelte:self {...child} let:node>
<slot node="{node}" />
</svelte:self>
{:else}
<TreeViewNode leaf {...child} />
<TreeViewNode leaf {...child} let:node>
<slot node="{node}" />
</TreeViewNode>
{/if}
{/each}
{:else}
{@const selected = $selectedNodeIds.includes(id)}
<!-- svelte-ignore a11y-no-noninteractive-element-to-interactive-role -->
<li
bind:this="{ref}"
role="treeitem"
id="{id}"
tabindex="{disabled ? undefined : -1}"
aria-current="{id === $activeNodeId || undefined}"
aria-selected="{disabled ? undefined : $selectedNodeIds.includes(id)}"
aria-selected="{disabled ? undefined : selected}"
aria-disabled="{disabled}"
class:bx--tree-node="{true}"
class:bx--tree-parent-node="{true}"
class:bx--tree-node--active="{id === $activeNodeId}"
class:bx--tree-node--selected="{$selectedNodeIds.includes(id)}"
class:bx--tree-node--selected="{selected}"
class:bx--tree-node--disabled="{disabled}"
class:bx--tree-node--with-icon="{icon}"
aria-expanded="{expanded}"
Expand Down Expand Up @@ -151,16 +157,20 @@
</span>
<span class:bx--tree-node__label__details="{true}">
<svelte:component this="{icon}" class="bx--tree-node__icon" />
{text}
<slot node="{{ ...node, selected, disabled }}" />
</span>
</div>
{#if expanded}
<ul role="group" class:bx--tree-node__children="{true}">
{#each children as child (child.id)}
{#if Array.isArray(child.children)}
<svelte:self {...child} />
<svelte:self {...child} let:node>
<slot node="{node}" />
</svelte:self>
{:else}
<TreeViewNode leaf {...child} />
<TreeViewNode leaf {...child} let:node>
<slot node="{node}">{node.text}</slot>
</TreeViewNode>
{/if}
{/each}
</ul>
Expand Down
10 changes: 9 additions & 1 deletion tests/TreeView.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@
on:select="{({ detail }) => console.log('select', detail)}"
on:toggle="{({ detail }) => console.log('toggle', detail)}"
on:focus="{({ detail }) => console.log('focus', detail)}"
/>
let:node
>
{node.id}
{node.disabled}
{node.expanded}
{node.leaf}
{node.selected}
{node.text}
</TreeView>
14 changes: 13 additions & 1 deletion types/TreeView/TreeView.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,19 @@ export default class TreeView extends SvelteComponentTyped<
focus: CustomEvent<TreeNode & { expanded: boolean; leaf: boolean }>;
keydown: WindowEventMap["keydown"];
},
{ labelText: {} }
{
default: {
node: {
id: TreeNodeId;
text: string;
expanded: boolean;
leaf: boolean;
disabled: boolean;
selected: boolean;
};
};
labelText: {};
}
> {
/**
* Programmatically expand all nodes
Expand Down

0 comments on commit 6a55fef

Please sign in to comment.