From 418bd1c0d95e4ffe4e4fec15c5ecfc1b30482ab8 Mon Sep 17 00:00:00 2001 From: konnorrogers Date: Wed, 13 Mar 2024 14:24:19 -0400 Subject: [PATCH] fix icons rendering as null --- src/components/tree/tree.component.ts | 8 ++++++-- src/components/tree/tree.test.ts | 28 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/components/tree/tree.component.ts b/src/components/tree/tree.component.ts index 78123cca..34a90d7d 100644 --- a/src/components/tree/tree.component.ts +++ b/src/components/tree/tree.component.ts @@ -144,12 +144,16 @@ export default class SlTree extends ShoelaceElement { .forEach((status: 'expand' | 'collapse') => { const existingIcon = item.querySelector(`[slot="${status}-icon"]`); + const expandButtonIcon = this.getExpandButtonIcon(status) + + if (!expandButtonIcon) return + if (existingIcon === null) { // No separator exists, add one - item.append(this.getExpandButtonIcon(status)!); + item.append(expandButtonIcon); } else if (existingIcon.hasAttribute('data-default')) { // A default separator exists, replace it - existingIcon.replaceWith(this.getExpandButtonIcon(status)!); + existingIcon.replaceWith(expandButtonIcon); } else { // The user provided a custom icon, leave it alone } diff --git a/src/components/tree/tree.test.ts b/src/components/tree/tree.test.ts index aec85286..3b10f15c 100644 --- a/src/components/tree/tree.test.ts +++ b/src/components/tree/tree.test.ts @@ -752,4 +752,32 @@ describe('', () => { }); }); }); + + + // https://github.com/shoelace-style/shoelace/issues/1916 + it("Should not render 'null' if it can't find a custom icon", async () => { + const tree = await fixture(html` + + + Item 1 + + + Item A + + + + Item 2 + Item A + Item B + + + Item 3 + Item A + Item B + + + `) + + expect(tree.textContent).to.not.includes("null") + }) });