Fix DOM reuse for trigger/condition/action (#13407)

This commit is contained in:
Paulus Schoutsen 2022-08-19 08:05:15 -04:00 committed by GitHub
parent f3d92ba0e0
commit 5c16447eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 9 deletions

View File

@ -42,13 +42,13 @@ export default class HaAutomationAction extends LitElement {
private _focusLastActionOnChange = false; private _focusLastActionOnChange = false;
private _actionKeys = new WeakMap<Action, string>();
protected render() { protected render() {
return html` return html`
${repeat( ${repeat(
this.actions, this.actions,
// Use the action as key, so moving around keeps the same DOM, (action) => this._getKey(action),
// including expand state
(action) => action,
(action, idx) => html` (action, idx) => html`
<ha-automation-action-row <ha-automation-action-row
.index=${idx} .index=${idx}
@ -95,6 +95,14 @@ export default class HaAutomationAction extends LitElement {
} }
} }
private _getKey(action: Action) {
if (!this._actionKeys.has(action)) {
this._actionKeys.set(action, Math.random().toString());
}
return this._actionKeys.get(action)!;
}
private _addAction(ev: CustomEvent<ActionDetail>) { private _addAction(ev: CustomEvent<ActionDetail>) {
const action = (ev.currentTarget as HaSelect).items[ev.detail.index] const action = (ev.currentTarget as HaSelect).items[ev.detail.index]
.value as typeof ACTION_TYPES[number]; .value as typeof ACTION_TYPES[number];
@ -131,6 +139,10 @@ export default class HaAutomationAction extends LitElement {
if (newValue === null) { if (newValue === null) {
actions.splice(index, 1); actions.splice(index, 1);
} else { } else {
// Store key on new value.
const key = this._getKey(actions[index]);
this._actionKeys.set(newValue, key);
actions[index] = newValue; actions[index] = newValue;
} }

View File

@ -38,6 +38,8 @@ export default class HaAutomationCondition extends LitElement {
private _focusLastConditionOnChange = false; private _focusLastConditionOnChange = false;
private _conditionKeys = new WeakMap<Condition, string>();
protected updated(changedProperties: PropertyValues) { protected updated(changedProperties: PropertyValues) {
if (!changedProperties.has("conditions")) { if (!changedProperties.has("conditions")) {
return; return;
@ -79,9 +81,7 @@ export default class HaAutomationCondition extends LitElement {
return html` return html`
${repeat( ${repeat(
this.conditions, this.conditions,
// Use the condition as key, so moving around keeps the same DOM, (condition) => this._getKey(condition),
// including expand state
(condition) => condition,
(cond, idx) => html` (cond, idx) => html`
<ha-automation-condition-row <ha-automation-condition-row
.index=${idx} .index=${idx}
@ -111,6 +111,14 @@ export default class HaAutomationCondition extends LitElement {
`; `;
} }
private _getKey(condition: Condition) {
if (!this._conditionKeys.has(condition)) {
this._conditionKeys.set(condition, Math.random().toString());
}
return this._conditionKeys.get(condition)!;
}
private _addCondition(ev: CustomEvent<ActionDetail>) { private _addCondition(ev: CustomEvent<ActionDetail>) {
const condition = (ev.currentTarget as HaSelect).items[ev.detail.index] const condition = (ev.currentTarget as HaSelect).items[ev.detail.index]
.value as Condition["condition"]; .value as Condition["condition"];
@ -138,6 +146,10 @@ export default class HaAutomationCondition extends LitElement {
if (newValue === null) { if (newValue === null) {
conditions.splice(index, 1); conditions.splice(index, 1);
} else { } else {
// Store key on new value.
const key = this._getKey(conditions[index]);
this._conditionKeys.set(newValue, key);
conditions[index] = newValue; conditions[index] = newValue;
} }

View File

@ -41,13 +41,13 @@ export default class HaAutomationTrigger extends LitElement {
private _focusLastTriggerOnChange = false; private _focusLastTriggerOnChange = false;
private _triggerKeys = new WeakMap<Trigger, string>();
protected render() { protected render() {
return html` return html`
${repeat( ${repeat(
this.triggers, this.triggers,
// Use the trigger as key, so moving around keeps the same DOM, (trigger) => this._getKey(trigger),
// including expand state
(trigger) => trigger,
(trg, idx) => html` (trg, idx) => html`
<ha-automation-trigger-row <ha-automation-trigger-row
.index=${idx} .index=${idx}
@ -91,6 +91,14 @@ export default class HaAutomationTrigger extends LitElement {
} }
} }
private _getKey(action: Trigger) {
if (!this._triggerKeys.has(action)) {
this._triggerKeys.set(action, Math.random().toString());
}
return this._triggerKeys.get(action)!;
}
private _addTrigger(ev: CustomEvent<ActionDetail>) { private _addTrigger(ev: CustomEvent<ActionDetail>) {
const platform = (ev.currentTarget as HaSelect).items[ev.detail.index] const platform = (ev.currentTarget as HaSelect).items[ev.detail.index]
.value as Trigger["platform"]; .value as Trigger["platform"];
@ -118,6 +126,10 @@ export default class HaAutomationTrigger extends LitElement {
if (newValue === null) { if (newValue === null) {
triggers.splice(index, 1); triggers.splice(index, 1);
} else { } else {
// Store key on new value.
const key = this._getKey(triggers[index]);
this._triggerKeys.set(newValue, key);
triggers[index] = newValue; triggers[index] = newValue;
} }