|
8 | 8 | HostListener, |
9 | 9 | Input, |
10 | 10 | NgZone, |
| 11 | + OnChanges, |
11 | 12 | Renderer2, |
| 13 | + SimpleChanges, |
12 | 14 | TemplateRef, |
13 | 15 | ViewChild |
14 | 16 | } from "@angular/core"; |
@@ -47,7 +49,7 @@ import { PopoverContainer } from "carbon-components-angular/popover"; |
47 | 49 | </span> |
48 | 50 | ` |
49 | 51 | }) |
50 | | -export class Tooltip extends PopoverContainer implements AfterContentChecked { |
| 52 | +export class Tooltip extends PopoverContainer implements OnChanges, AfterContentChecked { |
51 | 53 | static tooltipCount = 0; |
52 | 54 |
|
53 | 55 | @HostBinding("class.cds--tooltip") tooltipClass = true; |
@@ -132,6 +134,34 @@ export class Tooltip extends PopoverContainer implements AfterContentChecked { |
132 | 134 | return value instanceof TemplateRef; |
133 | 135 | } |
134 | 136 |
|
| 137 | + /** |
| 138 | + * Close the popover and reopen it with updated values without emitting an event |
| 139 | + * @param changes |
| 140 | + */ |
| 141 | + ngOnChanges(changes: SimpleChanges): void { |
| 142 | + // Close and reopen the popover, handle alignment/programmatic open/close |
| 143 | + const originalState = this.isOpen; |
| 144 | + this.handleChange(false); |
| 145 | + |
| 146 | + // Ignore first change since content is not initialized |
| 147 | + if ((changes.autoAlign && !changes.autoAlign.firstChange) |
| 148 | + || (changes.disabled && !changes.disabled.firstChange && !changes.disabled.currentValue)) { |
| 149 | + /** |
| 150 | + * When `disabled` is `true`, popover content node is removed. So when re-enabling `disabled`, |
| 151 | + * we manually update view so querySelector can detect the popover content node. |
| 152 | + * Otherwise, the position of the popover will be incorrect when autoAlign is enabled. |
| 153 | + */ |
| 154 | + this.changeDetectorRef.detectChanges(); |
| 155 | + |
| 156 | + // Reset the inline styles |
| 157 | + this.popoverContentRef = this.elementRef.nativeElement.querySelector(".cds--popover-content"); |
| 158 | + this.popoverContentRef.setAttribute("style", ""); |
| 159 | + this.caretRef = this.elementRef.nativeElement.querySelector("span.cds--popover-caret"); |
| 160 | + } |
| 161 | + |
| 162 | + this.handleChange(originalState); |
| 163 | + } |
| 164 | + |
135 | 165 | /** |
136 | 166 | * Check for any changes in the projected content & apply accessibility attribute if needed |
137 | 167 | */ |
|
0 commit comments