Skip to content

Commit 7fda0f7

Browse files
committed
fix(tree-view): use correct leaf depth to restore active node style
1 parent 6163d42 commit 7fda0f7

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/TreeView/TreeViewNode.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
focusNode,
8888
} = getContext("carbon:TreeView");
8989
const offset = () =>
90-
computeTreeLeafDepth(refLabel) + (leaf && icon ? 2 : 2.5);
90+
computeTreeLeafDepth(refLabel) - 1 + (leaf && icon ? 2 : 2.5);
9191
9292
afterUpdate(() => {
9393
if (id === $activeNodeId && prevActiveId !== $activeNodeId) {

src/TreeView/TreeViewNodeList.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
} = getContext("carbon:TreeView");
4242
4343
const offset = () => {
44-
const depth = computeTreeLeafDepth(refLabel);
44+
const depth = computeTreeLeafDepth(refLabel) - 1;
4545
4646
if (parent) return depth + 1;
4747
if (icon) return depth + 2;

tests/TreeView/TreeView.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,64 @@ describe("TreeView Props", () => {
346346
expect(node2).toHaveClass("bx--tree-node--active");
347347
});
348348

349+
describe("active node label indentation regression", () => {
350+
it("applies correct offset to root-level leaf node label (prevents active indicator clipping)", () => {
351+
render(TreeViewProps, { activeId: 1 });
352+
353+
const activeNode = screen.getByRole("treeitem", { name: /Node 2/ });
354+
const label = activeNode.querySelector(".bx--tree-node__label");
355+
expect.assert(label instanceof HTMLElement);
356+
expect(label.style.paddingLeft).toBe("2.5rem");
357+
expect(label.style.marginLeft).toBe("-2.5rem");
358+
});
359+
360+
it("applies correct offset to nested leaf node label when expanded", () => {
361+
render(TreeViewProps, {
362+
activeId: 5,
363+
expandedIds: [1],
364+
nodes: [
365+
{ id: 0, text: "Node 1" },
366+
{
367+
id: 1,
368+
text: "Node 2",
369+
nodes: [
370+
{ id: 2, text: "Node 2a" },
371+
{ id: 5, text: "Node 2b" },
372+
],
373+
},
374+
],
375+
});
376+
377+
const activeNode = screen.getByRole("treeitem", { current: true });
378+
const label = activeNode.querySelector(".bx--tree-node__label");
379+
expect.assert(label instanceof HTMLElement);
380+
// Nested at depth 2: offset = (2 - 1) + 2.5 = 3.5rem
381+
expect(label.style.paddingLeft).toBe("3.5rem");
382+
expect(label.style.marginLeft).toBe("-3.5rem");
383+
});
384+
385+
it("applies correct offset to root-level parent node label", () => {
386+
render(TreeViewProps, {
387+
activeId: 1,
388+
nodes: [
389+
{ id: 0, text: "Node 1" },
390+
{
391+
id: 1,
392+
text: "Node 2",
393+
nodes: [{ id: 2, text: "Node 2a" }],
394+
},
395+
],
396+
});
397+
398+
const activeNode = screen.getByRole("treeitem", { name: /Node 2/ });
399+
const label = activeNode.querySelector(".bx--tree-node__label");
400+
expect.assert(label instanceof HTMLElement);
401+
// Root parent: offset = (1 - 1) + 1 = 1rem
402+
expect(label.style.paddingLeft).toBe("1rem");
403+
expect(label.style.marginLeft).toBe("-1rem");
404+
});
405+
});
406+
349407
it("handles multiple selectedIds", () => {
350408
render(TreeViewMultiselect, { selectedIds: [0, 7, 9] });
351409

0 commit comments

Comments
 (0)