Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(start-ui): allow usage of tooltip #2146

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
</th>
<!-- the column template defined by the user will be displayed here -->
<!-- and it will receive the right context containing the displayedValue and the row data-->
<td mat-cell *matCellDef="let rowItem" [ngClass]="getCellClassNames(rowItem)" (click)="onCellClick($event, rowItem)">
<td mat-cell *matCellDef="let rowItem" [ngClass]="getCellClassNames(rowItem)" (click)="onCellClick($event, rowItem)"
[matTooltip]="getCellTooltip(rowItem)" [matTooltipPosition]="cellTooltipPosition" [matTooltipClass]="getCellTooltipClassNames(rowItem)">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the tooltip is an empty string ""? I think we should disable the tooltip in such cases with the matTooltipDisabled property otherwise I fear that Angular Material would still be doing some logic to display the tooltip which will be overkill if that would be an empty string in the end.

Another solution would be to have 2 <td mat-cell> with an *ngIf: one will contain the [matTooltip] if there is a relevant tooltip to show (not empty). And the other one won't contain the [matTooltip], basically the same we had before, when there is no tooltip to show.

What do you think?

<ng-container
*ngTemplateOutlet="
columnTemplate;
Expand Down
70 changes: 70 additions & 0 deletions packages/stark-ui/src/modules/table/components/column.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,40 @@ export class StarkTableColumnComponent extends AbstractStarkUiComponent implemen
@Input()
public stickyEnd = false;

/**
* A function to generate tooltip for cells based on the value, its row and the name of the column.
* Or a static string with the tooltip.
*/
@Input()
public cellTooltip?: ((value: any, row?: object, columnName?: string) => string) | string;

/**
* Position where the tooltip should be displayed.
* Default: "below"
* Possible positions: above, below, left, right, before, after
*/
@Input()
public get cellTooltipPosition(): string {
return this._cellTooltipPosition;
}

public set cellTooltipPosition(tooltip: string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark about using specific types instead of just string. You could re-use the ones from the ColumnProperties 😉

this._cellTooltipPosition = tooltip || "below";
}

/**
* @ignore
* @internal
*/
private _cellTooltipPosition = "below"

/**
* A function to generate classNames for the tooltip of cells based on the value, its row and the name of the column.
* Or a static string with the classNames.
*/
@Input()
public cellTooltipClassName?: ((value: any, row?: object, columnName?: string) => string) | string;

/**
* Output that will emit a StarkColumnCellClickedOutput whenever a cell in the column is clicked
*/
Expand Down Expand Up @@ -420,4 +454,40 @@ export class StarkTableColumnComponent extends AbstractStarkUiComponent implemen

return classes.join(" ");
}

/**
* Gets the tooltip for a specific cell, if the cellTooltip Input or the cellTooltipFn function has been given as an Input.
* @param row - The data object of the row the cell is in.
* @returns The tooltip for the cell.
*/
public getCellTooltip(row: object): string {
if (!this.cellTooltip) {
return "";
}

if (typeof this.cellTooltip === "string") {
return this.cellTooltip;
}

const value: any = this.getRawValue(row);
return this.cellTooltip(value, row, this.name);
}

/**
* Gets the classes for the tooltip of a specific cell, if the cellTooltipClassName Input or the cellTooltipClassNameFn function has been given as an Input.
* @param row - The data object of the row the cell is in.
* @returns The classes for the tooltip of the cell.
*/
public getCellTooltipClassNames(row: object): string {
if (!this.cellTooltipClassName) {
return "";
}

if (typeof this.cellTooltipClassName === "string") {
return this.cellTooltipClassName;
}

const value: any = this.getRawValue(row);
return this.cellTooltipClassName(value, row, this.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
[cellFormatter]="col.cellFormatter"
[cellClassName]="col.cellClassName"
[headerClassName]="col.headerClassName"
[cellTooltip]="col.cellTooltip"
[cellTooltipPosition]="col.cellTooltipPosition"
[cellTooltipClassName]="col.cellTooltipClassName"
>
<ng-template let-context>
<ng-container
Expand Down
Loading