11/** @odoo -module **/
2- import { Component , useState } from "@odoo/owl" ;
2+ import { Component , useState , useRef , onMounted } from "@odoo/owl" ;
33import { TodoItem } from "./todo_item" ;
44
55export class TodoList extends Component {
@@ -8,12 +8,49 @@ export class TodoList extends Component {
88 static components = { TodoItem } ;
99
1010 setup ( ) {
11- this . state = useState ( {
12- todos : [
13- { id : 1 , description : "buy milk" , isCompleted : false } ,
14- { id : 2 , description : "write report" , isCompleted : true } ,
15- { id : 3 , description : "call friend" , isCompleted : false } ,
16- ]
11+ // this.state = useState({
12+ // todos: [
13+ // { id: 1, description: "buy milk", isCompleted: false },
14+ // { id: 2, description: "write report", isCompleted: true },
15+ // { id: 3, description: "call friend", isCompleted: false },
16+ // ]
17+ // });
18+ this . state = useState ( {
19+ todos : [ ] ,
20+ nextId : 1 ,
21+ } ) ;
22+ this . inputRef = useRef ( "input" ) ;
23+ onMounted ( ( ) => {
24+ this . inputRef . el . focus ( ) ;
1725 } ) ;
18- }
26+
27+ }
28+ addTodo ( ev ) {
29+ if ( ev . keyCode === 13 ) {
30+ const value = ev . target . value . trim ( ) ;
31+ if ( ! value ) {
32+ return ;
33+ }
34+ this . state . todos . push ( {
35+ id : this . state . nextId ++ ,
36+ description : value ,
37+ isCompleted : false ,
38+ } ) ;
39+ ev . target . value = "" ;
40+ }
41+ }
42+
43+ toggleTodo ( id ) {
44+ const todo = this . state . todos . find ( t => t . id === id ) ;
45+ if ( todo ) {
46+ todo . isCompleted = ! todo . isCompleted ;
47+ }
48+ }
49+
50+ removeTodo ( id ) {
51+ const index = this . state . todos . findIndex ( t => t . id === id ) ;
52+ if ( index >= 0 ) {
53+ this . state . todos . splice ( index , 1 ) ;
54+ }
55+ }
1956}
0 commit comments