Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2주차] 송은수 미션 제출합니다. #5

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
71 changes: 71 additions & 0 deletions src/components/AddTodo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React, { useState } from 'react';
import styled from 'styled-components';
import useLocalStorage from '../hooks/useLocalStorage';

let AddTodoLayout = styled.div`
width: 100%;
height: 50px;
padding: 0 1rem;
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.5rem;

Choose a reason for hiding this comment

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

저는 리엑트 기능 구현에 집중하느라 이번엔 절대단위로만 개발했는데
상대단위를 자유롭게 잘 활용하시는 것 같아 인상깊습니다!

`;

let TodoInput = styled.input`
width: 100%;
border: none;
border: 1px solid #e0e0e0;
font-size: 1rem;
flex-grow: 1;
outline: none;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
`;

let AddButton = styled.button`
width: 2rem;
height: 2rem;
font-size: 1.5rem;
border: none;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-weight: 600;
cursor: pointer;
&:hover {
background-color: #f5f5f5;
}
`;

export default function AddTodo() {
const { getTodoFromLocalStorage, setTodoToLocalStorage } = useLocalStorage();

Choose a reason for hiding this comment

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

저는 아직 커스텀 훅을 사용해보지 못했는데,
useLocalStorage() 관리하는걸 커스텀 훅으로 생성해서
하나의 훅에서 로컬 스토리지에 데이터 저장/ 불러오기 기능을 관리할 수 있어서 신기합니다!

const [inputValue, setInputValue] = useState('');
const addButtonHandler = () => {
if (inputValue.trim() === '') {
return;
}
let todoList = getTodoFromLocalStorage('todoList');
todoList.push({ todo: inputValue, isdone: false });
setTodoToLocalStorage('todoList', todoList);
setInputValue('');
};
return (
<AddTodoLayout>
<TodoInput
placeholder="할 일을 입력해주세요..."
value={inputValue}
onChange={(e) => {
setInputValue((prev) => e.target.value);
}}
onKeyPress={(e) => {
if (e.key === 'Enter') {
addButtonHandler();
}
}}
/>
<AddButton>+</AddButton>
</AddTodoLayout>
);
}