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', () => {
114114 expect ( badgeContent . getAttribute ( 'aria-describedby' ) ) . toBeFalsy ( ) ;
115115 } ) ;
116116
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+
117143} ) ;
118144
119145/** Test component that contains a MatBadge. */
@@ -132,7 +158,7 @@ describe('MatBadge', () => {
132158} )
133159class BadgeTestApp {
134160 badgeColor : ThemePalette ;
135- badgeContent = '1' ;
161+ badgeContent : string | number = '1' ;
136162 badgeDirection = 'above after' ;
137163 badgeHidden = false ;
138164 badgeSize = 'medium' ;
Original file line number Diff line number Diff line change @@ -31,10 +31,12 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
3131 '[class.mat-badge-small]' : 'size === "small"' ,
3232 '[class.mat-badge-medium]' : 'size === "medium"' ,
3333 '[class.mat-badge-large]' : 'size === "large"' ,
34- '[class.mat-badge-hidden]' : 'hidden' ,
34+ '[class.mat-badge-hidden]' : 'hidden || !_hasContent ' ,
3535 } ,
3636} )
3737export class MatBadge implements OnDestroy {
38+ /** Whether the badge has any content. */
39+ _hasContent = false ;
3840
3941 /** The color of the badge. Can be `primary`, `accent`, or `warn`. */
4042 @Input ( 'matBadgeColor' )
@@ -62,8 +64,9 @@ export class MatBadge implements OnDestroy {
6264 /** The content for the badge */
6365 @Input ( 'matBadge' )
6466 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 ;
6770 this . _updateTextContent ( ) ;
6871 }
6972 private _content : string ;
You can’t perform that action at this time.
0 commit comments