Skip to content

Commit 10e09b2

Browse files
authored
Configurable todo item tap action (#26551)
* Configurable todo item tap action * string
1 parent f9cd2b6 commit 10e09b2

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

src/panels/lovelace/cards/hui-todo-list-card.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ import { createEntityNotFoundWarning } from "../components/hui-warning";
5454
import type { LovelaceCard, LovelaceCardEditor } from "../types";
5555
import type { TodoListCardConfig } from "./types";
5656

57+
export const ITEM_TAP_ACTION_EDIT = "edit";
58+
export const ITEM_TAP_ACTION_TOGGLE = "toggle";
59+
5760
@customElement("hui-todo-list-card")
5861
export class HuiTodoListCard extends LitElement implements LovelaceCard {
5962
public static async getConfigElement(): Promise<LovelaceCardEditor> {
@@ -482,7 +485,7 @@ export class HuiTodoListCard extends LitElement implements LovelaceCard {
482485
)}
483486
.itemId=${item.uid}
484487
@change=${this._completeItem}
485-
@click=${this._openItem}
488+
@click=${this._itemTap}
486489
@request-selected=${this._requestSelected}
487490
@keydown=${this._handleKeydown}
488491
>
@@ -575,7 +578,18 @@ export class HuiTodoListCard extends LitElement implements LovelaceCard {
575578
return;
576579
}
577580
if (ev.key === "Enter") {
581+
this._itemTap(ev);
582+
}
583+
}
584+
585+
private _itemTap(ev): void {
586+
if (
587+
!this._config!.item_tap_action ||
588+
this._config!.item_tap_action === ITEM_TAP_ACTION_EDIT
589+
) {
578590
this._openItem(ev);
591+
} else if (this._config!.item_tap_action === ITEM_TAP_ACTION_TOGGLE) {
592+
this._completeItem(ev);
579593
}
580594
}
581595

src/panels/lovelace/editor/config-elements/hui-todo-list-editor.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { html, LitElement, nothing } from "lit";
33
import { customElement, property, state } from "lit/decorators";
44
import memoizeOne from "memoize-one";
55
import { assert, assign, boolean, object, optional, string } from "superstruct";
6+
import { mdiGestureTap } from "@mdi/js";
7+
import {
8+
ITEM_TAP_ACTION_EDIT,
9+
ITEM_TAP_ACTION_TOGGLE,
10+
} from "../../cards/hui-todo-list-card";
611
import { isComponentLoaded } from "../../../../common/config/is_component_loaded";
712
import { fireEvent } from "../../../../common/dom/fire_event";
813
import "../../../../components/ha-alert";
@@ -17,6 +22,8 @@ import { configElementStyle } from "./config-elements-style";
1722
import { TodoListEntityFeature, TodoSortMode } from "../../../../data/todo";
1823
import { supportsFeature } from "../../../../common/entity/supports-feature";
1924

25+
const ITEM_TAP_ACTIONS = [ITEM_TAP_ACTION_EDIT, ITEM_TAP_ACTION_TOGGLE];
26+
2027
const cardConfigStruct = assign(
2128
baseLovelaceCardConfig,
2229
object({
@@ -26,6 +33,7 @@ const cardConfigStruct = assign(
2633
hide_completed: optional(boolean()),
2734
hide_create: optional(boolean()),
2835
display_order: optional(string()),
36+
item_tap_action: optional(string()),
2937
})
3038
);
3139

@@ -64,11 +72,35 @@ export class HuiTodoListEditor
6472
},
6573
},
6674
},
75+
{
76+
name: "interactions",
77+
type: "expandable",
78+
flatten: true,
79+
iconPath: mdiGestureTap,
80+
schema: [
81+
{
82+
name: "item_tap_action",
83+
required: true,
84+
selector: {
85+
select: {
86+
mode: "dropdown",
87+
options: Object.values(ITEM_TAP_ACTIONS).map((action) => ({
88+
value: action,
89+
label: localize(
90+
`ui.panel.lovelace.editor.card.todo-list.actions.${action}`
91+
),
92+
})),
93+
},
94+
},
95+
},
96+
],
97+
},
6798
] as const
6899
);
69100

70101
private _data = memoizeOne((config) => ({
71102
display_order: "none",
103+
item_tap_action: "edit",
72104
...config,
73105
}));
74106

@@ -106,7 +138,10 @@ export class HuiTodoListEditor
106138
}
107139

108140
private _valueChanged(ev: CustomEvent): void {
109-
const config = ev.detail.value;
141+
const config = { ...ev.detail.value };
142+
if (config.item_tap_action === ITEM_TAP_ACTION_EDIT) {
143+
delete config.item_tap_action;
144+
}
110145
fireEvent(this, "config-changed", { config });
111146
}
112147

@@ -130,6 +165,7 @@ export class HuiTodoListEditor
130165
case "hide_completed":
131166
case "hide_create":
132167
case "display_order":
168+
case "item_tap_action":
133169
return this.hass!.localize(
134170
`ui.panel.lovelace.editor.card.todo-list.${schema.name}`
135171
);

src/translations/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7734,6 +7734,11 @@
77347734
"hide_completed": "Hide completed items",
77357735
"hide_create": "Hide 'Add item' field",
77367736
"display_order": "Display order",
7737+
"item_tap_action": "Item tap behavior",
7738+
"actions": {
7739+
"edit": "Default (edit item)",
7740+
"toggle": "Toggle item"
7741+
},
77377742
"sort_modes": {
77387743
"none": "Default",
77397744
"manual": "Manual",

0 commit comments

Comments
 (0)