Convert HaFormSchemas to use selectors (#11589)

This commit is contained in:
Bram Kragten 2022-02-07 17:06:04 +01:00 committed by GitHub
parent ca8d31c6bb
commit e72a4e4a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 23 deletions

View File

@ -50,7 +50,11 @@ const SCHEMAS: {
action: { name: "Action", selector: { action: {} } }, action: { name: "Action", selector: { action: {} } },
text: { text: {
name: "Text", name: "Text",
selector: { text: { multiline: false } }, selector: { text: {} },
},
password: {
name: "Password",
selector: { text: { type: "password" } },
}, },
text_multiline: { text_multiline: {
name: "Text multiline", name: "Text multiline",

View File

@ -19,22 +19,21 @@ import { haStyle, haStyleDialog } from "../../../../src/resources/styles";
import type { HomeAssistant } from "../../../../src/types"; import type { HomeAssistant } from "../../../../src/types";
import { RegistriesDialogParams } from "./show-dialog-registries"; import { RegistriesDialogParams } from "./show-dialog-registries";
const SCHEMA = [ const SCHEMA: HaFormSchema[] = [
{ {
type: "string",
name: "registry", name: "registry",
required: true, required: true,
selector: { text: {} },
}, },
{ {
type: "string",
name: "username", name: "username",
required: true, required: true,
selector: { text: {} },
}, },
{ {
type: "string",
name: "password", name: "password",
required: true, required: true,
format: "password", selector: { text: { type: "password" } },
}, },
]; ];

View File

@ -1,10 +1,12 @@
import "@material/mwc-textarea/mwc-textarea";
import "@material/mwc-textfield/mwc-textfield";
import { mdiEye, mdiEyeOff } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement } from "lit"; import { css, CSSResultGroup, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import { StringSelector } from "../../data/selector"; import { StringSelector } from "../../data/selector";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "@material/mwc-textfield/mwc-textfield"; import "../ha-icon-button";
import "@material/mwc-textarea/mwc-textarea";
@customElement("ha-selector-text") @customElement("ha-selector-text")
export class HaTextSelector extends LitElement { export class HaTextSelector extends LitElement {
@ -20,6 +22,8 @@ export class HaTextSelector extends LitElement {
@property({ type: Boolean }) public disabled = false; @property({ type: Boolean }) public disabled = false;
@state() private _unmaskedPassword = false;
protected render() { protected render() {
if (this.selector.text?.multiline) { if (this.selector.text?.multiline) {
return html`<mwc-textarea return html`<mwc-textarea
@ -35,13 +39,30 @@ export class HaTextSelector extends LitElement {
></mwc-textarea>`; ></mwc-textarea>`;
} }
return html`<mwc-textfield return html`<mwc-textfield
.value=${this.value || ""} .value=${this.value || ""}
.placeholder=${this.placeholder || ""} .placeholder=${this.placeholder || ""}
.disabled=${this.disabled} .disabled=${this.disabled}
@input=${this._handleChange} .type=${this._unmaskedPassword ? "text" : this.selector.text?.type}
.label=${this.label || ""} @input=${this._handleChange}
required .label=${this.label || ""}
></mwc-textfield>`; .suffix=${this.selector.text?.type === "password"
? // reserve some space for the icon.
html`<div style="width: 24px"></div>`
: this.selector.text?.suffix}
required
></mwc-textfield>
${this.selector.text?.type === "password"
? html`<ha-icon-button
toggles
.label=${`${this._unmaskedPassword ? "Hide" : "Show"} password`}
@click=${this._toggleUnmaskedPassword}
.path=${this._unmaskedPassword ? mdiEyeOff : mdiEye}
></ha-icon-button>`
: ""}`;
}
private _toggleUnmaskedPassword(): void {
this._unmaskedPassword = !this._unmaskedPassword;
} }
private _handleChange(ev) { private _handleChange(ev) {
@ -54,10 +75,22 @@ export class HaTextSelector extends LitElement {
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
return css` return css`
:host {
display: block;
position: relative;
}
mwc-textfield, mwc-textfield,
mwc-textarea { mwc-textarea {
width: 100%; width: 100%;
} }
ha-icon-button {
position: absolute;
top: 16px;
right: 16px;
--mdc-icon-button-size: 24px;
--mdc-icon-size: 20px;
color: var(--secondary-text-color);
}
`; `;
} }
} }

View File

@ -95,7 +95,22 @@ export interface ActionSelector {
export interface StringSelector { export interface StringSelector {
text: { text: {
multiline: boolean; multiline?: boolean;
type?:
| "number"
| "text"
| "search"
| "tel"
| "url"
| "email"
| "password"
| "date"
| "month"
| "week"
| "time"
| "datetime-local"
| "color";
suffix?: string;
}; };
} }

View File

@ -18,10 +18,18 @@ import { onboardUserStep } from "../data/onboarding";
import { PolymerChangedEvent } from "../polymer-types"; import { PolymerChangedEvent } from "../polymer-types";
const CREATE_USER_SCHEMA: HaFormSchema[] = [ const CREATE_USER_SCHEMA: HaFormSchema[] = [
{ type: "string", name: "name", required: true }, { name: "name", required: true, selector: { text: {} } },
{ type: "string", name: "username", required: true }, { name: "username", required: true, selector: { text: {} } },
{ type: "string", name: "password", required: true }, {
{ type: "string", name: "password_confirm", required: true }, name: "password",
required: true,
selector: { text: { type: "password" } },
},
{
name: "password_confirm",
required: true,
selector: { text: { type: "password" } },
},
]; ];
@customElement("onboarding-create-user") @customElement("onboarding-create-user")

View File

@ -106,7 +106,7 @@ class ZHAConfigDashboard extends LitElement {
</ha-card> </ha-card>
${this._configuration ${this._configuration
? Object.entries(this._configuration.schemas).map( ? Object.entries(this._configuration.schemas).map(
([section, schema]) => html` <ha-card ([section, schema]) => html`<ha-card
header=${this.hass.localize( header=${this.hass.localize(
`component.zha.config_panel.${section}.title` `component.zha.config_panel.${section}.title`
)} )}

View File

@ -101,7 +101,7 @@ class HaMfaModuleSetupFlow extends LitElement {
)} )}
</p>` </p>`
: this._step.type === "form" : this._step.type === "form"
? html` <ha-markdown ? html`<ha-markdown
allowsvg allowsvg
breaks breaks
.content=${this.hass.localize( .content=${this.hass.localize(