-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
86 lines (78 loc) · 2.32 KB
/
App.tsx
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
import React, { useState, useRef } from "react";
import "./styles.css";
type FormElement = React.FormEvent<HTMLFormElement>;
interface ITask {
name: string;
done: boolean;
}
export default function App() {
const [newTask, setNewTask] = useState<string>("");
const [tasks, setTasks] = useState<ITask[]>([]);
const taskInput = useRef<HTMLInputElement>(null);
const addTask = (name: string): void => {
const newTasks = [...tasks, { name, done: false }];
setTasks(newTasks);
};
const handleSubmit = (e: FormElement): void => {
e.preventDefault();
addTask(newTask);
setNewTask("");
taskInput.current?.focus();
};
const toogleDoneTask = (i: number): void => {
const newTasks: ITask[] = [...tasks];
newTasks[i].done = !newTasks[i].done;
setTasks(newTasks);
};
const removeTasks = (i: number) => {
const newTasks: ITask[] = [...tasks];
newTasks.splice(i, 1);
setTasks(newTasks);
};
return (
<section className="container p-4">
<div className="row">
<div className="col-md-6 offset-md-3">
<div className="card-body">
<form onSubmit={handleSubmit}>
<input
type="text"
onChange={(e) => setNewTask(e.target.value)}
value={newTask}
className="form-control"
ref={taskInput}
autoFocus
/>
<button> Save </button>
</form>
</div>
<div>
{tasks.map((t: ITask, i: number) => (
<div className="card card-body mt-2" key={i}>
<h2 style={{ textDecoration: t.done ? "line-thourgh" : "" }}>
{t.name}
</h2>
<div>
<button
className="btn btn-secondary"
onClick={() => toogleDoneTask(i)}
>
{t.done ? "✅" : "❌"}
</button>
<div>
<button
className="btn btn-danger"
onClick={() => removeTasks(i)}
>
🗑
</button>
</div>
</div>
</div>
))}
</div>
</div>
</div>
</section>
);
}