Skip to content

Commit 108f182

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

File tree

8 files changed

+50
-33
lines changed

8 files changed

+50
-33
lines changed

awesome_owl/static/src/card.xml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 17 deletions
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

Lines changed: 2 additions & 10 deletions
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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
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+
// this.inputRef = useRef("input_todo");
16+
// onMounted(() => {
17+
// this.inputRef.el.focus();
18+
// })
19+
};
20+
21+
addTodo(ev) {
22+
if (ev.keyCode === 13) {
23+
this.state.idCounter++
24+
this.state.todos.push({
25+
'id': this.state.idCounter, 'description': ev.target.value, 'isCompleted': false
26+
});
27+
ev.target.value = "";
28+
}
29+
};
730
}

awesome_owl/static/src/todolist.xml

Lines changed: 10 additions & 2 deletions
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

Lines changed: 9 additions & 0 deletions
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)