Skip to content

Commit

Permalink
fix(badge): hide badges with no content (#12239)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisbeto authored and josephperrott committed Jul 18, 2018
1 parent 7f8fd9f commit 701a0dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/lib/badge/badge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ describe('MatBadge', () => {
expect(badgeContent.getAttribute('aria-describedby')).toBeFalsy();
});

it('should toggle visibility based on whether the badge has content', () => {
const classList = badgeNativeElement.classList;

expect(classList.contains('mat-badge-hidden')).toBe(false);

testComponent.badgeContent = '';
fixture.detectChanges();

expect(classList.contains('mat-badge-hidden')).toBe(true);

testComponent.badgeContent = 'hello';
fixture.detectChanges();

expect(classList.contains('mat-badge-hidden')).toBe(false);

testComponent.badgeContent = ' ';
fixture.detectChanges();

expect(classList.contains('mat-badge-hidden')).toBe(true);

testComponent.badgeContent = 0;
fixture.detectChanges();

expect(classList.contains('mat-badge-hidden')).toBe(false);
});

});

/** Test component that contains a MatBadge. */
Expand All @@ -132,7 +158,7 @@ describe('MatBadge', () => {
})
class BadgeTestApp {
badgeColor: ThemePalette;
badgeContent = '1';
badgeContent: string | number = '1';
badgeDirection = 'above after';
badgeHidden = false;
badgeSize = 'medium';
Expand Down
9 changes: 6 additions & 3 deletions src/lib/badge/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
'[class.mat-badge-small]': 'size === "small"',
'[class.mat-badge-medium]': 'size === "medium"',
'[class.mat-badge-large]': 'size === "large"',
'[class.mat-badge-hidden]': 'hidden',
'[class.mat-badge-hidden]': 'hidden || !_hasContent',
},
})
export class MatBadge implements OnDestroy {
/** Whether the badge has any content. */
_hasContent = false;

/** The color of the badge. Can be `primary`, `accent`, or `warn`. */
@Input('matBadgeColor')
Expand Down Expand Up @@ -62,8 +64,9 @@ export class MatBadge implements OnDestroy {
/** The content for the badge */
@Input('matBadge')
get content(): string { return this._content; }
set content(val: string) {
this._content = val;
set content(value: string) {
this._content = value;
this._hasContent = value != null && `${value}`.trim().length > 0;
this._updateTextContent();
}
private _content: string;
Expand Down

0 comments on commit 701a0dd

Please sign in to comment.