Skip to content

Commit 4e38f69

Browse files
andrewseguinmmalerba
authored andcommitted
feat(tooltip): allow tooltip be disabled (#3578)
* feat(tooltip): allow tooltip be disabled * hide if disabled while shown
1 parent 7cfece2 commit 4e38f69

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

src/demo-app/tooltip/tooltip-demo.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ <h1>Tooltip Demo</h1>
77
color="primary"
88
[mdTooltip]="message"
99
[mdTooltipPosition]="position"
10+
[mdTooltipDisabled]="disabled"
1011
[mdTooltipShowDelay]="showDelay"
1112
[mdTooltipHideDelay]="hideDelay">
1213
Mouse over to see the tooltip
@@ -25,12 +26,16 @@ <h1>Tooltip Demo</h1>
2526
<md-radio-button value="after">After</md-radio-button>
2627
</md-radio-group>
2728
</p>
28-
2929
<p>
3030
<strong>Message: </strong>
3131
<md-input-container><input mdInput type="text" [(ngModel)]="message"></md-input-container>
3232
</p>
3333

34+
<p>
35+
<strong>Disabled: </strong>
36+
<md-checkbox [(ngModel)]="disabled"></md-checkbox>
37+
</p>
38+
3439
<p>
3540
<strong>Show Delay (ms): </strong>
3641
<md-input-container>

src/demo-app/tooltip/tooltip-demo.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {TooltipPosition} from '@angular/material';
1212
export class TooltipDemo {
1313
position: TooltipPosition = 'below';
1414
message: string = 'Here is the tooltip';
15+
disabled = false;
1516
showDelay = 0;
1617
hideDelay = 1000;
1718
}

src/lib/tooltip/tooltip.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ delay of 1500ms. The longpress behavior requires HammerJS to be loaded on the pa
3333
The tooltip can also be shown and hidden through the `show` and `hide` directive methods,
3434
which both accept a number in milliseconds to delay before applying the display change.
3535

36+
To turn off the tooltip and prevent it from showing to the user, use the `mdTooltipDisabled` input flag.
37+

src/lib/tooltip/tooltip.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ describe('MdTooltip', () => {
112112
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
113113
}));
114114

115+
it('should not show if disabled', fakeAsync(() => {
116+
// Test that disabling the tooltip will not set the tooltip visible
117+
tooltipDirective.disabled = true;
118+
tooltipDirective.show();
119+
fixture.detectChanges();
120+
tick(0);
121+
expect(tooltipDirective._isTooltipVisible()).toBe(false);
122+
123+
// Test to make sure setting disabled to false will show the tooltip
124+
// Sanity check to make sure everything was correct before (detectChanges, tick)
125+
tooltipDirective.disabled = false;
126+
tooltipDirective.show();
127+
fixture.detectChanges();
128+
tick(0);
129+
expect(tooltipDirective._isTooltipVisible()).toBe(true);
130+
}));
131+
132+
it('should hide if disabled while visible', fakeAsync(() => {
133+
// Display the tooltip with a timeout before hiding.
134+
tooltipDirective.hideDelay = 1000;
135+
tooltipDirective.show();
136+
fixture.detectChanges();
137+
tick(0);
138+
expect(tooltipDirective._isTooltipVisible()).toBe(true);
139+
140+
// Set tooltip to be disabled and verify that the tooltip hides.
141+
tooltipDirective.disabled = true;
142+
tick(0);
143+
expect(tooltipDirective._isTooltipVisible()).toBe(false);
144+
}));
145+
115146
it('should not show if hide is called before delay finishes', fakeAsync(() => {
116147
expect(tooltipDirective._tooltipInstance).toBeUndefined();
117148

src/lib/tooltip/tooltip.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {Platform} from '../core/platform/index';
3333
import 'rxjs/add/operator/first';
3434
import {ScrollDispatcher} from '../core/overlay/scroll/scroll-dispatcher';
3535
import {Subscription} from 'rxjs/Subscription';
36+
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
3637

3738
export type TooltipPosition = 'left' | 'right' | 'above' | 'below' | 'before' | 'after';
3839

@@ -62,6 +63,7 @@ export class MdTooltip implements OnInit, OnDestroy {
6263
scrollSubscription: Subscription;
6364

6465
private _position: TooltipPosition = 'below';
66+
private _disabled: boolean = false;
6567

6668
/** Allows the user to define the position of the tooltip relative to the parent element */
6769
@Input('mdTooltipPosition')
@@ -78,6 +80,18 @@ export class MdTooltip implements OnInit, OnDestroy {
7880
}
7981
}
8082

83+
/** Disables the display of the tooltip. */
84+
@Input('mdTooltipDisabled')
85+
get disabled(): boolean { return this._disabled; }
86+
set disabled(value) {
87+
this._disabled = coerceBooleanProperty(value);
88+
89+
// If tooltip is disabled, hide immediately.
90+
if (this._disabled) {
91+
this.hide(0);
92+
}
93+
}
94+
8195
/** @deprecated */
8296
@Input('tooltip-position')
8397
get _positionDeprecated(): TooltipPosition { return this._position; }
@@ -115,6 +129,11 @@ export class MdTooltip implements OnInit, OnDestroy {
115129
get _matPosition() { return this.position; }
116130
set _matPosition(v) { this.position = v; }
117131

132+
// Properties with `mat-` prefix for noconflict mode.
133+
@Input('matTooltipDisabled')
134+
get _matDisabled() { return this.disabled; }
135+
set _matDisabled(v) { this.disabled = v; }
136+
118137
// Properties with `mat-` prefix for noconflict mode.
119138
@Input('matTooltipHideDelay')
120139
get _matHideDelay() { return this.hideDelay; }
@@ -168,7 +187,7 @@ export class MdTooltip implements OnInit, OnDestroy {
168187

169188
/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */
170189
show(delay: number = this.showDelay): void {
171-
if (!this._message || !this._message.trim()) { return; }
190+
if (this.disabled || !this._message || !this._message.trim()) { return; }
172191

173192
if (!this._tooltipInstance) {
174193
this._createTooltip();
@@ -192,7 +211,7 @@ export class MdTooltip implements OnInit, OnDestroy {
192211

193212
/** Returns true if the tooltip is currently visible to the user */
194213
_isTooltipVisible(): boolean {
195-
return this._tooltipInstance && this._tooltipInstance.isVisible();
214+
return !!this._tooltipInstance && this._tooltipInstance.isVisible();
196215
}
197216

198217
/** Create the tooltip to display */

0 commit comments

Comments
 (0)