Skip to content

Commit 9cb4c74

Browse files
committed
[IMP] awesome_owl: implement add & delete task functionality in todo list and improve UI for task view.
- Focus input field on mount in TodoList component using useAutoFocus() method. - Add checkbox to TodoItem with toggleState callback to update isCompleted status on change - Add removeTodo callback to TodoItem and trigger it via click on remove icon, to delete the task.
1 parent 270300f commit 9cb4c74

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

awesome_owl/static/src/todo/todo_item.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,20 @@ export class TodoItem extends Component {
1111
isCompleted: Boolean,
1212
}
1313
},
14+
toggleTaskState: {
15+
type: Function,
16+
},
17+
taskDelete: {
18+
type: Function,
19+
}
20+
21+
}
22+
23+
toggleTaskState() {
24+
this.props.toggleTaskState(this.props.todo.id);
25+
}
26+
27+
taskDelete() {
28+
this.props.taskDelete(this.props.todo.id);
1429
}
1530
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<templates xml:space="preserve">
33
<t t-name="awesome_owl.TodoItem">
4-
<div class="d-flex mb-1" t-att-class="{'text-muted text-decoration-line-through': props.todo.isCompleted}">
5-
<span><t t-esc="props.todo.id"></t>.</span>
6-
<span class="ms-1"><t t-esc="props.todo.description"></t></span>
4+
<div class="d-flex align-items-center gap-2">
5+
<input t-att-id="props.todo.id" type="checkbox" class="form-check-input mb-2" t-att-checked="props.todo.isCompleted" t-on-change="toggleTaskState"/>
6+
<div class="d-flex mb-1 p-1 ps-2 flex-grow-1" t-att-for="props.todo.id" t-att-style="!props.todo.isCompleted ? 'border-radius: 8px; background: #eaeaea;' : ''" t-att-class="{'text-muted text-decoration-line-through': props.todo.isCompleted}">
7+
<span class="pe-2" style="border-right: 1px solid #999;"><t t-esc="props.todo.id"></t>.</span>
8+
<span class="ms-2"><t t-esc="props.todo.description"></t></span>
9+
</div>
10+
<span class="fa fa-trash-o text-danger mb-2" t-on-click="taskDelete"/>
711
</div>
812
</t>
913
</templates>

awesome_owl/static/src/todo/todo_list.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component, useState } from "@odoo/owl";
22
import { TodoItem } from "./todo_item";
3+
import { useAutoFocus } from "../utils"
34

45
export class TodoList extends Component {
56
static template = "awesome_owl.TodoList"
@@ -8,6 +9,7 @@ export class TodoList extends Component {
89

910
setup () {
1011
this.todos = useState([]);
12+
useAutoFocus();
1113
}
1214

1315
addTask(event) {
@@ -20,4 +22,17 @@ export class TodoList extends Component {
2022
});
2123
event.target.value = '';
2224
}
25+
26+
toggleTaskState(todo_id) {
27+
const task = this.todos.find((t) => t.id === todo_id);
28+
if (!task) return;
29+
task.isCompleted = !task.isCompleted;
30+
}
31+
32+
taskDelete(todo_id) {
33+
const taskIndex = this.todos.findIndex((t) => t.id === todo_id)
34+
if (taskIndex >= 0) {
35+
this.todos.splice(taskIndex, 1);
36+
}
37+
}
2338
}

awesome_owl/static/src/todo/todo_list.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<t t-name="awesome_owl.TodoList">
44
<div class="border p-2 m-5" style="width: 400px;">
55
<h3 class="mb-3">Todo List</h3>
6-
<input id="task-entry" placeholder="Enter a new task" class="form-control" t-on-keyup="addTask"></input>
6+
<input id="task-entry" placeholder="Enter a new task" class="form-control mb-3" t-on-keyup="addTask" t-ref="focus-input"></input>
77
<t t-foreach="todos" t-as="todo" t-key="todo.id">
8-
<TodoItem todo="todo"/>
8+
<TodoItem todo="todo" toggleTaskState.bind="toggleTaskState" taskDelete.bind="taskDelete"/>
99
</t>
1010
</div>
1111
</t>

awesome_owl/static/src/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { useRef, onMounted } from "@odoo/owl";
2+
3+
export const useAutoFocus = () => {
4+
const inputRef = useRef("focus-input");
5+
onMounted(() => {
6+
inputRef.el?.focus();
7+
})
8+
}

0 commit comments

Comments
 (0)