Skip to content

Commit

Permalink
Allow custom localize function for datatable (#21270)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Jul 4, 2024
1 parent 8b9fa9b commit df7b5b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
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

0 comments on commit df7b5b0

Please sign in to comment.