Skip to content

Commit ab78183

Browse files
crisbetommalerba
authored andcommittedDec 7, 2018
fix(badge): aria-label not being updated if description changes (#14393)
When we create a new badge element, we set its `aria-label` to the description of the badge, however that `aria-label` doesn't update if the description changes. These changes update the label accordingly.
1 parent b2788bf commit ab78183

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
 

‎src/lib/badge/badge.spec.ts

+19
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,25 @@ describe('MatBadge', () => {
165165
expect(element.classList).toContain('mat-badge-disabled');
166166
});
167167

168+
it('should update the aria-label if the description changes', () => {
169+
const badgeContent = badgeNativeElement.querySelector('.mat-badge-content')!;
170+
171+
fixture.componentInstance.badgeDescription = 'initial content';
172+
fixture.detectChanges();
173+
174+
expect(badgeContent.getAttribute('aria-label')).toBe('initial content');
175+
176+
fixture.componentInstance.badgeDescription = 'changed content';
177+
fixture.detectChanges();
178+
179+
expect(badgeContent.getAttribute('aria-label')).toBe('changed content');
180+
181+
fixture.componentInstance.badgeDescription = '';
182+
fixture.detectChanges();
183+
184+
expect(badgeContent.hasAttribute('aria-label')).toBe(false);
185+
});
186+
168187
});
169188

170189
/** Test component that contains a MatBadge. */

‎src/lib/badge/badge.ts

+6
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
9494
get description(): string { return this._description; }
9595
set description(newDescription: string) {
9696
if (newDescription !== this._description) {
97+
const badgeElement = this._badgeElement;
9798
this._updateHostAriaDescription(newDescription, this._description);
9899
this._description = newDescription;
100+
101+
if (badgeElement) {
102+
newDescription ? badgeElement.setAttribute('aria-label', newDescription) :
103+
badgeElement.removeAttribute('aria-label');
104+
}
99105
}
100106
}
101107
private _description: string;

0 commit comments

Comments
 (0)
Please sign in to comment.