File tree Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Expand file tree Collapse file tree 2 files changed +33
-4
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,32 @@ describe('MatBadge', () => {
114
114
expect ( badgeContent . getAttribute ( 'aria-describedby' ) ) . toBeFalsy ( ) ;
115
115
} ) ;
116
116
117
+ it ( 'should toggle visibility based on whether the badge has content' , ( ) => {
118
+ const classList = badgeNativeElement . classList ;
119
+
120
+ expect ( classList . contains ( 'mat-badge-hidden' ) ) . toBe ( false ) ;
121
+
122
+ testComponent . badgeContent = '' ;
123
+ fixture . detectChanges ( ) ;
124
+
125
+ expect ( classList . contains ( 'mat-badge-hidden' ) ) . toBe ( true ) ;
126
+
127
+ testComponent . badgeContent = 'hello' ;
128
+ fixture . detectChanges ( ) ;
129
+
130
+ expect ( classList . contains ( 'mat-badge-hidden' ) ) . toBe ( false ) ;
131
+
132
+ testComponent . badgeContent = ' ' ;
133
+ fixture . detectChanges ( ) ;
134
+
135
+ expect ( classList . contains ( 'mat-badge-hidden' ) ) . toBe ( true ) ;
136
+
137
+ testComponent . badgeContent = 0 ;
138
+ fixture . detectChanges ( ) ;
139
+
140
+ expect ( classList . contains ( 'mat-badge-hidden' ) ) . toBe ( false ) ;
141
+ } ) ;
142
+
117
143
} ) ;
118
144
119
145
/** Test component that contains a MatBadge. */
@@ -132,7 +158,7 @@ describe('MatBadge', () => {
132
158
} )
133
159
class BadgeTestApp {
134
160
badgeColor : ThemePalette ;
135
- badgeContent = '1' ;
161
+ badgeContent : string | number = '1' ;
136
162
badgeDirection = 'above after' ;
137
163
badgeHidden = false ;
138
164
badgeSize = 'medium' ;
Original file line number Diff line number Diff line change @@ -31,10 +31,12 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
31
31
'[class.mat-badge-small]' : 'size === "small"' ,
32
32
'[class.mat-badge-medium]' : 'size === "medium"' ,
33
33
'[class.mat-badge-large]' : 'size === "large"' ,
34
- '[class.mat-badge-hidden]' : 'hidden' ,
34
+ '[class.mat-badge-hidden]' : 'hidden || !_hasContent ' ,
35
35
} ,
36
36
} )
37
37
export class MatBadge implements OnDestroy {
38
+ /** Whether the badge has any content. */
39
+ _hasContent = false ;
38
40
39
41
/** The color of the badge. Can be `primary`, `accent`, or `warn`. */
40
42
@Input ( 'matBadgeColor' )
@@ -62,8 +64,9 @@ export class MatBadge implements OnDestroy {
62
64
/** The content for the badge */
63
65
@Input ( 'matBadge' )
64
66
get content ( ) : string { return this . _content ; }
65
- set content ( val : string ) {
66
- this . _content = val ;
67
+ set content ( value : string ) {
68
+ this . _content = value ;
69
+ this . _hasContent = value != null && `${ value } ` . trim ( ) . length > 0 ;
67
70
this . _updateTextContent ( ) ;
68
71
}
69
72
private _content : string ;
You can’t perform that action at this time.
0 commit comments