Localize show_password in auth form (#19300)

* Localize show_password in auth form

* changes from review
This commit is contained in:
karwosts 2024-01-16 13:39:35 -08:00 committed by GitHub
parent 490ed86e86
commit a06c9d0cc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 9 deletions

View File

@ -222,6 +222,7 @@ export class HaAuthFlow extends LitElement {
</h1> </h1>
${this._computeStepDescription(step)} ${this._computeStepDescription(step)}
<ha-auth-form <ha-auth-form
.localize=${this.localize}
.data=${this._stepData!} .data=${this._stepData!}
.schema=${autocompleteLoginFields(step.data_schema)} .schema=${autocompleteLoginFields(step.data_schema)}
.error=${step.errors} .error=${step.errors}

View File

@ -1,11 +1,23 @@
/* eslint-disable lit/prefer-static-styles */ /* eslint-disable lit/prefer-static-styles */
import { html } from "lit"; import { html } from "lit";
import { customElement } from "lit/decorators"; import { customElement, property } from "lit/decorators";
import { HaForm } from "../components/ha-form/ha-form"; import { HaForm } from "../components/ha-form/ha-form";
import "./ha-auth-form-string"; import "./ha-auth-form-string";
import { LocalizeFunc } from "../common/translations/localize";
const localizeBaseKey = "ui.panel.page-authorize.form";
@customElement("ha-auth-form") @customElement("ha-auth-form")
export class HaAuthForm extends HaForm { export class HaAuthForm extends HaForm {
@property({ attribute: false }) public localize?: LocalizeFunc;
protected getFormProperties(): Record<string, any> {
return {
localize: this.localize,
localizeBaseKey,
};
}
protected fieldElementName(type: string): string { protected fieldElementName(type: string): string {
if (type === "string") { if (type === "string") {
return `ha-auth-form-${type}`; return `ha-auth-form-${type}`;

View File

@ -19,13 +19,15 @@ import type {
HaFormStringData, HaFormStringData,
HaFormStringSchema, HaFormStringSchema,
} from "./types"; } from "./types";
import { HomeAssistant } from "../../types"; import { LocalizeFunc, LocalizeKeys } from "../../common/translations/localize";
const MASKED_FIELDS = ["password", "secret", "token"]; const MASKED_FIELDS = ["password", "secret", "token"];
@customElement("ha-form-string") @customElement("ha-form-string")
export class HaFormString extends LitElement implements HaFormElement { export class HaFormString extends LitElement implements HaFormElement {
@property({ attribute: false }) public hass?: HomeAssistant; @property({ attribute: false }) public localize?: LocalizeFunc;
@property() public localizeBaseKey = "ui.components.selectors.text";
@property() public schema!: HaFormStringSchema; @property() public schema!: HaFormStringSchema;
@ -81,11 +83,11 @@ export class HaFormString extends LitElement implements HaFormElement {
return html` return html`
<ha-icon-button <ha-icon-button
toggles toggles
.label=${this.hass?.localize( .label=${this.localize?.(
this.unmaskedPassword `${this.localizeBaseKey}.${
? "ui.components.selectors.text.hide_password" this.unmaskedPassword ? "hide_password" : "show_password"
: "ui.components.selectors.text.show_password" }` as LocalizeKeys
) || (this.unmaskedPassword ? "Hide password" : "Show password")} )}
@click=${this.toggleUnmaskedPassword} @click=${this.toggleUnmaskedPassword}
.path=${this.unmaskedPassword ? mdiEyeOff : mdiEye} .path=${this.unmaskedPassword ? mdiEyeOff : mdiEye}
></ha-icon-button> ></ha-icon-button>

View File

@ -39,7 +39,7 @@ const getWarning = (obj, item) => (obj && item.name ? obj[item.name] : null);
@customElement("ha-form") @customElement("ha-form")
export class HaForm extends LitElement implements HaFormElement { export class HaForm extends LitElement implements HaFormElement {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass?: HomeAssistant;
@property({ attribute: false }) public data!: HaFormDataContainer; @property({ attribute: false }) public data!: HaFormDataContainer;
@ -64,6 +64,10 @@ export class HaForm extends LitElement implements HaFormElement {
@property() public localizeValue?: (key: string) => string; @property() public localizeValue?: (key: string) => string;
protected getFormProperties(): Record<string, any> {
return {};
}
public async focus() { public async focus() {
await this.updateComplete; await this.updateComplete;
const root = this.renderRoot.querySelector(".root"); const root = this.renderRoot.querySelector(".root");
@ -143,9 +147,11 @@ export class HaForm extends LitElement implements HaFormElement {
helper: this._computeHelper(item), helper: this._computeHelper(item),
disabled: this.disabled || item.disabled || false, disabled: this.disabled || item.disabled || false,
hass: this.hass, hass: this.hass,
localize: this.hass?.localize,
computeLabel: this.computeLabel, computeLabel: this.computeLabel,
computeHelper: this.computeHelper, computeHelper: this.computeHelper,
context: this._generateContext(item), context: this._generateContext(item),
...this.getFormProperties(),
})} })}
`; `;
})} })}