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

Allow custom localize function for datatable #21270

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
11 changes: 9 additions & 2 deletions src/components/data-table/ha-data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type { HaCheckbox } from "../ha-checkbox";
import "../ha-svg-icon";
import "../search-input";
import { filterData, sortData } from "./sort-filter";
import { LocalizeFunc } from "../../common/translations/localize";

export interface RowClickedEvent {
id: string;
Expand Down Expand Up @@ -110,6 +111,8 @@ const UNDEFINED_GROUP_KEY = "zzzzz_undefined";
export class HaDataTable extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public localizeFunc?: LocalizeFunc;

@property({ type: Boolean }) public narrow = false;

@property({ type: Object }) public columns: DataTableColumnContainer = {};
Expand Down Expand Up @@ -317,6 +320,8 @@ export class HaDataTable extends LitElement {
);

protected render() {
const localize = this.localizeFunc || this.hass.localize;

const columns = this._sortedColumns(this.columns, this.columnOrder);

const renderRow = (row: DataTableRowData, index: number) =>
Expand Down Expand Up @@ -436,7 +441,7 @@ export class HaDataTable extends LitElement {
<div class="mdc-data-table__row" role="row">
<div class="mdc-data-table__cell grows center" role="cell">
${this.noDataText ||
this.hass.localize("ui.components.data-table.no-data")}
localize("ui.components.data-table.no-data")}
</div>
</div>
</div>
Expand Down Expand Up @@ -619,6 +624,8 @@ export class HaDataTable extends LitElement {
return;
}

const localize = this.localizeFunc || this.hass.localize;

if (this.appendRow || this.hasFab || this.groupColumn) {
let items = [...data];

Expand Down Expand Up @@ -672,7 +679,7 @@ export class HaDataTable extends LitElement {
>
</ha-icon-button>
${groupName === UNDEFINED_GROUP_KEY
? this.hass.localize("ui.components.data-table.ungrouped")
? localize("ui.components.data-table.ungrouped")
: groupName || ""}
</div>`,
});
Expand Down
8 changes: 4 additions & 4 deletions src/layouts/hass-tabs-subpage-data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ export class HaTabsSubpageDataTable extends LitElement {
: ""}
<ha-data-table
.hass=${this.hass}
.localize=${localize}
.narrow=${this.narrow}
.columns=${this.columns}
.data=${this.data}
Expand Down Expand Up @@ -575,10 +576,9 @@ export class HaTabsSubpageDataTable extends LitElement {
</div>
<div slot="primaryAction">
<ha-button @click=${this._toggleFilters}>
${this.hass.localize(
"ui.components.subpage-data-table.show_results",
{ number: this.data.length }
)}
${localize("ui.components.subpage-data-table.show_results", {
number: this.data.length,
})}
</ha-button>
</div>
</ha-dialog>`
Expand Down
Loading