-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodolist3
139 lines (125 loc) · 3.24 KB
/
todolist3
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
import React, { useState } from 'react';
import { Layout, Row, Col, Input, Button, List, Checkbox, Typography } from 'antd';
const { Header, Content } = Layout;
const { Title } = Typography;
// TodoInput Component
const TodoInput = ({ onAddTodo }) => {
const [newTodoText, setNewTodoText] = useState('');
const handleInputChange = (e) => {
setNewTodoText(e.target.value);
};
const handleSubmit = () => {
if (newTodoText.trim() !== '') {
onAddTodo({
text: newTodoText.trim(),
completed: false,
});
setNewTodoText('');
}
};
return (
<div>
<Input.Group compact>
<Input
placeholder="Add new task"
value={newTodoText}
onChange={handleInputChange}
/>
<Button type="primary" onClick={handleSubmit}>
Add
</Button>
</Input.Group>
</div>
);
};
// TodoList Component
const TodoList = ({ todos, onDeleteTodo, onToggleComplete }) => {
return (
<div>
<Title level={3}>To-Do List</Title>
<List
itemLayout="horizontal"
dataSource={todos}
renderItem={(item, index) => (
<List.Item actions={[
<Button type="primary" onClick={() => onDeleteTodo(index)}>
Delete
</Button>,
]}>
<List.Item.Meta
title={
<Checkbox
checked={item.completed}
onChange={() => onToggleComplete(index)}
>
{item.text}
</Checkbox>
}
/>
</List.Item>
)}
/>
</div>
);
};
// App Component
function App() {
const [todos, setTodos] = useState([]);
const handleAddTodo = (newTodo) => {
setTodos([...todos, newTodo]);
};
const handleDeleteTodo = (index) => {
const updatedTodos = [...todos];
updatedTodos.splice(index, 1);
setTodos(updatedTodos);
};
const handleToggleComplete = (index) => {
const updatedTodos = [...todos];
updatedTodos[index].completed = !updatedTodos[index].completed;
setTodos(updatedTodos);
};
return (
<Layout>
<Header style={{ padding: '0 24px' }}>
<h1 style={{ color: 'white' }}>To-Do List</h1>
</Header>
<Content style={{ padding: '24px' }}>
<Row gutter={[16, 16]}>
<Col span={12}>
<TodoInput onAddTodo={handleAddTodo} />
</Col>
<Col span={12}>
<TodoList
todos={todos}
onDeleteTodo={handleDeleteTodo}
onToggleComplete={handleToggleComplete}
/>
</Col>
</Row>
</Content>
</Layout>
);
}
return (
<Layout>
<Header style={{ padding: '0 24px' }}>
<h1 style={{ color: 'white' }}>To-Do List</h1>
</Header>
<Content style={{ padding: '24px' }}>
<Row gutter={[16, 16]}>
<Col span={12}>
<TodoInput onAddTodo={handleAddTodo} />
</Col>
<Col span={12}>
<TodoList
todos={todos}
onDeleteTodo={handleDeleteTodo}
onToggleComplete={handleToggleComplete}
/>
</Col>
</Row>
</Content>
</Layout>
);
}
export default App;