Skip to content

Commit

Permalink
feat(treeview): make node slottable
Browse files Browse the repository at this point in the history
Closes #1660
  • Loading branch information
metonym committed Nov 11, 2023
1 parent 417102d commit c6c7569
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
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

0 comments on commit c6c7569

Please sign in to comment.