diff --git a/src/app/page.tsx b/src/app/page.tsx index f92b4f4..f99948c 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -9,6 +9,8 @@ type Todo = { completed: boolean; // 完了したかどうかのフラグ。チェックボックスに対応。 }; +type FilterType = 'all' | 'active' | 'completed'; // フィルターの種類。全て、未完了、完了の3種類。 + export default function Home() { // Todoリスト本体の状態管理 const [todos, setTodos] = useState([]); // Todo配列を保持 @@ -31,7 +33,26 @@ export default function Home() { // 編集中のテキスト const [editText, setEditText] = useState(''); + // フィルター状態の管理(全て、未完了、完了) + const [filter, setFilter] = useState('all'); + // フィルターに応じたTodoの数をカウント + const activeCount = todos.filter(todo => !todo.completed).length; + const completedCount = todos.filter(todo => todo.completed).length; + + + // フィルターを変更する関数 + const getFilteredTodos = () => { // フィルターに応じてTodoリストを返す関数 + switch (filter) { // フィルターの種類に応じて処理を分岐 + case 'active': // 未完了のTodoのみを返す + return todos.filter(todo => !todo.completed); + case 'completed': // 完了したTodoのみを返す + return todos.filter(todo => todo.completed); + default: // 'all'の場合は全てのTodoを返す + return todos; // 'all'の場合は全てのTodoを返す + } + }; + const startEdit = (todo: Todo) => { // 編集開始関数 setEditingId(todo.id); // 編集対象のIDをセット setEditText(todo.text); // 編集用のテキストをセット @@ -142,76 +163,127 @@ export default function Home() { 追加 -
- {todos.map((todo) => ( -
- toggleTodo(todo.id)} - className="mr-3 h-5 w-5 cursor-pointer" - /> +
{/* フィルターと削除ボタンのコンテナ */} +
{/* フィルター用のボタン */} + + + +
+ + {/* 完了済みTODOをすべて削除 */} + {completedCount > 0 && ( + + )} +
- {editingId === todo.id ? ( - // 編集モード +
+ {getFilteredTodos().length === 0 ? ( +

+ {filter === 'active' && 'すべて完了しました!'} + {filter === 'completed' && '完了したTODOがありません'} + {filter === 'all' && 'TODOがありません'} +

+ ) : ( + getFilteredTodos().map((todo) => ( +
setEditText(e.target.value)} - onKeyDown={(e) => { - if (e.key === 'Enter') saveEdit(); - if (e.key === 'Escape') cancelEdit(); - }} - className="flex-1 border border-blue-500 p-2 rounded focus:outline-none focus:ring-2 focus:ring-blue-500" - autoFocus + type="checkbox" + checked={todo.completed} + onChange={() => toggleTodo(todo.id)} + className="mr-3 h-5 w-5 cursor-pointer" /> - ) : ( - // 通常モード - startEdit(todo)} - className={`flex-1 cursor-pointer p-2 rounded hover:bg-gray-200 transition-colors ${ - todo.completed ? 'line-through text-gray-500' : 'text-gray-800' - }`} - > - {todo.text} - - )} - - {editingId === todo.id ? ( - // 編集モードのボタン -
- - -
- ) : ( - // 通常モードのボタン -
- - -
- )} -
- ))} + {todo.text} + + )} + {editingId === todo.id ? ( + // 編集モードのボタン +
+ + +
+ ) : ( + // 通常モードのボタン +
+ + +
+ )} +
+ )) + )}
{/* 確認ダイアログ */}