Skip to content

Commit 0d3344e

Browse files
committed
practice again for job
1 parent 695557e commit 0d3344e

File tree

1 file changed

+93
-48
lines changed

1 file changed

+93
-48
lines changed

src/All Assessment/assessment22/Todo.jsx

Lines changed: 93 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,111 @@
1+
// import React, { useState } from "react";
2+
3+
// const Todo = () => {
4+
// const [todos, setTodos] = useState([]);
5+
// const [todo, setTodo] = useState("");
6+
// console.log(todo)
7+
8+
// const addTodo = () => {
9+
// if(todo.trim() !== ""){
10+
// let newTodo = {id: Date.now() , content: todo}
11+
// setTodos( prev => [...prev, newTodo ])
12+
// }
13+
// };
14+
15+
// const removeTodo = (id) => {
16+
// setTodos(todos.filter(t => t.id !== id))
17+
// };
18+
19+
// const updateTodo = (id, newText) => {
20+
// setTodos(todos.map(t => ( t.id === id ? {...t, content: newText} : t)))
21+
// };
22+
23+
// const handleChange = (e) => {
24+
// setTodo(e.target.value)
25+
// };
26+
27+
// return (
28+
// <div className="flex flex-col h-screen items-center">
29+
// <h1 className="text-5xl font-bold font-sans mt-10">Todo Local Storage</h1>
30+
// <div className="flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 mt-10">
31+
// <input
32+
// className="w-[25rem] px-3 py-1 border border-black rounded-sm"
33+
// type="text"
34+
// placeholder="learn todo"
35+
// value={todo}
36+
// onChange={handleChange}
37+
// />
38+
// <button
39+
// className="bg-red-700 px-10 py-2 text-white font-bold"
40+
// onClick={addTodo}
41+
// >
42+
// Add
43+
// </button>
44+
// </div>
45+
// <div className="mt-10">
46+
// {todos.map((todo) => (
47+
// <div key={todo.id} className="flex space-x-4">
48+
// <p>{todo.content}</p>
49+
// <button
50+
// className="bg-blue-500 px-4 py-1 text-white rounded"
51+
// onClick={() => updateTodo(todo.id, prompt("Update todo", todo.content))}
52+
// >
53+
// Update
54+
// </button>
55+
// <button
56+
// className="bg-red-500 px-4 py-1 text-white rounded"
57+
// onClick={() => removeTodo(todo.id)}
58+
// >
59+
// Remove
60+
// </button>
61+
// </div>
62+
// ))}
63+
// </div>
64+
// </div>
65+
// );
66+
// };
67+
68+
// export default Todo;
69+
170
import React, { useState } from "react";
271

372
const Todo = () => {
4-
const [todos, setTodos] = useState([]);
573
const [todo, setTodo] = useState("");
6-
console.log(todo)
74+
const [todos, setTodos] = useState([]);
75+
const [clicked, setClicked] = useState(false);
76+
console.log(todos);
777

78+
const handleChange = (e) => {
79+
setTodo(e.target.value);
80+
};
881
const addTodo = () => {
9-
if(todo.trim() !== ""){
10-
let newTodo = {id: Date.now() , content: todo}
11-
setTodos( prev => [...prev, newTodo ])
82+
if (todo !== "") {
83+
let newTodo = { id: Date.now(), content: todo };
84+
setTodos((prev) => [...prev, newTodo]);
85+
setTodo("");
1286
}
1387
};
14-
1588
const removeTodo = (id) => {
1689
setTodos(todos.filter(t => t.id !== id))
1790
};
18-
1991
const updateTodo = (id, newText) => {
20-
setTodos(todos.map(t => ( t.id === id ? {...t, content: newText} : t)))
92+
setTodos(todos.filter(t => t.id === id ? {...t, content: newText}: t))
2193
};
22-
23-
const handleChange = (e) => {
24-
setTodo(e.target.value)
25-
};
26-
2794
return (
28-
<div className="flex flex-col h-screen items-center">
29-
<h1 className="text-5xl font-bold font-sans mt-10">Todo Local Storage</h1>
30-
<div className="flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 mt-10">
31-
<input
32-
className="w-[25rem] px-3 py-1 border border-black rounded-sm"
33-
type="text"
34-
placeholder="learn todo"
35-
value={todo}
36-
onChange={handleChange}
37-
/>
38-
<button
39-
className="bg-red-700 px-10 py-2 text-white font-bold"
40-
onClick={addTodo}
41-
>
42-
Add
43-
</button>
44-
</div>
45-
<div className="mt-10">
46-
{todos.map((todo) => (
47-
<div key={todo.id} className="flex space-x-4">
48-
<p>{todo.content}</p>
49-
<button
50-
className="bg-blue-500 px-4 py-1 text-white rounded"
51-
onClick={() => updateTodo(todo.id, prompt("Update todo", todo.content))}
52-
>
53-
Update
54-
</button>
55-
<button
56-
className="bg-red-500 px-4 py-1 text-white rounded"
57-
onClick={() => removeTodo(todo.id)}
58-
>
59-
Remove
60-
</button>
61-
</div>
95+
<div>
96+
97+
<input type="text" onChange={handleChange} value={todo} />
98+
<button onClick={addTodo}>add me</button>
99+
100+
<ul>
101+
{todos.map((t) => (
102+
<div>
103+
104+
<li key={t.id}>{t.content}<button onClick={()=>removeTodo(t.id)}>Remove</button> <button onClick={setClicked(t => !t)}>update</button></li>
105+
{clicked && <input type="text" onChange={handleChange} placeholder="change here"/>}
106+
</div>
62107
))}
63-
</div>
108+
</ul>
64109
</div>
65110
);
66111
};

0 commit comments

Comments
 (0)