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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | import React, { useEffect, useMemo } from 'react';
import { useForm, useWatch } from 'react-hook-form';
import { OAuthProvider } from '@uniquegood/realworld-auth-interface';
import useSocialLogin from '@lib/hooks/useSocialLogin';
import styled from 'styled-components';
import Image from 'next/image';
export interface FormFields {
nickName: string;
email: string;
password: string;
passwordConfirm: string;
}
interface SignUpFormProps {
submitFormHandler: (formData: FormFields) => unknown;
}
export default function SignUpForm({ submitFormHandler }: SignUpFormProps) {
const [checkDisable, setCheckDisable] = React.useState<boolean>(false);
const {
register,
handleSubmit,
setValue,
getValues,
watch,
formState: { errors, isValid }
} = useForm<FormFields>({ mode: 'onChange' });
const passwordValue = watch('password');
const passwordLength = useMemo(() => passwordValue && passwordValue.length, [passwordValue]);
const nickNameIsInvalid = errors.nickName?.type === 'pattern' || !!errors.nickName?.message;
const emailIsInvalid = errors.email?.type === 'pattern' || !!errors.email?.message;
const isPasswordInvalid = !!errors.password?.message;
const isPasswordConfirmInvalid = !!errors.passwordConfirm?.message;
function validator(key: 'password' | 'passwordConfirm') {
return (value: string) => {
const passwordConfirm = getValues(key);
const isTyping = passwordConfirm.length > 0;
const minLength = value.length >= 8;
let result;
if (isTyping && value !== passwordConfirm) {
result = '비밀번호가 동일하지 않습니다.';
setCheckDisable(false);
} else if (
(!minLength && value === passwordConfirm) ||
(!minLength && value !== passwordConfirm)
) {
setCheckDisable(false);
} else if (minLength && value === passwordConfirm) {
result = true;
setCheckDisable(true);
}
return result;
};
}
const onSubmit = handleSubmit(async ({ ...rest }) => {
const formData = rest;
submitFormHandler(formData);
});
return (
<Form onSubmit={onSubmit}>
<Entry>
<InputWrap>
<Input
id="nickName"
type="text"
className={`oauth-input form-control ${nickNameIsInvalid && 'valid-error'}`}
placeholder="닉네임을 입력해주세요"
{...register('nickName', {
required: '닉네임을 확인해주세요.',
pattern: {
value: /[ㄱ-ㅎ|ㅏ-ㅣ|가-힣a-zA-Z-_\d]{2,20}/,
message: '2~20자, 한글, 영어, -, _의 사용이 가능합니다.'
},
onChange: (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.value.length > 20) {
setValue('nickName', event.target.value.slice(0, 20));
}
}
})}
autoComplete="off"
/>
</InputWrap>
{errors.nickName && <ValidationSpan>{errors.nickName.message}</ValidationSpan>}
</Entry>
<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: '이메일 주소를 확인해주세요'
}
})}
autoComplete="off"
/>
{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,
validate: validator('passwordConfirm')
})}
autoComplete="new-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>
<Entry>
<InputWrap style={{ position: 'relative' }}>
<Input
id="passwordConfirm"
className={`oauth-input form-control ${isPasswordConfirmInvalid && 'valid-error'}`}
type="password"
placeholder="비밀번호를 확인해주세요"
{...register('passwordConfirm', {
required: '비밀번호가 동일하지 않습니다.',
validate: validator('password'),
maxLength: {
value: 100,
message: '100글자 이하로 입력해주세요.'
}
})}
autoComplete="off"
/>
{checkDisable ? (
<VerifiedIcon
src="/icons/verified.svg"
alt="check icon"
style={{ position: 'absolute', top: '13px', right: '15px' }}
/>
) : (
<VerifiedIcon
src="/icons/verified_solid.svg"
alt="check icon"
style={{ position: 'absolute', top: '13px', right: '15px' }}
/>
)}
</InputWrap>
{(errors.passwordConfirm?.type === 'validate' || errors.password?.type === 'validate') && (
<ValidationSpan>비밀번호가 동일하지 않습니다.</ValidationSpan>
)}
</Entry>
{/* 입력창 모두 완료되고 난 이후에 활성화되는 버튼 */}
<CheckButton type="submit" disabled={!isValid} checkDisable={isValid}>
이메일로 회원가입
</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 VerifiedIcon = styled.img``;
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;
`;
|