const editTodo = (id, title) => {
setTodos(todos.map(n => n.id === id
? { ...n, title }
: n
));
};
function TodoItem({ complete, title, id, removeTodo, toggleTodo, editTodo }) {
const [ isEdit, setIsEdit ] = useState(false);
const [ editValue, setEditValue ] = useState('');
return (
<div className="todo-item">
<input type="checkbox" checked={complete} onChange={() => toggleTodo(id)} />
{isEdit
? <>
<input value={editValue} onChange={e => setEditValue(e.target.value)} />
<button onClick={() => (setIsEdit(false), editTodo(id, editValue))}>Save</button>
<button onClick={() => setIsEdit(false)}>Cancel</button>
</>
: <span onDoubleClick={() => (setIsEdit(true), setEditValue(title))}>{title}</span>
}
<button onClick={() => removeTodo(id)}>X</button>
</div>
);
}