Skip to content

Commit 09bfd27

Browse files
committed
[IMP] awesome_owl: add sections 8-9
1 parent a22285a commit 09bfd27

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

awesome_owl/static/src/todo_list/todo_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<templates xml:space="preserve">
33

44
<t t-name="awesome_owl.todo_item">
5-
<div class="todo_item-body">
5+
<div class="todo_item-body" t-att-class="{'text-muted text-decoration-line-through': this.todo.isCompleted}">
66
<p class="todo_item-text">
77
<t t-esc="this.todo.id"/>. <t t-out="this.todo.description"/>
88
</p>
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import { Component, useState } from "@odoo/owl";
1+
import { Component, useState, useRef, onMounted } 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.todo_list"
67
static components = { TodoItem }
78

89
setup() {
9-
this.todos = useState([{ id: 3, description: "buy milk", isCompleted: false }]);
10+
useAutoFocus("todo-input")
11+
this.state = useState({todos: [
12+
{ id: 2, description: "penser à acheter du lait", isCompleted: false },
13+
{ id: 3, description: "penser à acheter des tomates cerises", isCompleted: true }
14+
],
15+
lastId: 3
16+
});
17+
}
18+
19+
addTodo(ev) {
20+
if (ev.keyCode === 13 && ev.target.value != "") {
21+
this.state.todos.push({id: ++this.state.lastId, description: ev.target.value, isCompleted: false});
22+
ev.target.value = ""
23+
}
1024
}
1125
}

awesome_owl/static/src/todo_list/todo_list.xml

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

44
<t t-name="awesome_owl.todo_list">
55
<div class="card d-inline-block m-2">
6-
<div class="todo_list-body">
7-
<t t-foreach="this.todos" t-as="i" t-key="i">
6+
<div class="todo_list-body border p-2 m-2">
7+
<input
8+
type="text"
9+
t-on-keyup="addTodo"
10+
placeholder="Enter something to do..."
11+
t-ref="todo-input"
12+
/>
13+
<t t-foreach="this.state.todos" t-as="i" t-key="i.id">
814
<TodoItem todo="i"/>
915
</t>
1016
</div>

awesome_owl/static/src/utils.js

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

0 commit comments

Comments
 (0)