Skip to content

Commit d6da331

Browse files
authored
feat(expansion-panel): show tooltip for title and description (#12878)
* feat(expansion-panel): show tooltip for title and description #12763 * refactor(expansion-panel): add private attr.title hostbinding * refactor(expansion-panel): tooltip content minor changes * chore(expansion-panel): update CHANGELOG * refactor(expansion-panel): add HeaderContentBaseDirective
1 parent 8925920 commit d6da331

File tree

4 files changed

+112
-13
lines changed

4 files changed

+112
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ All notable changes for each version of this project will be documented in this
55
## 16.0.0
66

77
### New Features
8+
- `IgxExpansionPanel`:
9+
- `IgxExpansionPanelTitleDirective` and `IgxExpansionPanelDescriptionDirective` show tooltip of the provided text content.
810
- `IgxDateRangePicker`
911
- Added `showWeekNumbers` input that toggles whether or not the number of a week will be visible next to it
1012

projects/igniteui-angular/src/lib/expansion-panel/expansion-panel.common.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventEmitter, InjectionToken } from '@angular/core';
1+
import { Directive, ElementRef, EventEmitter, InjectionToken } from '@angular/core';
22
import { AnimationReferenceMetadata } from '@angular/animations';
33
import { CancelableEventArgs, IBaseEventArgs } from '../core/utils';
44

@@ -26,3 +26,41 @@ export interface IExpansionPanelEventArgs extends IBaseEventArgs {
2626
}
2727

2828
export interface IExpansionPanelCancelableEventArgs extends IExpansionPanelEventArgs, CancelableEventArgs {}
29+
30+
@Directive()
31+
export abstract class HeaderContentBaseDirective {
32+
33+
constructor(protected element: ElementRef) { }
34+
35+
/**
36+
* Returns the `textContent` of an element
37+
*
38+
* ```html
39+
* <igx-expansion-panel-title>
40+
* Tooltip content
41+
* </igx-expansion-panel-title>
42+
* ```
43+
*
44+
* or the `title` content
45+
*
46+
* ```html
47+
* <igx-expansion-panel-title [title]="'Tooltip content'">
48+
* </igx-expansion-panel-title>
49+
* ```
50+
*
51+
* If both are provided, returns the `title` content.
52+
*
53+
* @param element
54+
* @returns tooltip content for an element
55+
*/
56+
public getTooltipContent = (element: ElementRef): string => {
57+
if (element.nativeElement.title) {
58+
return element.nativeElement.title;
59+
}
60+
if (element.nativeElement.textContent) {
61+
return element.nativeElement.textContent.trim();
62+
}
63+
64+
return null;
65+
};
66+
}

projects/igniteui-angular/src/lib/expansion-panel/expansion-panel.directives.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
import { Directive, HostBinding } from '@angular/core';
2+
import { HeaderContentBaseDirective } from './expansion-panel.common';
23

