diff --git a/src/App.js b/src/App.js index ae5ce50..f57f504 100644 --- a/src/App.js +++ b/src/App.js @@ -1,16 +1,62 @@ +import { useState } from 'react'; +import { v4 as uuidv4 } from 'uuid'; import Form from './components/Form'; import Item from './components/Item'; function App() { + const [todoList, setTodoList] = useState([]); + + const addTodo = newTodo => { + const newTodoForm = { + id: uuidv4(), + todo: newTodo, + isCompleted: false, + }; + const newTodoList = [newTodoForm, ...todoList]; + setTodoList(newTodoList); + }; + + const deleteTodo = id => { + const newTodoList = todoList.filter(todo => { + if (todo.id === id) return false; + return true; + }); + setTodoList(newTodoList); + }; + + const completeTodo = (id, isCompleted) => { + const newTodoList = todoList.map(todo => + todo.id === id ? { ...todo, isCompleted: !isCompleted } : todo, + ); + setTodoList(newTodoList); + }; + return (

Todo List

-
- + + {todoList.length === 0 ? ( +
할 일이 없습니다.
+ ) : ( + + )}
); } export default App; + +// TODO: [] 할 일이 없으면 "할 일 없음"이 화면에 보여집니다. diff --git a/src/components/Form.js b/src/components/Form.js index cb405e1..d9007e1 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -1,10 +1,47 @@ import PropTypes from 'prop-types'; +import { useState } from 'react'; function Form({ onAddTodo }) { + const ENTER = 'Enter'; + const [inputValue, setInputValue] = useState(''); + + const OnAdd = newTodo => { + onAddTodo(newTodo); + setInputValue(''); + }; + + const handleInputValueChange = e => { + setInputValue(e.target.value); + }; + + const handleClickAddButton = () => { + if (inputValue !== '') { + OnAdd(inputValue); + } + }; + + const handleKeyPressEnter = event => { + const debounce = setTimeout(() => { + if (event.key === ENTER) { + event.preventDefault(); + handleClickAddButton(); + } + }, 10); + return () => clearTimeout(debounce); + }; + return (
- - + +
); } diff --git a/src/components/Item.js b/src/components/Item.js index a4bd48e..5fa162c 100644 --- a/src/components/Item.js +++ b/src/components/Item.js @@ -3,10 +3,16 @@ import PropTypes from 'prop-types'; function Item({ id, todo, isCompleted, onDeleteTodo, onCompleteTodo }) { return (
  • - + onCompleteTodo(id, isCompleted)} + type="checkbox" + />

    {todo}

    - +
  • );