Section 12

This commit is contained in:
Matt Marcha 2024-08-21 16:15:54 -10:00
parent cae25a4fa2
commit 6396160269
4 changed files with 17 additions and 3 deletions

View file

@ -10,9 +10,14 @@ export class TodoItem extends Component {
shape: { id: Number, description: String, isCompleted: Boolean }
},
onToggle: Function,
removeTodo: Function,
};
toggle() {
this.props.onToggle(this.props.todo.id);
}
remove() {
this.props.removeTodo(this.props.todo.id);
}
}

View file

@ -7,6 +7,7 @@
<t t-esc="props.todo.id"/>.
<t t-esc="props.todo.description"/>
</label>
<span class="fa fa-remove" t-on-click="remove"/>
</div>
</t>
</templates>

View file

@ -10,6 +10,7 @@ export class TodoList extends Component {
static components = { TodoItem };
setup() {
this.todoCounter = 1;
this.todos = useState([]);
useAutofocus('todo_input');
}
@ -19,7 +20,7 @@ export class TodoList extends Component {
if(ev.keyCode === 13) {
if(ev.target.value !== '') {
// Let's create a todo
this.todos.push({ id: this.todos.length+1, description: ev.target.value, isCompleted: false });
this.todos.push({ id: this.todoCounter++, description: ev.target.value, isCompleted: false });
ev.target.value = '';
}
}
@ -31,4 +32,11 @@ export class TodoList extends Component {
todo.isCompleted = !todo.isCompleted;
}
}
removeItem(id) {
const todoIndex = this.todos.findIndex((obj) => obj.id === id);
if (todoIndex >= 0) {
this.todos.splice(todoIndex, 1);
}
}
}

View file

@ -4,7 +4,7 @@
<p><input t-ref="todo_input" type="text" name="new-todo" placeholder="Add new todo" t-on-keyup="addTodo" /></p>
<div class="d-inline-block border p-2 m-2">
<t t-foreach="todos" t-as="todo" t-key="todo.id">
<TodoItem todo="todo" onToggle.bind="toggleState"/>
<TodoItem todo="todo" onToggle.bind="toggleState" removeTodo.bind="removeItem"/>
</t>
</div>
</t>