3-
/**
4-
* @hidden @internal
5-
*/
4+
/** @hidden @internal */
65
@Directive({
76
// eslint-disable-next-line @angular-eslint/directive-selector
87
selector: 'igx-expansion-panel-title'
98
})
10-
export class IgxExpansionPanelTitleDirective {
9+
export class IgxExpansionPanelTitleDirective extends HeaderContentBaseDirective {
1110
@HostBinding('class.igx-expansion-panel__header-title')
1211
public cssClass = `igx-expansion-panel__header-title`;
12+
13+
@HostBinding('attr.title')
14+
private get title(): string {
15+
return this.getTooltipContent(this.element);
16+
}
1317
}
1418

15-
/**
16-
* @hidden @internal
17-
*/
19+
/** @hidden @internal */
1820
@Directive({
1921
// eslint-disable-next-line @angular-eslint/directive-selector
2022
selector: 'igx-expansion-panel-description'
2123
})
22-
export class IgxExpansionPanelDescriptionDirective {
24+
export class IgxExpansionPanelDescriptionDirective extends HeaderContentBaseDirective {
2325
@HostBinding('class.igx-expansion-panel__header-description')
2426
public cssClass = `igx-expansion-panel__header-description`;
27+
28+
@HostBinding('attr.title')
29+
private get title(): string {
30+
return this.getTooltipContent(this.element);
31+
}
2532
}
2633

27-
/**
28-
* @hidden @internal
29-
*/
34+
/** @hidden @internal */
3035
@Directive({
3136
// eslint-disable-next-line @angular-eslint/directive-selector
3237
selector: 'igx-expansion-panel-icon'

projects/igniteui-angular/src/lib/expansion-panel/expansion-panel.spec.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { By } from '@angular/platform-browser';
1616

1717
const CSS_CLASS_EXPANSION_PANEL = 'igx-expansion-panel';
1818
const CSS_CLASS_PANEL_HEADER = 'igx-expansion-panel__header';
19+
const CSS_CLASS_PANEL_HEADER_TITLE = 'igx-expansion-panel__header-title';
20+
const CSS_CLASS_PANEL_HEADER_DESCRIPTION = 'igx-expansion-panel__header-description';
1921
const CSS_CLASS_PANEL_TITLE_WRAPPER = 'igx-expansion-panel__title-wrapper';
2022
const CSS_CLASS_PANEL_BODY = 'igx-expansion-panel-body';
2123
const CSS_CLASS_HEADER_EXPANDED = 'igx-expansion-panel__header--expanded';
@@ -39,7 +41,8 @@ describe('igxExpansionPanel', () => {
3941
IgxExpansionPanelGridComponent,
4042
IgxExpansionPanelListComponent,
4143
IgxExpansionPanelSampleComponent,
42-
IgxExpansionPanelImageComponent
44+
IgxExpansionPanelImageComponent,
45+
IgxExpansionPanelTooltipComponent
4346
],
4447
imports: [
4548
IgxExpansionPanelModule,
@@ -1258,6 +1261,35 @@ describe('igxExpansionPanel', () => {
12581261
expect(image.tagName).toEqual('IMG');
12591262
expect (textWrapper.textContent.trim()).toEqual(fixture.componentInstance.text);
12601263
}));
1264+
it('Should display tooltip with the title and description text content', () => {
1265+
const fixture: ComponentFixture<IgxExpansionPanelTooltipComponent> = TestBed.createComponent(IgxExpansionPanelTooltipComponent);
1266+
fixture.detectChanges();
1267+
1268+
const headerTitle = fixture.nativeElement.querySelector('.' + CSS_CLASS_PANEL_HEADER_TITLE);
1269+
const headerDescription = fixture.nativeElement.querySelector('.' + CSS_CLASS_PANEL_HEADER_DESCRIPTION);
1270+
1271+
const headerTitleTooltip = headerTitle.getAttribute('title');
1272+
const headerDescriptionTooltip = headerDescription.getAttribute('title');
1273+
1274+
expect(headerTitleTooltip).toEqual(headerTitle.textContent.trim());
1275+
expect(headerDescriptionTooltip).toEqual(headerDescription.textContent.trim());
1276+
});
1277+
it('Should display tooltip with the attr.title text content', () => {
1278+
const fixture: ComponentFixture<IgxExpansionPanelTooltipComponent> = TestBed.createComponent(IgxExpansionPanelTooltipComponent);
1279+
1280+
fixture.componentInstance.titleTooltip = 'Custom Title Tooltip';
1281+
fixture.componentInstance.descriptionTooltip = 'Custom Description Tooltip';
1282+
fixture.detectChanges();
1283+
1284+
const headerTitle = fixture.nativeElement.querySelector('.' + CSS_CLASS_PANEL_HEADER_TITLE);
1285+
const headerDescription = fixture.nativeElement.querySelector('.' + CSS_CLASS_PANEL_HEADER_DESCRIPTION);
1286+
1287+
const headerTitleTooltip = headerTitle.getAttribute('title');
1288+
const headerDescriptionTooltip = headerDescription.getAttribute('title');
1289+
1290+
expect(headerTitleTooltip).toEqual('Custom Title Tooltip');
1291+
expect(headerDescriptionTooltip).toEqual('Custom Description Tooltip');
1292+
});
12611293
});
12621294
});
12631295

@@ -1397,3 +1429,25 @@ export class IgxExpansionPanelImageComponent {
13971429
public text = 'A frog is any member of a diverse and largely carnivorous group of short-bodied, tailless amphibians composing the order Anura. The oldest fossil \"proto-frog\" appeared in the early Triassic of Madagascar, but molecular clock dating suggests their origins may extend further back to the Permian, 265 million years ago. Frogs are widely distributed, ranging from the tropics to subarctic regions, but the greatest concentration of species diversity is in tropical rainforests. There are approximately 4,800 recorded species, accounting for over 85% of extant amphibian species. They are also one of the five most diverse vertebrate orders. The body plan of an adult frog is generally characterized by a stout body, protruding eyes, cleft tongue, limbs folded underneath, and the absence of a tail. Besides living in fresh water and on dry land, the adults of some species are adapted for living underground or in trees. The skins of frogs are glandular, with secretions ranging from distasteful to toxic. Warty species of frog tend to be called toads but the distinction between frogs and toads is based on informal naming conventions concentrating on the warts rather than taxonomy or evolutionary history.';
13981430
}
13991431

1432+
@Component({
1433+
template: `
1434+
<igx-expansion-panel>
1435+
<igx-expansion-panel-header>
1436+
<igx-expansion-panel-title [title]="titleTooltip">
1437+
Example Title
1438+
</igx-expansion-panel-title>
1439+
<igx-expansion-panel-description [title]="descriptionTooltip">
1440+
Example Description
1441+
</igx-expansion-panel-description>
1442+
</igx-expansion-panel-header>
1443+
<igx-expansion-panel-body>
1444+
Example Body
1445+
</igx-expansion-panel-body>
1446+
</igx-expansion-panel>
1447+
`
1448+
})
1449+
export class IgxExpansionPanelTooltipComponent {
1450+
public titleTooltip = '';
1451+
public descriptionTooltip = '';
1452+
}
1453+

0 commit comments

Comments
 (0)