Skip to content

Commit f19357f

Browse files
committed
Complete Todo App
1 parent e7cbcbd commit f19357f

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

todo-app/src/Todo.jsx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,60 @@
1-
import React from "react";
1+
import React, { useState } from "react";
22

33
function Todo() {
4-
return <div>Todo</div>;
4+
const [input, setInput] = useState("");
5+
const [todos, setTodos] = useState([]);
6+
7+
const handleSubmit = () => {
8+
setTodos((todos) =>
9+
todos.concat({
10+
text: input,
11+
id: crypto.randomUUID(),
12+
})
13+
);
14+
15+
setInput("");
16+
17+
console.log(todos);
18+
};
19+
20+
const removeTodo = (id) => {
21+
setTodos((todos) => todos.filter((t) => t.id !== id));
22+
};
23+
24+
return (
25+
<div className="container">
26+
<input
27+
type="text"
28+
placeholder="New ToDo"
29+
value={input}
30+
onChange={(e) => {
31+
setInput(e.target.value);
32+
}}
33+
/>
34+
35+
<button type="submit" onClick={handleSubmit}>
36+
Submit
37+
</button>
38+
39+
<ul className="todos">
40+
{todos.map(({ text, id }) => {
41+
return (
42+
<li className="todo" key={id}>
43+
<span>{text}</span>
44+
<button
45+
className="close"
46+
onClick={() => {
47+
removeTodo(id);
48+
}}
49+
>
50+
X
51+
</button>
52+
</li>
53+
);
54+
})}
55+
</ul>
56+
</div>
57+
);
558
}
659

760
export default Todo;

todo-app/src/index.css

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
* {
2+
padding: 0;
3+
margin: 0;
4+
box-sizing: border-box;
5+
}
6+
7+
body {
8+
background: #f5f9eb;
9+
height: 100vh;
10+
display: flex;
11+
justify-content: center;
12+
align-items: center;
13+
}
14+
15+
.container {
16+
background: #fcfff3;
17+
padding: 50px;
18+
}
19+
20+
input {
21+
padding: 15px;
22+
border: none;
23+
outline: none;
24+
background: #f5f9eb;
25+
width: 300px;
26+
margin-right: 10px;
27+
}
28+
29+
button {
30+
border-radius: 100px;
31+
background: #454545;
32+
padding: 5px;
33+
outline: none;
34+
border: none;
35+
color: #fff;
36+
padding: 10px 20px;
37+
cursor: pointer;
38+
}
39+
40+
.todos-list {
41+
margin-top: 3rem;
42+
}
43+
44+
.todo {
45+
list-style: none;
46+
display: flex;
47+
justify-content: space-between;
48+
align-items: center;
49+
background: #f5f9eb;
50+
padding: 7px 20px;
51+
margin: 10px;
52+
font-family: sans-serif;
53+
}
54+
55+
.close {
56+
padding: 5px 10px;
57+
cursor: pointer;
58+
}

0 commit comments

Comments
 (0)