mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 11:46:42 +00:00
Use dynamicElement directive in ha-form (#4317)
* Use dynamicContentDirective * Turn around * Remove attributes * Rename to dynamicElement
This commit is contained in:
parent
2557414b11
commit
e6ac0258e3
@ -1,7 +1,7 @@
|
|||||||
import { directive, Part, NodePart } from "lit-html";
|
import { directive, Part, NodePart } from "lit-html";
|
||||||
|
|
||||||
export const dynamicContentDirective = directive(
|
export const dynamicElement = directive(
|
||||||
(tag: string, properties: { [key: string]: any }) => (part: Part): void => {
|
(tag: string, properties?: { [key: string]: any }) => (part: Part): void => {
|
||||||
if (!(part instanceof NodePart)) {
|
if (!(part instanceof NodePart)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"dynamicContentDirective can only be used in content bindings"
|
"dynamicContentDirective can only be used in content bindings"
|
||||||
@ -14,16 +14,20 @@ export const dynamicContentDirective = directive(
|
|||||||
element !== undefined &&
|
element !== undefined &&
|
||||||
tag.toUpperCase() === (element as HTMLElement).tagName
|
tag.toUpperCase() === (element as HTMLElement).tagName
|
||||||
) {
|
) {
|
||||||
Object.entries(properties).forEach(([key, value]) => {
|
if (properties) {
|
||||||
element![key] = value;
|
Object.entries(properties).forEach(([key, value]) => {
|
||||||
});
|
element![key] = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
element = document.createElement(tag);
|
element = document.createElement(tag);
|
||||||
Object.entries(properties).forEach(([key, value]) => {
|
if (properties) {
|
||||||
element![key] = value;
|
Object.entries(properties).forEach(([key, value]) => {
|
||||||
});
|
element![key] = value;
|
||||||
|
});
|
||||||
|
}
|
||||||
part.setValue(element);
|
part.setValue(element);
|
||||||
}
|
}
|
||||||
);
|
);
|
@ -3,10 +3,8 @@ import {
|
|||||||
LitElement,
|
LitElement,
|
||||||
html,
|
html,
|
||||||
property,
|
property,
|
||||||
query,
|
|
||||||
CSSResult,
|
CSSResult,
|
||||||
css,
|
css,
|
||||||
PropertyValues,
|
|
||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
|
|
||||||
import "./ha-form-string";
|
import "./ha-form-string";
|
||||||
@ -16,6 +14,7 @@ import "./ha-form-boolean";
|
|||||||
import "./ha-form-select";
|
import "./ha-form-select";
|
||||||
import "./ha-form-positive_time_period_dict";
|
import "./ha-form-positive_time_period_dict";
|
||||||
import { fireEvent } from "../../common/dom/fire_event";
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
|
import { dynamicElement } from "../../common/dom/dynamic-element-directive";
|
||||||
|
|
||||||
export type HaFormSchema =
|
export type HaFormSchema =
|
||||||
| HaFormStringSchema
|
| HaFormStringSchema
|
||||||
@ -100,20 +99,14 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
@property() public computeError?: (schema: HaFormSchema, error) => string;
|
@property() public computeError?: (schema: HaFormSchema, error) => string;
|
||||||
@property() public computeLabel?: (schema: HaFormSchema) => string;
|
@property() public computeLabel?: (schema: HaFormSchema) => string;
|
||||||
@property() public computeSuffix?: (schema: HaFormSchema) => string;
|
@property() public computeSuffix?: (schema: HaFormSchema) => string;
|
||||||
@query("ha-form") private _childForm?: HaForm;
|
|
||||||
@query("#element") private _elementContainer?: HTMLDivElement;
|
|
||||||
|
|
||||||
public focus() {
|
public focus() {
|
||||||
const input = this._childForm
|
const input =
|
||||||
? this._childForm
|
this.shadowRoot!.getElementById("child-form") ||
|
||||||
: this._elementContainer
|
this.shadowRoot!.querySelector("ha-form");
|
||||||
? this._elementContainer.lastChild
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
if (!input) {
|
if (!input) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(input as HTMLElement).focus();
|
(input as HTMLElement).focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,40 +144,16 @@ export class HaForm extends LitElement implements HaFormElement {
|
|||||||
</div>
|
</div>
|
||||||
`
|
`
|
||||||
: ""}
|
: ""}
|
||||||
<div id="element"></div>
|
${dynamicElement(`ha-form-${this.schema.type}`, {
|
||||||
|
schema: this.schema,
|
||||||
|
data: this.data,
|
||||||
|
label: this._computeLabel(this.schema),
|
||||||
|
suffix: this._computeSuffix(this.schema),
|
||||||
|
id: "child-form",
|
||||||
|
})}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected updated(changedProperties: PropertyValues) {
|
|
||||||
const schemaChanged = changedProperties.has("schema");
|
|
||||||
const oldSchema = schemaChanged
|
|
||||||
? changedProperties.get("schema")
|
|
||||||
: undefined;
|
|
||||||
if (
|
|
||||||
!Array.isArray(this.schema) &&
|
|
||||||
schemaChanged &&
|
|
||||||
(!oldSchema || (oldSchema as HaFormSchema).type !== this.schema.type)
|
|
||||||
) {
|
|
||||||
const element = document.createElement(
|
|
||||||
`ha-form-${this.schema.type}`
|
|
||||||
) as HaFormElement;
|
|
||||||
element.schema = this.schema;
|
|
||||||
element.data = this.data;
|
|
||||||
element.label = this._computeLabel(this.schema);
|
|
||||||
element.suffix = this._computeSuffix(this.schema);
|
|
||||||
if (this._elementContainer!.lastChild) {
|
|
||||||
this._elementContainer!.removeChild(this._elementContainer!.lastChild);
|
|
||||||
}
|
|
||||||
this._elementContainer!.appendChild(element);
|
|
||||||
} else if (this._elementContainer && this._elementContainer.lastChild) {
|
|
||||||
const element = this._elementContainer!.lastChild as HaFormElement;
|
|
||||||
element.schema = this.schema;
|
|
||||||
element.data = this.data;
|
|
||||||
element.label = this._computeLabel(this.schema);
|
|
||||||
element.suffix = this._computeSuffix(this.schema);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private _computeLabel(schema: HaFormSchema) {
|
private _computeLabel(schema: HaFormSchema) {
|
||||||
return this.computeLabel
|
return this.computeLabel
|
||||||
? this.computeLabel(schema)
|
? this.computeLabel(schema)
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
LitElement,
|
LitElement,
|
||||||
property,
|
property,
|
||||||
} from "lit-element";
|
} from "lit-element";
|
||||||
import { dynamicContentDirective } from "../../../../common/dom/dynamic-content-directive";
|
import { dynamicElement } from "../../../../common/dom/dynamic-element-directive";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
import { HomeAssistant } from "../../../../types";
|
import { HomeAssistant } from "../../../../types";
|
||||||
@ -266,7 +266,7 @@ export default class HaAutomationTriggerRow extends LitElement {
|
|||||||
</paper-listbox>
|
</paper-listbox>
|
||||||
</paper-dropdown-menu-light>
|
</paper-dropdown-menu-light>
|
||||||
<div>
|
<div>
|
||||||
${dynamicContentDirective(
|
${dynamicElement(
|
||||||
`ha-automation-trigger-${this.trigger.platform}`,
|
`ha-automation-trigger-${this.trigger.platform}`,
|
||||||
{ hass: this.hass, trigger: this.trigger }
|
{ hass: this.hass, trigger: this.trigger }
|
||||||
)}
|
)}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user