Implement a React-based Todo List application. Complete the TodoListEasy
component with functionality to add, toggle, and remove todo items.
addTodo
functionremoveTodo
functiontoggleTodo
function{id: number, text: string, completed: boolean}
<ul>
with data-testid="todo-list"
data-testid="error-message"
for error display1{id: 1, text: "My todo test", completed: false}
2
import React, { useState } from 'react'; function TodoListEasy() { const [todos, setTodos] = useState([]); const [newTodo, setNewTodo] = useState(''); const [error, setError] = useState(''); // TODO: Implement addTodo function const addTodo = () => { {/* {id: 1, text: "My todo test", completed: true} */} }; // TODO: Implement toggleTodo function const toggleTodo = () => { }; // TODO: Implement removeTodo function const removeTodo = () => { }; return ( <div> <h1>Todo List</h1> <form onSubmit={addTodo} aria-label="Add New Todo" data-testid="todo-form" style={{ display: 'flex', gap: '10px', marginBottom: '10px' }} > <label htmlFor="new-todo">New Todo</label> <input id="new-todo" type="text" value={newTodo} placeholder="Enter a new todo" data-testid="new-todo-input" /> <button type="submit" data-testid="add-todo-button" >Add Todo</button> </form> {error && <p role="alert" data-testid="error-message">{error}</p>} <ul data-testid="todo-list"> {todos.map((todo) => ( <li key={todo.id} style={{ display: 'flex', gap: '10px', marginBottom: '10px' }}> <input type="checkbox" checked={false} data-testid={`todo-item-${todo.id}-checkbox`} /> <span style={{ textDecoration: todo.completed ? 'line-through' : 'none' }} data-testid={`todo-item-${todo.id}-text`} > {todo.text} </span> {/* TODO: Complete functionality */} <button onClick={() => removeTodo(todo.id)} aria-label={`Remove ${todo.text}`} data-testid={`todo-item-${todo.id}-remove-button`} > Remove </button> </li> ))} </ul> </div> ); } export default TodoListEasy;
Tests