From 9ff988db7ab18e3de7b8027b1679eec61472e806 Mon Sep 17 00:00:00 2001 From: KentaroKumode <23e1273@andrew.ac.jp> Date: Sat, 12 Jul 2025 16:43:37 +0900 Subject: [PATCH] =?UTF-8?q?todo=E3=82=BB=E3=83=83=E3=83=88=E6=A9=9F?= =?UTF-8?q?=E8=83=BD=E8=BF=BD=E5=8A=A0+todo=E5=AE=8C=E4=BA=86=E6=A9=9F?= =?UTF-8?q?=E8=83=BD=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/page.tsx | 87 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 3 deletions(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 79ca5b1..c1baa81 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,8 +1,89 @@ -// src/app/page.tsx +'use client'; // ← 重要!状態管理を使う時は必須 + +import { useState } from 'react'; + +// TypeScriptの型定義 +type Todo = { + id: number; // 一意の識別子 + text: string; // TODOの内容 + completed: boolean; // 完了状態 +}; + export default function Home() { + // useState フックの使い方 + const [todos, setTodos] = useState([]); + const [inputValue, setInputValue] = useState(''); + const addTodo = () => { + // 空の入力をチェック(バリデーション) + if (inputValue.trim() === '') { + alert('TODOを入力してください'); + return; + } + const newTodo: Todo = { + id: Date.now(), // 簡易的なID生成(本番ではuuidを推奨) + text: inputValue, + completed: false + }; + setTodos([...todos, newTodo]); + setInputValue(''); + }; + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); // ページのリロードを防ぐ + addTodo(); + }; + + const toggleTodo = (id: number) => { + // mapを使って新しい配列を作成(イミュータビリティ) + const updatedTodos = todos.map(todo => { + if (todo.id === id) { + // 該当するTODOを見つけたら、completedを反転 + return { ...todo, completed: !todo.completed }; + } + return todo; // それ以外はそのまま + }); + + setTodos(updatedTodos); + }; + return ( -
-

私のTODOアプリ

+
+
+

TODOアプリ

+{/* JSX部分 */} +
+ setInputValue(e.target.value)} + placeholder="TODOを入力してEnterキー" + className="w-full p-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500" + /> + +
+
+ {todos.map((todo) => ( +
+ toggleTodo(todo.id)} + className="mr-3 h-5 w-5 cursor-pointer" + /> + + {todo.text} + +
+ ))} +
+
); } \ No newline at end of file