File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff line change @@ -47,10 +47,19 @@ export const LabelMixin = <T extends Constructor<LitElement>>(
47
47
@state ( )
48
48
private _labelSlotHasContent = false ;
49
49
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
+ ) ;
54
63
}
55
64
56
65
/**
Original file line number Diff line number Diff line change @@ -119,6 +119,19 @@ describe('UuiButton', () => {
119
119
const slot = element . shadowRoot ! . querySelector ( 'button' ) ! ;
120
120
expect ( slot ) . to . exist ;
121
121
} ) ;
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 "> </ 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
+ } ) ;
122
135
} ) ;
123
136
124
137
describe ( 'events' , ( ) => {
You can’t perform that action at this time.
0 commit comments