Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tree) icons rendering as null #1922

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/pages/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ New versions of Shoelace are released as-needed and generally occur when a criti
- Added the `hover-bridge` feature to `<sl-popup>` to support better tooltip accessibility [#1734]
- Added the `loading` attribute and the `spinner` and `spinner__base` part to `<sl-menu-item>` [#1700]
- Fixed files that did not have `.js` extensions. [#1770]
- Fixed a bug in `<sl-tree>` when providing custom expand / collapse icons [#1922]
- Fixed `<sl-dialog>` not accounting for elements with hidden dialog controls like `<video>` [#1755]
- Fixed focus trapping not scrolling elements into view. [#1750]
- Fixed more performance issues with focus trapping performance. [#1750]
Expand Down
8 changes: 6 additions & 2 deletions src/components/tree/tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 25 additions & 0 deletions src/components/tree/tree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,29 @@ describe('<sl-tree>', () => {
});
});
});

// 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<SlTree>(html`
<sl-tree>
<sl-tree-item>
Item 1
<sl-icon name="1-circle" slot="expand-icon"></sl-icon>
<sl-tree-item> Item A </sl-tree-item>
</sl-tree-item>
<sl-tree-item>
Item 2
<sl-tree-item>Item A</sl-tree-item>
<sl-tree-item>Item B</sl-tree-item>
</sl-tree-item>
<sl-tree-item>
Item 3
<sl-tree-item>Item A</sl-tree-item>
<sl-tree-item>Item B</sl-tree-item>
</sl-tree-item>
</sl-tree>
`);

expect(tree.textContent).to.not.includes('null');
});
});
Loading