diff --git a/src/App.tsx b/src/App.tsx index a522ca4..661e0df 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,27 +1,23 @@ import { Container } from '@chakra-ui/react'; import React, { useState } from 'react'; -import TodoInput from './conponents/TodoInput'; -import TodoList from './conponents/TodoList'; - -type TodoType = { - title: string; -}; +import TodoInput from '@/conponents/TodoInput'; +import TodoList from '@/conponents/TodoList'; +import { TodoType } from '@/types/TodoType'; const initialTodos: TodoType[] = [ - { title: '資料を作る' }, - { title: '歯医者に行く' }, - { title: '課題をやる' }, + { id: '0', title: '資料を作る', isCompleted: false }, + { id: '1', title: '歯医者に行く', isCompleted: false }, + { id: '2', title: '課題をやる', isCompleted: false }, ]; const App = () => { const [todos, setTodos] = useState(initialTodos); - const [status, setStatus] = useState([false, false, false]); return ( - - + + ); }; diff --git a/src/conponents/Todo.tsx b/src/conponents/Todo.tsx index d781ad1..60e6740 100644 --- a/src/conponents/Todo.tsx +++ b/src/conponents/Todo.tsx @@ -2,36 +2,32 @@ import { CloseIcon } from '@chakra-ui/icons'; import { Flex, Spacer, Editable, EditablePreview, EditableInput, Checkbox } from '@chakra-ui/react'; import React, { Dispatch, SetStateAction } from 'react'; -type TodoType = { - title: string; -}; +import { TodoType } from '@/types/TodoType'; type Props = { index: number; title: string; todos: TodoType[]; setTodos: Dispatch>; - status: boolean[]; - setStatus: Dispatch>; }; -const Todo = ({ index, title, todos, setTodos, status, setStatus }: Props) => { +const Todo = ({ index, title, todos, setTodos }: Props) => { const removeTodo = () => { const newTodos = todos.filter((_, i) => i !== index); setTodos(newTodos); }; const handleStatus = () => { - const newStatus = [...status]; - newStatus[index] = !newStatus[index]; - setStatus(newStatus); + const newStatus = [...todos]; + newStatus[index].isCompleted = !newStatus[index].isCompleted; + setTodos(newStatus); }; return ( - handleStatus()} /> + { handleStatus()} />} - {status[index] ? : } + {todos[index].isCompleted ? : } diff --git a/src/conponents/TodoInput.tsx b/src/conponents/TodoInput.tsx index 98ad746..6ebeda9 100644 --- a/src/conponents/TodoInput.tsx +++ b/src/conponents/TodoInput.tsx @@ -1,18 +1,16 @@ import { Input } from '@chakra-ui/react'; import React, { useState, ChangeEvent, Dispatch, SetStateAction, KeyboardEvent } from 'react'; -type TodoType = { - title: string; -}; +import { TodoType } from '@/types/TodoType'; type Props = { todos: TodoType[]; setTodos: Dispatch>; - status: boolean[]; - setStatus: Dispatch>; }; -const TodoInput = ({ todos, setTodos, status, setStatus }: Props) => { +let num = 3; + +const TodoInput = ({ todos, setTodos }: Props) => { const [title, setTitle] = useState(''); return ( @@ -24,10 +22,9 @@ const TodoInput = ({ todos, setTodos, status, setStatus }: Props) => { onChange={(e: ChangeEvent) => setTitle(e.target.value)} onKeyPress={(e: KeyboardEvent) => { if (e.key === 'Enter') { - const newTodos = [...todos, { title: title }]; - const newStatus = [...status, false]; + const newTodos = [...todos, { id: String(num), title: title, isCompleted: false }]; setTodos(newTodos); - setStatus(newStatus); + num++; } }} /> diff --git a/src/conponents/TodoList.tsx b/src/conponents/TodoList.tsx index 0f2559b..4e85619 100644 --- a/src/conponents/TodoList.tsx +++ b/src/conponents/TodoList.tsx @@ -3,30 +3,18 @@ import React, { Dispatch, SetStateAction } from 'react'; import Todo from './Todo'; -type TodoType = { - title: string; -}; +import { TodoType } from '@/types/TodoType'; type Props = { todos: TodoType[]; setTodos: Dispatch>; - status: boolean[]; - setStatus: Dispatch>; }; -const TodoList = ({ todos, setTodos, status, setStatus }: Props) => { +const TodoList = ({ todos, setTodos }: Props) => { return ( } spacing={4}> {todos.map((todo, i) => ( - + ))} ); diff --git a/src/types/TodoType.ts b/src/types/TodoType.ts new file mode 100644 index 0000000..a86b06b --- /dev/null +++ b/src/types/TodoType.ts @@ -0,0 +1,5 @@ +export type TodoType = { + id: string; + title: string; + isCompleted: boolean; +};