Skip to content

Commit 16065f8

Browse files
committed
[IMP] awesome_owl: tasks 8-12 (todo list)
1 parent 09aff26 commit 16065f8

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

awesome_owl/static/src/todo_list/todo_item.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ export class TodoItem extends Component {
77
todo : {
88
id: Number,
99
description: String,
10-
isCompleted: Boolean }
11-
10+
isCompleted: Boolean },
11+
onClick : { Type: Function, optional : true }
1212
};
13+
14+
toggleState() {
15+
this.props.todo.isCompleted = !this.props.todo.isCompleted;
16+
}
17+
18+
removeTodo() {
19+
this.props.onClick(this.props.todo.id);
20+
}
1321
}

awesome_owl/static/src/todo_list/todo_item.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
<t t-name="awesome_owl.TodoItem">
55
<div>
6-
<t t-out="props.todo.id"/>.
7-
<t t-out="props.todo.description"/>
6+
<p t-att-class="props.todo.isCompleted ? 'text-muted text-decoration-line-through' : ''">
7+
<input type="checkbox" t-att-checked="props.todo.isCompleted" t-on-change="toggleState"/>
8+
<t t-out="props.todo.id"/>.
9+
<t t-out="props.todo.description"/>
10+
<span class="fa fa-remove text-danger" t-on-click="removeTodo"/>
11+
</p>
812
</div>
913
</t>
1014

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, useState } from "@odoo/owl"
1+
import { Component, useState, useRef } from "@odoo/owl"
22
import { TodoItem } from "./todo_item"
33

44
export class TodoList extends Component {
@@ -7,10 +7,30 @@ export class TodoList extends Component {
77

88
setup() {
99
this.todos = useState([
10-
{ id:1, description: "buy bread", isCompleted: false },
11-
{ id:2, description: "buy milk", isCompleted: false },
12-
{ id:3, description: "have lunch", isCompleted: true },
10+
{id: 1, description: "buy bread", isCompleted: false},
11+
{id: 2, description: "buy milk", isCompleted: false},
12+
{id: 3, description: "have lunch", isCompleted: true},
1313
]);
14+
this.todoCounter = this.todos.length;
15+
this.inputRef = useRef("todo_input")
16+
}
17+
18+
addTodo(ev) {
19+
if (ev.keyCode === 13) {
20+
const input_text = this.inputRef.el.value.trim();
21+
if (input_text) {
22+
this.todoCounter++;
23+
this.todos.push({id: this.todoCounter, description: input_text, isCompleted: false});
24+
this.inputRef.el.value = "";
25+
}
26+
}
27+
}
28+
29+
removeTodo(id) {
30+
const index = this.todos.findIndex(todo => todo.id === id);
31+
if (index >= 0) {
32+
this.todos.splice(index, 1);
33+
}
1434
}
1535
}
1636

awesome_owl/static/src/todo_list/todo_list.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
<t t-name="awesome_owl.TodoList">
55
<div>
66
<p>Todo List:</p>
7+
<input placeholder="Add a todo" t-on-keyup="addTodo" t-ref="todo_input"/>
78
<p t-foreach="todos" t-as="td" t-key="td.id">
8-
<TodoItem todo="td"/>
9+
<TodoItem todo="td" onClick.bind="removeTodo"/>
910
</p>
1011
</div>
1112
</t>

0 commit comments

Comments
 (0)