Skip to content

Commit c602406

Browse files
authored
Merge pull request #844 from umbraco/v1/bugfix/label-empty-textnodes
fix: label does not render if there are empty text nodes inside the default slot
2 parents dc7594d + 48ba4c9 commit c602406

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
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.story.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ const Template: Story = props => {
108108
.rel=${props.rel}
109109
look=${props.look}
110110
color=${props.color}
111-
label=${props.label}
112-
>${props.content}</uui-button
113-
>
111+
label=${props.label}>
112+
${props.slot}
113+
</uui-button>
114114
`;
115115
};
116116

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', () => {

packages/uui-toggle/lib/uui-toggle.story.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export const AAAOverview: Story = props => html`
4040
.labelPosition=${props.labelPosition}
4141
?disabled=${props.disabled}
4242
?readonly=${props.readonly}
43-
?checked=${props.checked}
44-
>${props.slot}</uui-toggle
45-
>
43+
?checked=${props.checked}>
44+
${props.slot}
45+
</uui-toggle>
4646
`;
4747
AAAOverview.storyName = 'Overview';
4848

0 commit comments

Comments
 (0)