Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | import React, { useMemo } from 'react';
import { useForm } from 'react-hook-form';
import { OAuthProvider } from '@uniquegood/realworld-auth-interface';
import useSocialLogin from '@lib/hooks/useSocialLogin';
import styled from 'styled-components';
export interface FormFields {
email: string;
password: string;
}
interface SignInFormProps {
submitFormHandler: (formData: FormFields) => unknown;
isPendingSubmit: boolean;
}
export default function SignInForm({ submitFormHandler, isPendingSubmit }: SignInFormProps) {
const {
register,
handleSubmit,
watch,
formState: { errors, isValid }
} = useForm<FormFields>({ mode: 'onChange' });
const passwordValue = watch('password');
const passwordLength = useMemo(() => passwordValue && passwordValue.length, [passwordValue]);
const emailIsInvalid = errors.email?.type === 'pattern' || !!errors.email?.message;
const isPasswordInvalid = !!errors.password?.message;
const onSubmit = handleSubmit(async ({ ...rest }) => {
const formData = rest;
submitFormHandler(formData);
});
return (
<Form onSubmit={onSubmit}>
<Entry>
<InputWrap>
<Input
id="email"
type="text"
className={`oauth-input form-control ${emailIsInvalid && 'email-valid-error'}`}
placeholder="이메일을 입력해주세요"
{...register('email', {
required: '이메일 주소를 확인해주세요',
pattern: {
value:
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/,
message: '이메일 주소를 확인해주세요'
}
})}
name="email"
autoComplete="email"
/>
{errors.email && <ValidationSpan>{errors.email?.message}</ValidationSpan>}
</InputWrap>
</Entry>
<Entry>
<InputWrap>
<Input
id="password"
className={`oauth-input form-control ${isPasswordInvalid && 'valid-error'}`}
type="password"
placeholder="비밀번호를 입력해주세요"
{...register('password', {
required: true
})}
name="password"
autoComplete="password"
/>
{errors.password?.type === 'required' && (
<ValidationSpan>비밀번호를 입력해주세요.</ValidationSpan>
)}
{errors.password?.type !== 'required' && passwordLength < 8 && passwordLength > 0 && (
<ValidationSpan>8글자 이상으로 입력해주세요.</ValidationSpan>
)}
{errors.password?.type !== 'required' && passwordLength > 100 && passwordLength > 9 && (
<ValidationSpan>100글자 이하로 입력해주세요.</ValidationSpan>
)}
</InputWrap>
</Entry>
<CheckButton
type="submit"
disabled={!isValid || isPendingSubmit}
checkDisable={isValid && !isPendingSubmit}
>
이메일로 로그인
</CheckButton>
</Form>
);
}
const Form = styled.form`
margin: 0 0 24px 0;
`;
const Entry = styled.div`
margin-bottom: 16px;
`;
const InputWrap = styled.div`
box-sizing: border-box;
.form-control {
display: block;
margin-top: 16px;
margin-bottom: 16px;
padding: 12px 16px;
border: 1px solid #ededed;
border-radius: 4px;
width: 100%;
background: #fafafa;
font-size: 14px;
line-height: 20px;
:focus {
color: #495057;
background-color: #fff;
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.2rem rgb(0 123 255 / 25%);
}
}
.oauth-input {
padding: 10px;
height: 52px;
width: 100%;
}
.valid-error {
border-color: #c869ff !important;
border-width: 2px !important;
outline: none !important;
box-shadow: none !important;
}
.email-valid-error {
border-color: #c869ff !important;
border-width: 2px !important;
outline: none !important;
box-shadow: none !important;
}
`;
const Input = styled.input`
box-sizing: border-box;
display: block;
width: 100%;
height: calc(2.25rem + 2px);
padding: 0.375rem 0.75rem;
font-size: 1rem;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
`;
const ValidationSpan = styled.span`
color: #c869ff;
margin-left: 17px;
font-size: 14px;
`;
const CheckButton = styled.button<{ checkDisable: boolean }>`
background-color: ${(props) => (props.checkDisable ? '#c869ff;' : '#ededed')};
color: ${(props) => (props.checkDisable ? 'white' : '#626262 ')};
cursor: ${(props) => (props.checkDisable ? 'pointer' : 'default')};
display: block;
border: none;
border-radius: 22px;
height: 44px;
width: 100%;
font-size: 14px;
font-weight: 500;
padding: 11px 0;
text-decoration: none !important;
text-align: center;
`;
|