Skip to content

Commit 95bdff6

Browse files
feat(suggest): add custom tooltip
1 parent eda4d8a commit 95bdff6

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

projects/angular/components/ui-suggest/src/models/suggestValue.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ export interface ISuggestValue extends VirtualScrollItem {
5353
svgIcon?: string;
5454
matIcon?: string;
5555
};
56+
57+
/**
58+
* Tooltip associated to the entry.
59+
*/
60+
tooltip?: string;
5661
}

projects/angular/components/ui-suggest/src/ui-suggest.component.html

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,31 @@
100100
(keydown.esc)="preventDefault($event)"
101101
(keydown.tab)="close(false)"
102102
(keydown.shift.tab)="close()">
103-
<mat-chip-row *ngFor="let option of value; let i = index"
104-
[removable]="canRemoveChip(option)"
105-
[matTooltip]="intl.translateLabel(option.text)"
106-
(removed)="deselectItem(option)"
107-
class="chip-selectable">
108-
<mat-icon *ngIf="!!option?.icon?.svgIcon"
109-
[svgIcon]="option!.icon!.svgIcon!"
110-
class="chip-selectable-icon">
111-
</mat-icon>
112-
<mat-icon *ngIf="!!option?.icon?.matIcon"
113-
class="chip-selectable-icon">
114-
{{option?.icon?.matIcon}}
115-
</mat-icon>
116-
<span>
117-
{{ intl.translateLabel(option.text) }}
118-
</span>
119-
<button [attr.aria-hidden]="true"
120-
matChipRemove
121-
tabindex="-1">
122-
<mat-icon>close</mat-icon>
123-
</button>
124-
</mat-chip-row>
103+
<ng-container *ngFor="let option of value; let i = index">
104+
<mat-chip-row *ngLet="canRemoveChip(option) as canRemoveChip"
105+
[removable]="canRemoveChip"
106+
[matTooltip]="intl.translateLabel(option.tooltip ?? option.text)"
107+
(removed)="deselectItem(option)"
108+
class="chip-selectable">
109+
<mat-icon *ngIf="!!option?.icon?.svgIcon"
110+
[svgIcon]="option!.icon!.svgIcon!"
111+
class="chip-selectable-icon">
112+
</mat-icon>
113+
<mat-icon *ngIf="!!option?.icon?.matIcon"
114+
class="chip-selectable-icon">
115+
{{option?.icon?.matIcon}}
116+
</mat-icon>
117+
<span>
118+
{{ intl.translateLabel(option.text) }}
119+
</span>
120+
<button *ngIf="canRemoveChip"
121+
[attr.aria-hidden]="true"
122+
matChipRemove
123+
tabindex="-1">
124+
<mat-icon>close</mat-icon>
125+
</button>
126+
</mat-chip-row>
127+
</ng-container>
125128

126129
<input #searchInput
127130
[uiAutofocus]="isOpen"
@@ -408,7 +411,7 @@
408411
</ng-container>
409412
</ng-container>
410413
<ng-template #defaultItem>
411-
<div [matTooltip]="disableTooltip ? '' : intl.translateLabel(item.text)"
414+
<div [matTooltip]="disableTooltip ? '' : intl.translateLabel(item.tooltip ?? item.text)"
412415
[attr.data-item-id]="item.id"
413416
matTooltipPosition="right"
414417
class="ui-suggest-item">

projects/angular/components/ui-suggest/src/ui-suggest.component.spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ const sharedSpecifications = (
14711471
discardPeriodicTasks();
14721472
}));
14731473

1474-
it('should NOT be able to remove chip if readonly', fakeAsync(() => {
1474+
it('should NOT show remove button if chip is readonly', fakeAsync(() => {
14751475
const items = component.items!.slice(0, 3);
14761476
component.value = items;
14771477
component.readonly = true;
@@ -1481,19 +1481,7 @@ const sharedSpecifications = (
14811481
const initialChips = fixture.debugElement.queryAll(By.css('.mat-mdc-chip'));
14821482
expect(initialChips.length).toBe(3);
14831483

1484-
const chipRemoveButton = getNativeElement<HTMLButtonElement>(initialChips[1].query(By.css('button')));
1485-
chipRemoveButton.click();
1486-
1487-
fixture.detectChanges();
1488-
tick(5000);
1489-
1490-
const updatedChips = fixture.debugElement
1491-
.queryAll(By.css(SELECTORS.chipLabel))
1492-
.map(el => getNativeElement<HTMLSpanElement>(el));
1493-
1494-
expect(updatedChips.length).toEqual(3);
1495-
expect(updatedChips.map(chip => chip.innerText)).toEqual(items.map(item => item.text));
1496-
1484+
expect(initialChips[1].query(By.css('button'))).toBeFalsy();
14971485
discardPeriodicTasks();
14981486
}));
14991487
});

projects/angular/components/ui-suggest/src/ui-suggest.component.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
331331
!this.isOpen &&
332332
this._hasValue
333333
) {
334-
return this._getValueSummary();
334+
return this._getValueSummary(true);
335335
}
336336

337337
return null;
@@ -1719,11 +1719,12 @@ export class UiSuggestComponent extends UiSuggestMatFormFieldDirective
17191719
});
17201720
}
17211721

1722-
private _getValueSummary() {
1723-
return (this.displayValueFactory ?? this._defaultDisplayValueFactory)(this.value);
1722+
private _getValueSummary(fromTooltip = false) {
1723+
return (this.displayValueFactory ?? this._defaultDisplayValueFactory)(this.value, fromTooltip);
17241724
}
17251725

1726-
private _defaultDisplayValueFactory = (value?: ISuggestValue[]) => (value ?? []).map(v => this.intl.translateLabel(v.text)).join(', ');
1726+
private _defaultDisplayValueFactory = (value?: ISuggestValue[], fromTooltip = false) =>
1727+
(value ?? []).map(v => this.intl.translateLabel((fromTooltip && v.tooltip) || v.text)).join(', ');
17271728

17281729
private _cantNavigate(increment: number) {
17291730
return (!this.items.length &&

0 commit comments

Comments
 (0)