Skip to content

Commit 9401323

Browse files
Eyasmmalerba
andauthored
Allow undefined & null values in [matTooltip] explicitly (#29397)
* fix(material/tooltip): Update MatTooltip's "message not present" test to use null/undefined types outright. We were using nullness assertions to cause these assignments to succeed, but we should allow users to set null/undefined values outright. Right now we don't, so this update will cause the tests to fail. But this will be soon fixed. * fix(material/tooltip): Allow null and undefined values for `[matTooltip]` explicitly. We have tests that already set these values (but used to use nullness assertions to overcome the build failures) and protect against it by treating these values as empty strings (and thus we hide the tooltip), but we don't allow anyone who uses templates with strict type checking to set optional values unless they add their own nullness assertion. * fix(material/tooltip): Also explicitly define the type of `MatTooltip.message` getter as always a string. We do this since the underlying `._message` type is a string, the message setter can accept null and undefined values, but will convert them to strings before saving to `_message`, so we can always assume someone _reading_ the value will see a plain string. * ci: Approve new API goldens for tooltip --------- Co-authored-by: Miles Malerba <[email protected]>
1 parent a454216 commit 9401323

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

src/material/tooltip/tooltip.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,12 @@ describe('MDC-based MatTooltip', () => {
413413
it('should not show tooltip if message is not present or empty', () => {
414414
assertTooltipInstance(tooltipDirective, false);
415415

416-
tooltipDirective.message = undefined!;
416+
tooltipDirective.message = undefined;
417417
fixture.detectChanges();
418418
tooltipDirective.show();
419419
assertTooltipInstance(tooltipDirective, false);
420420

421-
tooltipDirective.message = null!;
421+
tooltipDirective.message = null;
422422
fixture.detectChanges();
423423
tooltipDirective.show();
424424
assertTooltipInstance(tooltipDirective, false);

src/material/tooltip/tooltip.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
296296

297297
/** The message to be displayed in the tooltip */
298298
@Input('matTooltip')
299-
get message() {
299+
get message(): string {
300300
return this._message;
301301
}
302302

303-
set message(value: string) {
303+
set message(value: string | null | undefined) {
304304
this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this._message, 'tooltip');
305305

306306
// If the message is not a string (e.g. number), convert it to a string and trim it.

tools/public_api_guard/material/tooltip.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
7676
set hideDelay(value: NumberInput);
7777
_isTooltipVisible(): boolean;
7878
get message(): string;
79-
set message(value: string);
79+
set message(value: string | null | undefined);
8080
// (undocumented)
8181
ngAfterViewInit(): void;
8282
ngOnDestroy(): void;

0 commit comments

Comments
 (0)