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
29 changes: 29 additions & 0 deletions src/hook/useInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useState } from 'react';

type ValidatorFn = (value: string) => string | null;

function useInput(validator: ValidatorFn, initialValue ='') {
const [value, setValue] = useState(initialValue);
const [error, setError] = useState<string | null>(null);
const [touched, setTouched] = useState(false);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

touched는 어떤 경우에 사용이 되는건가요?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

처음 입력할때 입력을 완료하기 전에는 오류 메세지가 나지 않도록 하기위해 만들었습니다.
id 랑 pw를 입력하다가 다른곳을 클릭해 포커스가 나가면, 그때부터 touched 가 true가 되고 오류가 있으면 오류 메세지가 출력됩니다.


const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
if (touched) {
const errorMsg = validator(e.target.value);
setError(errorMsg);
}
};

const onBlur = () => {
setTouched(true);
const errorMsg = validator(value);
setError(errorMsg);
};

const isValid = !error && touched;

return { value, onChange, onBlur, error, isValid };
}

export default useInput;
85 changes: 85 additions & 0 deletions src/page/Login.styled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

import theme from '@/styles/theme';
import styled from '@emotion/styled';

export const MyDiv = styled.div`
max-width: 720px;
width: 100%;
min-height: 100vh;
height: 100%;
background-color: rgb(255, 255, 255);
padding-top: 2.75rem;
`;

export const LoginMain = styled.main`
width: 100%;
height: calc(-2.75rem + 100vh);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
`;

export const KakaoLogo = styled.img`
width: 5.5rem;
color: rgb(42, 48, 56);
`;
export const LoginSection = styled.section`
width: 100%;
max-width: 26.25rem;
padding: 16px;
`;

export const InputSection = styled.input<{ hasError?: boolean }>`
width: 100%;
box-sizing: border-box;
color: rgb(42, 48, 56);
transition: border-color 200ms;
border-style: solid;
min-height: 2.75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.375rem;
padding: 8px 0px;
border-width: 0px 0px 1px;
border-color: ${({ hasError }) => (hasError ? 'red' : 'rgb(220, 222, 227)')};
&:focus {
outline: none;
border-color: ${({ hasError }) => hasError ? 'red' : 'rgb(42, 48, 56)'};
}

`;

export const LoginButton = styled.button<{ notVaild?: boolean }>`
width: 100%;
height: 2.75rem;
font-size: 0.875rem;
font-weight: 400;
line-height: 1.1875rem;
color: rgb(42, 48, 56);
background-color: ${theme.colors.kakaoYellow};
border-radius: 4px;
border: none;
cursor: ${({notVaild}) => (notVaild? 'not-allowed' :'pointer' ) };
opacity: ${({notVaild}) => (notVaild? '0.5' :'1' ) };
transition: background-color 200ms;
`;

export const EmptyDiv16h= styled.div`
width: 100%;
height: 16px;
background-color: transparent;
`;

export const EmptyDiv48h = styled.div`
width: 100%;
height: 48px;
background-color: transparent;
`;

export const ErrorMessage = styled.div`
color: red;
font-size: 0.75rem;
margin-top: 4px;
`;

106 changes: 33 additions & 73 deletions src/page/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,78 +1,19 @@
import styled from '@emotion/styled';
import { useNavigate } from 'react-router-dom';

const MyDiv = styled.div`
max-width: 720px;
width: 100%;
min-height: 100vh;
height: 100%;
background-color: rgb(255, 255, 255);
padding-top: 2.75rem;
`;

const LoginMain = styled.main`
width: 100%;
height: calc(-2.75rem + 100vh);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
`;

const KakaoLogo = styled.img`
width: 5.5rem;
color: rgb(42, 48, 56);
`;
const LoginSection = styled.section`
width: 100%;
max-width: 26.25rem;
padding: 16px;
`;

const InputSection = styled.input`
width: 100%;
box-sizing: border-box;
color: rgb(42, 48, 56);
transition: border-color 200ms;
border-style: solid;
min-height: 2.75rem;
font-size: 1rem;
font-weight: 400;
line-height: 1.375rem;
padding: 8px 0px;
border-width: 0px 0px 1px;
border-color: rgb(220, 222, 227);
`;

const LoginButton = styled.button`
width: 100%;
height: 2.75rem;
font-size: 0.875rem;
font-weight: 400;
line-height: 1.1875rem;
color: rgb(42, 48, 56);
background-color: rgb(254, 229, 0);
border-radius: 4px;
border: none;
cursor: pointer;
transition: background-color 200ms;
`;
import useInput from '@/hook/useInput';
import { validateEmail, validatePassword } from '@/utils/validateInput';
import { useNavigate } from 'react-router-dom';
import { EmptyDiv16h, EmptyDiv48h, ErrorMessage, InputSection, KakaoLogo, LoginButton, LoginMain, LoginSection, MyDiv } from './Login.styled';

const EmptyDiv1 = styled.div`
width: 100%;
height: 16px;
background-color: transparent;
`;

const EmptyDiv2 = styled.div`
width: 100%;
height: 48px;
background-color: transparent;
`;

const Login = () => {

const navigate = useNavigate();
const id = useInput(validateEmail);
const pw = useInput(validatePassword);

const canSubmit = id.isValid && pw.isValid;
const handleLoginClick = () =>{canSubmit && navigate('/')};

return (
<MyDiv>
Expand All @@ -84,14 +25,33 @@ const Login = () => {

<LoginSection>
<div>
<InputSection placeholder="이메일" />
<InputSection
placeholder="이메일"
value={id.value}
onChange={id.onChange}
onBlur={id.onBlur}
hasError={!!id.error}
/>
{id.error && <ErrorMessage>{id.error}</ErrorMessage>}
</div>
<EmptyDiv1 />
<EmptyDiv16h />
<div>
<InputSection type="password" placeholder="비밀번호" />
<InputSection
type="password"
placeholder="비밀번호"
value={pw.value}
onChange={pw.onChange}
onBlur={pw.onBlur}
hasError={!!pw.error}
/>
{pw.error && <ErrorMessage>{pw.error}</ErrorMessage>}
</div>
<EmptyDiv2 />
<LoginButton onClick={() =>navigate('/')}>로그인</LoginButton>
<EmptyDiv48h />
<LoginButton
disabled={!canSubmit}
onClick={handleLoginClick}
notVaild={!canSubmit}
>로그인</LoginButton>
</LoginSection>
</LoginMain>
</MyDiv>
Expand Down
12 changes: 12 additions & 0 deletions src/utils/validateInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const validateEmail = (value: string): string | null => {
if (!value) return 'ID를 입력해주세요.';
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(value)) return 'ID는 이메일 형식으로 입력해주세요.';
return null;
};

export const validatePassword = (value: string): string | null => {
if (!value) return 'PW를 입력해주세요.';
if (value.length < 8) return 'PW는 최소 8글자 이상이어야 합니다.';
return null;
};