diff --git a/src/App.js b/src/App.js index ae5ce50..84fc143 100644 --- a/src/App.js +++ b/src/App.js @@ -1,13 +1,48 @@ +import { useState } from 'react'; +import { v4 as uuidv4 } from 'uuid'; import Form from './components/Form'; import Item from './components/Item'; function App() { + const [todos, setTodos] = useState([]); + + const onAddTodo = todo => { + setTodos([ + { + id: uuidv4(), + todo, + isCompleted: false, + }, + ...todos, + ]); + }; + + const onDeleteTodo = id => { + setTodos(todos.filter(todo => todo.id !== id)); + }; + + const onCompleteTodo = (id, isCompleted) => { + setTodos( + todos.map(todo => (todo.id === id ? { ...todo, isCompleted } : todo)), + ); + }; + return (

Todo List

-
+ + {todos.length === 0 &&

할 일이 없습니다.

}
); diff --git a/src/components/Form.js b/src/components/Form.js index cb405e1..26cddc8 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -1,10 +1,32 @@ +import { useState } from 'react'; import PropTypes from 'prop-types'; function Form({ onAddTodo }) { + const [todo, setTodo] = useState(''); + + const handleChange = e => { + setTodo(e.target.value); + }; + + const handleAddTodo = () => { + if (!todo) { + return; + } + onAddTodo(todo); + setTodo(''); + }; + return (
- - + +
); } diff --git a/src/components/Item.js b/src/components/Item.js index a4bd48e..0d4a072 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -1,12 +1,26 @@ import PropTypes from 'prop-types'; function Item({ id, todo, isCompleted, onDeleteTodo, onCompleteTodo }) { + const handleDeleteTodo = () => { + onDeleteTodo(id); + }; + + const handleCompleteTodo = e => { + onCompleteTodo(id, e.target.checked); + }; + return (
  • - +

    {todo}

    - +
  • );