diff --git a/src/app/page.tsx b/src/app/page.tsx index b247867..cc8b59d 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -17,6 +17,41 @@ export default function Home() { { id: 3, text: "TODOアプリを作る", completed: false }, ]); + // 入力中のテキストを管理 + const [inputText, setInputText] = useState(""); + + // TODO追加機能 + const handleAddTodo = (e: React.FormEvent) => { + e.preventDefault(); // フォームのデフォルト動作を防ぐ + + if (inputText.trim() === "") { + return; // 空文字の場合は追加しない + } + + const newTodo: Todo = { + id: Date.now(), // 簡易的なID生成 + text: inputText, + completed: false, + }; + + setTodos([...todos, newTodo]); // 新しいTODOを配列に追加 + setInputText(""); // 入力欄をクリア + }; + + // TODO完了状態の切り替え機能 + const handleToggleTodo = (id: number) => { + setTodos( + todos.map((todo) => + todo.id === id ? { ...todo, completed: !todo.completed } : todo + ) + ); + }; + + // TODO削除機能 + const handleDeleteTodo = (id: number) => { + setTodos(todos.filter((todo) => todo.id !== id)); + }; + return (
@@ -26,9 +61,11 @@ export default function Home() { {/* TODO入力フォーム */}
-
+ setInputText(e.target.value)} placeholder="新しいTODOを入力..." className="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-100" /> @@ -51,6 +88,7 @@ export default function Home() { handleToggleTodo(todo.id)} className="w-5 h-5 text-blue-500 rounded focus:ring-2 focus:ring-blue-500" /> {todo.text} -