Skip to content

Commit ec197fc

Browse files
committed
fix: ensure that empty text nodes do not make the label mixin think it is non-empty
1 parent ea8bf2a commit ec197fc

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

packages/uui-base/lib/mixins/LabelMixin.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,19 @@ export const LabelMixin = <T extends Constructor<LitElement>>(
4747
@state()
4848
private _labelSlotHasContent = false;
4949

50-
private labelSlotChanged(e: any): void {
51-
this._labelSlotHasContent =
52-
(e.target as HTMLSlotElement).assignedNodes({ flatten: true }).length >
53-
0;
50+
private labelSlotChanged(e: Event): void {
51+
const nodes = (e.target as HTMLSlotElement).assignedNodes();
52+
53+
if (!nodes.length) {
54+
this._labelSlotHasContent = false;
55+
return;
56+
}
57+
58+
// If some nodes are not TEXT_NODE, or if one of the nodes is not empty, set the slot as having content
59+
this._labelSlotHasContent = nodes.some(
60+
node =>
61+
node.nodeType !== Node.TEXT_NODE || !!node.textContent?.trim().length,
62+
);
5463
}
5564

5665
/**

packages/uui-button/lib/uui-button.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ describe('UuiButton', () => {
119119
const slot = element.shadowRoot!.querySelector('button')!;
120120
expect(slot).to.exist;
121121
});
122+
it('label property is used when no default slot is provided', async () => {
123+
const element = await fixture(
124+
html` <uui-button label="My label"> &nbsp; </uui-button>`,
125+
);
126+
expect(element.shadowRoot?.textContent).to.include('My label');
127+
});
128+
it('default slot takes precedence over label property', async () => {
129+
element.label = 'My label';
130+
await elementUpdated(element);
131+
const slot = element.shadowRoot!.querySelector('slot')!;
132+
expect(slot.assignedNodes().length).to.equal(1);
133+
expect(slot.assignedNodes()[0].textContent).to.equal('Hello uui-button');
134+
});
122135
});
123136

124137
describe('events', () => {

0 commit comments

Comments
 (0)