Skip to content

Commit 28ee12d

Browse files
committed
feat(expansion-panel): show tooltip for title and description #12763
1 parent a822aee commit 28ee12d

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

projects/igniteui-angular/src/lib/core/utils.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AnimationReferenceMetadata } from '@angular/animations';
22
import { CurrencyPipe, formatDate as _formatDate, isPlatformBrowser } from '@angular/common';
3-
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
3+
import { ElementRef, Inject, Injectable, PLATFORM_ID } from '@angular/core';
44
import { mergeWith } from 'lodash-es';
55
import { Observable } from 'rxjs';
66
import {
@@ -521,6 +521,36 @@ export const reverseMapper = (path: string, value: any) => {
521521
return obj;
522522
};
523523

524+
/**
525+
* Returns the `textContent` of an element
526+
*
527+
* ```html
528+
* <igx-expansion-panel-title>
529+
* Tooltip content
530+
* </igx-expansion-panel-title>
531+
* ```
532+
*
533+
* or the `title` content
534+
*
535+
* ```html
536+
* <igx-expansion-panel-title [title]="'Tooltip content'">
537+
* </igx-expansion-panel-title>
538+
* ```
539+
*
540+
* If both are provided, returns the `title` content.
541+
*
542+
* @param element
543+
* @returns tooltip content for an element
544+
* @hidden
545+
* @internal
546+
*/
547+
export const getTooltipContent = (element: ElementRef): any => {
548+
return element.nativeElement.title
549+
? element.nativeElement.title
550+
: element.nativeElement.textContent
551+
? element.nativeElement.textContent.trim() : null;
552+
};
553+
524554
export const yieldingLoop = (count: number, chunkSize: number, callback: (index: number) => void, done: () => void) => {
525555
let i = 0;
526556
const chunk = () => {

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

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Directive, HostBinding } from '@angular/core';
1+
import { Directive, ElementRef, HostBinding, OnInit } from '@angular/core';
2+
import { getTooltipContent } from '../core/utils';
23

34
/**
45
* @hidden @internal
@@ -7,9 +8,18 @@ import { Directive, HostBinding } from '@angular/core';
78
// eslint-disable-next-line @angular-eslint/directive-selector
89
selector: 'igx-expansion-panel-title'
910
})
10-
export class IgxExpansionPanelTitleDirective {
11+
export class IgxExpansionPanelTitleDirective implements OnInit {
1112
@HostBinding('class.igx-expansion-panel__header-title')
1213
public cssClass = `igx-expansion-panel__header-title`;
14+
15+
@HostBinding('attr.title')
16+
public title: string;
17+
18+
constructor(private element: ElementRef) {}
19+
20+
public ngOnInit() {
21+
this.title = getTooltipContent(this.element);
22+
}
1323
}
1424

1525
/**
@@ -19,9 +29,18 @@ export class IgxExpansionPanelTitleDirective {
1929
// eslint-disable-next-line @angular-eslint/directive-selector
2030
selector: 'igx-expansion-panel-description'
2131
})
22-
export class IgxExpansionPanelDescriptionDirective {
32+
export class IgxExpansionPanelDescriptionDirective implements OnInit {
2333
@HostBinding('class.igx-expansion-panel__header-description')
2434
public cssClass = `igx-expansion-panel__header-description`;
35+
36+
@HostBinding('attr.title')
37+
public title: string;
38+
39+
constructor(private element: ElementRef) {}
40+
41+
public ngOnInit() {
42+
this.title = getTooltipContent(this.element);
43+
}
2544
}
2645

2746
/**

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

+55-1
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)