Skip to content

Commit f889547

Browse files
crisbetoandrewseguin
authored andcommitted
refactor(badge): rework to account for ivy changes (#15555)
Reworks `matBadge` to account for Ivy setting static inputs before the view has been rendered out. This caused one of the tests to fail, because it relied on the view being in place when the `content` setter is called.
1 parent 177a433 commit f889547

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/lib/badge/badge.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import {
1515
Inject,
1616
Input,
1717
NgZone,
18+
OnChanges,
1819
OnDestroy,
1920
Optional,
2021
Renderer2,
22+
SimpleChanges,
2123
} from '@angular/core';
22-
import {ThemePalette, mixinDisabled, CanDisableCtor, CanDisable} from '@angular/material/core';
24+
import {CanDisable, CanDisableCtor, mixinDisabled, ThemePalette} from '@angular/material/core';
2325
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
2426

2527

@@ -53,7 +55,7 @@ export type MatBadgeSize = 'small' | 'medium' | 'large';
5355
'[class.mat-badge-disabled]': 'disabled',
5456
},
5557
})
56-
export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisable {
58+
export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges, CanDisable {
5759
/** Whether the badge has any content. */
5860
_hasContent = false;
5961

@@ -81,14 +83,7 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
8183
@Input('matBadgePosition') position: MatBadgePosition = 'above after';
8284

8385
/** The content for the badge */
84-
@Input('matBadge')
85-
get content(): string { return this._content; }
86-
set content(value: string) {
87-
this._content = value;
88-
this._hasContent = value != null && `${value}`.trim().length > 0;
89-
this._updateTextContent();
90-
}
91-
private _content: string;
86+
@Input('matBadge') content: string;
9287

9388
/** Message used to describe the decorated element via aria-describedby */
9489
@Input('matBadgeDescription')
@@ -144,6 +139,16 @@ export class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisabl
144139
return this.position.indexOf('before') === -1;
145140
}
146141

142+
ngOnChanges(changes: SimpleChanges) {
143+
const contentChange = changes['content'];
144+
145+
if (contentChange) {
146+
const value = contentChange.currentValue;
147+
this._hasContent = value != null && `${value}`.trim().length > 0;
148+
this._updateTextContent();
149+
}
150+
}
151+
147152
ngOnDestroy() {
148153
const badgeElement = this._badgeElement;
149154

tools/public_api_guard/lib/badge.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export declare const _MatBadgeMixinBase: CanDisableCtor & typeof MatBadgeBase;
22

3-
export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, CanDisable {
3+
export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, OnChanges, CanDisable {
44
_hasContent: boolean;
55
_id: number;
66
color: ThemePalette;
@@ -14,6 +14,7 @@ export declare class MatBadge extends _MatBadgeMixinBase implements OnDestroy, C
1414
_renderer?: Renderer2 | undefined, _animationMode?: string | undefined);
1515
isAbove(): boolean;
1616
isAfter(): boolean;
17+
ngOnChanges(changes: SimpleChanges): void;
1718
ngOnDestroy(): void;
1819
}
1920

0 commit comments

Comments
 (0)