Skip to content

Commit c387d41

Browse files
committed
[IMP] awesome_owl: add auto focus hook on todolist text input
1 parent 1d61c05 commit c387d41

File tree

8 files changed

+46
-33
lines changed

8 files changed

+46
-33
lines changed

awesome_owl/static/src/card.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
</div>
1515
</t>
1616

17-
</templates>
17+
</templates>

awesome_owl/static/src/counter.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
</div>
1010
</t>
1111

12-
</templates>
12+
</templates>

awesome_owl/static/src/playground.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
<TodoList/>
1212
</t>
1313

14-
</templates>
14+
</templates>

awesome_owl/static/src/todoitem.js

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,7 @@ import { Component, useState } from "@odoo/owl";
22

33
export class TodoItem extends Component {
44
static template = "awesome_owl.todoitem";
5-
6-
setup() {
7-
this.state = useState({
8-
todos: [],
9-
idCounter: 0
10-
});
11-
};
12-
13-
addTodo(ev) {
14-
if (ev.keyCode === 13) {
15-
this.state.idCounter++
16-
console.log(this.state.idCounter);
17-
this.state.todos.push({
18-
'id': this.state.idCounter, 'description': ev.target.value, 'isCompleted': false
19-
});
20-
ev.target.value = "";
21-
}
5+
static props = {
6+
todo: Object
227
}
238
}

awesome_owl/static/src/todoitem.xml

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22
<templates xml:space="preserve">
33

44
<t t-name="awesome_owl.todoitem">
5-
<br/>
6-
<input t-on-keyup="ev => this.addTodo(ev)" type="text" placeholder="Enter a new task"/>
7-
<t t-foreach="state.todos" t-as="i" t-key="i.id">
8-
<div style="border: 1px solid black">
9-
<div t-att-class="{'text-muted': i.isCompleted, 'text-decoration-line-through': i.isCompleted}">
10-
<t t-out="i.id+'. '+i.description"/>
11-
</div>
12-
</div>
13-
</t>
5+
<t t-out="props.todo.id+'. '+props.todo.description"/>
146
</t>
157

16-
</templates>
8+
</templates>

awesome_owl/static/src/todolist.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1-
import { Component } from "@odoo/owl";
1+
import { Component, useState, useRef, onMounted } from "@odoo/owl";
22
import { TodoItem } from "./todoitem";
3+
import { useAutofocus } from "./utils";
34

45
export class TodoList extends Component {
56
static template = "awesome_owl.todolist";
67
static components = { TodoItem };
8+
9+
setup() {
10+
this.state = useState({
11+
todos: [],
12+
idCounter: 0
13+
});
14+
useAutofocus("input_todo");
15+
};
16+
17+
addTodo(ev) {
18+
if (ev.keyCode === 13) {
19+
this.state.idCounter++
20+
this.state.todos.push({
21+
'id': this.state.idCounter, 'description': ev.target.value, 'isCompleted': false
22+
});
23+
ev.target.value = "";
24+
}
25+
};
726
}

awesome_owl/static/src/todolist.xml

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22
<templates xml:space="preserve">
33

44
<t t-name="awesome_owl.todolist">
5-
<TodoItem/>
5+
<br/>
6+
<input t-ref="input_todo" t-on-keyup="ev => this.addTodo(ev)" type="text" placeholder="Enter a new task"/>
7+
<t t-foreach="state.todos" t-as="i" t-key="i.id">
8+
<div style="border: 1px solid black">
9+
<div t-att-class="{'text-muted': i.isCompleted, 'text-decoration-line-through': i.isCompleted}">
10+
<TodoItem todo= "i"/>
11+
</div>
12+
</div>
13+
</t>
614
</t>
715

8-
</templates>
16+
</templates>

awesome_owl/static/src/utils.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useEffect, useRef } from "@odoo/owl";
2+
3+
export function useAutofocus(refName) {
4+
let ref = useRef(refName);
5+
useEffect(
6+
(el) => el && el.focus(),
7+
() => [ref.el]
8+
);
9+
}

0 commit comments

Comments
 (0)