Skip to content

Commit d66284d

Browse files
crisbetotinayuangao
authored andcommitted
fix(tooltip): close tooltip if message is cleared while open (#8544)
Closes the tooltip if its message is cleared while it is open. Currently it'll continue showing a small grey rectangle under the trigger.
1 parent d2c11ca commit d66284d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/lib/tooltip/tooltip.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ describe('MatTooltip', () => {
191191
expect(tooltipDirective._isTooltipVisible()).toBe(false);
192192
}));
193193

194+
it('should hide if the message is cleared while the tooltip is open', fakeAsync(() => {
195+
tooltipDirective.show();
196+
fixture.detectChanges();
197+
tick(0);
198+
expect(tooltipDirective._isTooltipVisible()).toBe(true);
199+
200+
fixture.componentInstance.message = '';
201+
fixture.detectChanges();
202+
tick(0);
203+
expect(tooltipDirective._isTooltipVisible()).toBe(false);
204+
}));
205+
194206
it('should not show if hide is called before delay finishes', async(() => {
195207
assertTooltipInstance(tooltipDirective, false);
196208

src/lib/tooltip/tooltip.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,20 @@ export class MatTooltip implements OnDestroy {
142142
private _message = '';
143143

144144
/** The message to be displayed in the tooltip */
145-
@Input('matTooltip') get message() { return this._message; }
145+
@Input('matTooltip')
146+
get message() { return this._message; }
146147
set message(value: string) {
147148
this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this._message);
148149

149150
// If the message is not a string (e.g. number), convert it to a string and trim it.
150151
this._message = value != null ? `${value}`.trim() : '';
151-
this._updateTooltipMessage();
152-
this._ariaDescriber.describe(this._elementRef.nativeElement, this.message);
152+
153+
if (!this._message && this._isTooltipVisible()) {
154+
this.hide(0);
155+
} else {
156+
this._updateTooltipMessage();
157+
this._ariaDescriber.describe(this._elementRef.nativeElement, this.message);
158+
}
153159
}
154160

155161
/** Classes to be passed to the tooltip. Supports the same syntax as `ngClass`. */

0 commit comments

Comments
 (0)