File tree Expand file tree Collapse file tree 5 files changed +47
-5
lines changed Expand file tree Collapse file tree 5 files changed +47
-5
lines changed Original file line number Diff line number Diff line change @@ -11,5 +11,20 @@ export class TodoItem extends Component {
11
11
isCompleted : Boolean ,
12
12
}
13
13
} ,
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 ) ;
14
29
}
15
30
}
Original file line number Diff line number Diff line change 1
1
<?xml version =" 1.0" encoding =" UTF-8" ?>
2
2
<templates xml : space =" preserve" >
3
3
<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" />
7
11
</div >
8
12
</t >
9
13
</templates >
Original file line number Diff line number Diff line change 1
1
import { Component , useState } from "@odoo/owl" ;
2
2
import { TodoItem } from "./todo_item" ;
3
+ import { useAutoFocus } from "../utils"
3
4
4
5
export class TodoList extends Component {
5
6
static template = "awesome_owl.TodoList"
@@ -8,6 +9,7 @@ export class TodoList extends Component {
8
9
9
10
setup ( ) {
10
11
this . todos = useState ( [ ] ) ;
12
+ useAutoFocus ( ) ;
11
13
}
12
14
13
15
addTask ( event ) {
@@ -20,4 +22,17 @@ export class TodoList extends Component {
20
22
} ) ;
21
23
event . target . value = '' ;
22
24
}
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
+ }
23
38
}
Original file line number Diff line number Diff line change 3
3
<t t-name =" awesome_owl.TodoList" >
4
4
<div class =" border p-2 m-5" style =" width: 400px;" >
5
5
<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 >
7
7
<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 " />
9
9
</t >
10
10
</div >
11
11
</t >
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments