Skip to content

Commit 2e18570

Browse files
crisbetojelbourn
authored andcommitted
refactor(badge): add method that exposes badge content element (#15869)
Adds a method that exposes the underlying badge content element. This allows consumers to add their own custom attributes to the badge element, without us having to proxy them through inputs. Fixes #15801.
1 parent fc0b971 commit 2e18570

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/material/badge/badge.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ComponentFixture, TestBed, fakeAsync} from '@angular/core/testing';
2-
import {Component, DebugElement, ViewEncapsulation} from '@angular/core';
2+
import {Component, DebugElement, ViewEncapsulation, ViewChild} from '@angular/core';
33
import {By} from '@angular/platform-browser';
44
import {MatBadge, MatBadgeModule} from './index';
55
import {ThemePalette} from '@angular/material/core';
@@ -200,6 +200,11 @@ describe('MatBadge', () => {
200200
expect(preExistingFixture.nativeElement.querySelectorAll('.mat-badge-content').length).toBe(2);
201201
});
202202

203+
it('should expose the badge element', () => {
204+
const badgeElement = badgeNativeElement.querySelector('.mat-badge-content')!;
205+
expect(fixture.componentInstance.badgeInstance.getBadgeElement()).toBe(badgeElement);
206+
});
207+
203208
});
204209

205210
/** Test component that contains a MatBadge. */
@@ -221,6 +226,7 @@ describe('MatBadge', () => {
221226
`
222227
})
223228
class BadgeTestApp {
229+
@ViewChild(MatBadge, {static: false}) badgeInstance: MatBadge;
224230
badgeColor: ThemePalette;
225231
badgeContent: string | number = '1';
226232
badgeDirection = 'above after';

src/material/badge/badge.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
115115
/** Unique id for the badge */
116116
_id: number = nextId++;
117117

118-
private _badgeElement: HTMLElement;
118+
private _badgeElement: HTMLElement | undefined;
119119

120120
constructor(
121121
private _ngZone: NgZone,
@@ -162,6 +162,14 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges
162162
}
163163
}
164164

165+
/**
166+
* Gets the element into which the badge's content is being rendered.
167+
* Undefined if the element hasn't been created (e.g. if the badge doesn't have content).
168+
*/
169+
getBadgeElement(): HTMLElement | undefined {
170+
return this._badgeElement;
171+
}
172+
165173
/** Injects a span element into the DOM with the content. */
166174
private _updateTextContent(): HTMLSpanElement {
167175
if (!this._badgeElement) {

tools/public_api_guard/material/badge.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, O
99
position: MatBadgePosition;
1010
size: MatBadgeSize;
1111
constructor(_ngZone: NgZone, _elementRef: ElementRef<HTMLElement>, _ariaDescriber: AriaDescriber, _renderer: Renderer2, _animationMode?: string | undefined);
12+
getBadgeElement(): HTMLElement | undefined;
1213
isAbove(): boolean;
1314
isAfter(): boolean;
1415
ngOnChanges(changes: SimpleChanges): void;

0 commit comments

Comments
 (0)