Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
import { useState } from 'react';
import Form from './components/Form';
import Item from './components/Item';

function App() {
const [todos, setTodos] = useState([]);
const [deleteComplete, setdeleteComplete] = useState(false);
const addTodo = newTodo => {
const newId = todos.length > 0 ? todos[0].id + 1 : 1;
setTodos([{ id: newId, text: newTodo, isCompleted: false }, ...todos]);
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

신기하게 해결했네요. 이런 방법은 생각도 못했네요 👍

setdeleteComplete(false);
};

const deleteTodo = id => {
setTodos(todos.filter(todo => todo.id !== id));
setdeleteComplete(true);
};

const completeTodo = (id, isCompleted) => {
setTodos(
todos.map(todo => (todo.id === id ? { ...todo, isCompleted } : todo)),
);
};

const deleteAll = () => {
setTodos([]);
setdeleteComplete(true);
};
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가 기능까지 👍

return (
<div className="App">
<h1>Todo List</h1>
<Form />
<ul>
<Item />
</ul>
<Form onAddTodo={addTodo} />
{todos.length === 0 ? (
<p> {deleteComplete ? '할 일이 없습니다.' : '할 일이 없습니다.'} </p>
) : (
<div>
<ul>
{todos.map(todo => (
<Item
key={todo.id}
id={todo.id}
todo={todo.text}
isCompleted={todo.isCompleted}
onDeleteTodo={deleteTodo}
onCompleteTodo={completeTodo}
/>
))}
</ul>
<button type="button" onClick={deleteAll}>
전체 삭제
</button>
</div>
)}
</div>
);
}

export default App;
export default App;
25 changes: 22 additions & 3 deletions src/components/Form.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
form
import { useState } from 'react';
import PropTypes from 'prop-types';

function Form({ onAddTodo }) {
const [toDo, setTodo] = useState('');
const AddToDo = () => {
if (toDo === '') return;
onAddTodo(toDo);
setTodo('');
};
const doList = value => {
const lists = value.target.value;
setTodo(lists);
};
return (
<div>
<input type="text" />
<button type="button">추가</button>
<input
type="text"
value={toDo}
onChange={doList}
placeholder="할 일을 입력해주세요."
/>
<button type="button" onClick={AddToDo}>
추가
</button>
</div>
);
}
Expand All @@ -17,4 +36,4 @@ Form.propTypes = {
onAddTodo: PropTypes.func.isRequired,
};

export default Form;
export default Form;
14 changes: 10 additions & 4 deletions src/components/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import PropTypes from 'prop-types';
function Item({ id, todo, isCompleted, onDeleteTodo, onCompleteTodo }) {
return (
<li>
<input type="checkbox" />
<p>{todo}</p>
<input
type="checkbox"
checked={isCompleted}
onChange={() => onCompleteTodo(id, !isCompleted)}
/>
{todo}
<div>
<button type="button">삭제</button>
<button type="button" onClick={() => onDeleteTodo(id)}>
삭제
</button>
</div>
</li>
);
Expand All @@ -29,4 +35,4 @@ Item.propTypes = {
onCompleteTodo: PropTypes.func.isRequired,
};

export default Item;
export default Item;