-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist11
201 lines (181 loc) · 6.03 KB
/
todolist11
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React, { useState } from 'react';
import { Layout, Row, Col, Input, Button, List, Checkbox, Typography, Modal, Form, Input as InputAnt, message, Space } from 'antd';
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
const { Header, Content, Footer } = Layout;
const { Title } = Typography;
// TodoInput Component
const TodoInput = ({ onAddTodo }) => {
const [newTodoText, setNewTodoText] = useState('');
const handleInputChange = (event) => {
setNewTodoText(event.target.value);
};
const handleAddTodo = () => {
if (newTodoText.trim() !== '') {
onAddTodo({ id: Date.now(), text: newTodoText.trim(), completed: false });
setNewTodoText('');
}
};
return (
<div style={{ display: 'flex', alignItems: 'center' }}>
<Input
placeholder="Add new task"
value={newTodoText}
onChange={handleInputChange}
style={{ width: 'calc(100% - 80px)' }} // Adjust width for button
/>
<Button type="primary" onClick={handleAddTodo} style={{ marginLeft: 8 }}>
Add
</Button>
</div>
);
};
// TodoList Component
const TodoList = ({ todos, onDeleteTodo, onToggleComplete, onEditTodo, filter, setFilter, onDeleteDone, onDeleteAll }) => {
const [isEditing, setIsEditing] = useState(false);
const [editingTodo, setEditingTodo] = useState(null);
const handleEditTodo = (todo) => {
setIsEditing(true);
setEditingTodo(todo);
};
const handleCancelEdit = () => {
setIsEditing(false);
setEditingTodo(null);
};
const handleSaveEdit = (values) => {
onEditTodo(editingTodo.id, values.text);
setIsEditing(false);
setEditingTodo(null);
};
const filteredTodos = filter === 'all'
? todos
: filter === 'completed'
? todos.filter((todo) => todo.completed)
: todos.filter((todo) => !todo.completed);
return (
<div>
<Title level={3}>To-Do List</Title>
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
<Button onClick={() => setFilter('all')} type={filter === 'all' ? 'primary' : 'default'} style={{ backgroundColor: '#00a0e9', border: 'none', textAlign: 'left' }}>
All
</Button>
<Button onClick={() => setFilter('completed')} type={filter === 'completed' ? 'primary' : 'default'} style={{ backgroundColor: '#00a0e9', border: 'none' }}>
Completed
</Button>
<Button onClick={() => setFilter('active')} type={filter === 'active' ? 'primary' : 'default'} style={{ backgroundColor: '#00a0e9', border: 'none', textAlign: 'right' }}>
Active
</Button>
</div>
<List
itemLayout="horizontal"
dataSource={filteredTodos}
renderItem={(item) => (
<List.Item>
<List.Item.Meta
title={
<span style={{ textDecoration: item.completed ? 'line-through' : 'none' }}>
{item.text}
</span>
}
/>
<Space>
<Checkbox checked={item.completed} onChange={() => onToggleComplete(item.id)} />
<Button type="primary" onClick={() => handleEditTodo(item)} icon={<EditOutlined />} />
<Button type="danger" onClick={() => onDeleteTodo(item.id)} icon={<DeleteOutlined />} />
</Space>
</List.Item>
)}
/>
<Modal
title="Edit Task"
visible={isEditing}
onCancel={handleCancelEdit}
footer={[
<Button key="back" onClick={handleCancelEdit}>
Cancel
</Button>,
<Button key="submit" type="primary" onClick={() => handleSaveEdit}>
Save
</Button>,
]}
>
<Form
name="edit-todo-form"
initialValues={{ text: editingTodo ? editingTodo.text : '' }}
onFinish={handleSaveEdit}
>
<Form.Item name="text" label="Task" rules={[{ required: true }]}>
<InputAnt />
</Form.Item>
</Form>
</Modal>
</div>
);
};
// App Component
function App() {
const [todos, setTodos] = useState([]);
const [filter, setFilter] = useState('all');
const handleAddTodo = (newTodo) => {
setTodos([...todos, newTodo]);
};
const handleDeleteTodo = (id) => {
setTodos(todos.filter((todo) => todo.id !== id));
};
const handleToggleComplete = (id) => {
setTodos(
todos.map((todo) =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
)
);
};
const handleEditTodo = (id, newText) => {
setTodos(
todos.map((todo) =>
todo.id === id ? { ...todo, text: newText } : todo
)
);
};
const handleDeleteDone = () => {
setTodos(todos.filter((todo) => !todo.completed));
};
const handleDeleteAll = () => {
setTodos([]);
};
return (
<Layout style={{ maxWidth: '700px', margin: '0 auto' }}> {/* Add max-width and margin */}
<Header style={{ padding: '0 24px' }}>
<h1 style={{ color: 'white' }}>To-Do List</h1>
</Header>
<Content style={{ padding: '24px' }}>
<Row gutter={[16, 16]}>
<Col span={24}>
<TodoInput onAddTodo={handleAddTodo} />
</Col>
<Col span={24}>
<TodoList
todos={todos}
onDeleteTodo={handleDeleteTodo}
onToggleComplete={handleToggleComplete}
onEditTodo={handleEditTodo}
filter={filter}
setFilter={setFilter}
onDeleteDone={handleDeleteDone}
onDeleteAll={handleDeleteAll}
/>
</Col>
</Row>
</Content>
<Footer style={{ textAlign: 'center' }}>
<Space>
<Button type="danger" onClick={handleDeleteDone} style={{ backgroundColor: '#f5222d', border: 'none' }}>
Delete done tasks
</Button>
<Button type="danger" onClick={handleDeleteAll} style={{ backgroundColor: '#f5222d', border: 'none' }}>
Delete all tasks
</Button>
</Space>
</Footer>
</Layout>
);
}
export default App;