import React, { useState } from "react";
import "./styles.css";
export const App = () => {
const [todoText, setTodoText] = useState("");
const [incompleteTodos, setIncompleteTodos] = useState([
"テスト1",
"テスト2"
]);
const [completeTodos, setCompleteTodos] = useState(["テスト3", "テスト4"]);
//inputのonChangeが変わるたびにsetTodoTextも変えていく
const onChangeTodoText = (event) => {
setTodoText(event.target.value);
};
//追加ボタンを押したときに未完了のTODOリストに追加のTODOをたす
const onClickAdd = () => {
if (todoText === "") return;
const newTodos = [...incompleteTodos, todoText];
setIncompleteTodos(newTodos);
setTodoText("");
};
//削除ボタンが押された時の処理
const onClickDelete = (index) => {
const newTodos = [...incompleteTodos];
newTodos.splice(index, 1);
setIncompleteTodos(newTodos);
};
//完了ボタンが押された時の処理
const onClickComplete = (index) => {
const newIncompleteTodos = [...incompleteTodos];
newIncompleteTodos.splice(index, 1);
setIncompleteTodos(newIncompleteTodos);
const newCompleteTodos = [...completeTodos, incompleteTodos[index]];
setCompleteTodos(newCompleteTodos);
};
//戻すボタンが押された時の処理
const onClickBack = (index) => {
const newCompleteTodos = [...completeTodos];
newCompleteTodos.splice(index, 1);
setCompleteTodos(newCompleteTodos);
const newIncompleteTodos = [...incompleteTodos, completeTodos[index]];
setIncompleteTodos(newIncompleteTodos);
};
return (
<>
<div className="input_area">
<input
placeholder="TODOを入力"
value={todoText}
onChange={onChangeTodoText}
/>
<button onClick={onClickAdd}>追加</button>
</div>
<div className="incomplete_area">
<p>未完了のTODO</p>
<ul>
{incompleteTodos.map((todo, index) => {
return (
<li key={todo} className="list_low">
<p>{todo}</p>
<button onClick={() => onClickComplete(index)}>完了</button>
<button onClick={() => onClickDelete(index)}>削除</button>
</li>
);
})}
</ul>
</div>
<div className="complete_area">
<p>完了のTODO</p>
<ul>
{completeTodos.map((todo, index) => {
return (
<li key={todo} className="list_low">
<p>{todo}</p>
<button onClick={() => onClickBack(index)}>戻す</button>
</li>
);
})}
</ul>
</div>
</>
);
};
.input_area {
background-color: rgb(100, 117, 72);
padding: 20px;
}
input {
padding: 5px;
width: 300px;
}
button {
margin: 0 10px;
}
.incomplete_area {
background-color: rgb(182, 199, 154);
padding: 20px;
}
.complete_area {
background-color: rgb(210, 223, 189);
padding: 20px;
}
.list_low {
display: flex;
align-items: center;
}