-
Notifications
You must be signed in to change notification settings - Fork 0
/
TodoItem.jsx
71 lines (62 loc) · 2.13 KB
/
TodoItem.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React, { useState } from 'react';
import PropTypes from 'prop-types';
function TodoItem({ task, deleteTask, toggleCompleted, editTask }) {
const [isEditing, setIsEditing] = useState(false);
const [editedText, setEditedText] = useState(task.text);
const handleCheckboxChange = () => {
toggleCompleted(task.id);
};
const handleEditButtonClick = () => {
setIsEditing(true);
};
const handleEditInputChange = (e) => {
setEditedText(e.target.value);
};
const handleEditFormSubmit = (e) => {
e.preventDefault();
editTask(task.id, editedText);
setIsEditing(false); // Set editing mode to false after saving changes
};
console.log('isEditing:', isEditing); // Add this line for debugging
return (
<div className="todo-item">
<input
type="checkbox"
checked={task.completed}
onChange={handleCheckboxChange}
/>
{isEditing ? (
<form onSubmit={handleEditFormSubmit}>
<input
type="text"
value={editedText}
onChange={handleEditInputChange}
autoFocus
/>
<button type="submit">Save</button>
</form>
) : (
<>
<p style={{ textDecoration: task.completed ? 'line-through' : 'none' }}>
{task.text}
</p>
{!isEditing && (
<button onClick={handleEditButtonClick}>Edit</button>
)}
</>
)}
<button onClick={() => deleteTask(task.id)}>DELETE</button>
</div>
);
}
TodoItem.propTypes = {
task: PropTypes.shape({
id: PropTypes.number.isRequired,
text: PropTypes.string.isRequired,
completed: PropTypes.bool.isRequired
}),
deleteTask: PropTypes.func.isRequired,
toggleCompleted: PropTypes.func.isRequired,
editTask: PropTypes.func.isRequired
};
export default TodoItem;