-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor: 버튼 활성화 isValid 추가 및 setError 메시지 처리
- Loading branch information
Showing
1 changed file
with
9 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ import Button from '../common/Button/Button'; | |
import { useForm, useController } from 'react-hook-form'; | ||
|
||
const LoginForm = ({ handleLogin, message }) => { | ||
const { control, handleSubmit, formState: { errors } } = useForm({ | ||
const { control, handleSubmit, setError, formState: { isValid, errors } } = useForm({ | ||
mode: 'onChange', | ||
defaultValues: { | ||
email: '[email protected]', | ||
password: 'bc12345', | ||
|
@@ -25,12 +26,13 @@ const LoginForm = ({ handleLogin, message }) => { | |
|
||
const onSubmit = (user) => { | ||
handleLogin(user); | ||
if (message) { | ||
setError('password', { | ||
message: message | ||
}) | ||
} | ||
}; | ||
|
||
const isValid = () => { | ||
return emailController.field.value !== '' && passwordController.field.value !== ''; | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<Input | ||
|
@@ -45,15 +47,15 @@ const LoginForm = ({ handleLogin, message }) => { | |
label="비밀번호" | ||
type="password" | ||
placeHolder="비밀번호를 입력해주세요" | ||
errorMsg={message} | ||
errorMsg={errors.password?.message || message} | ||
required | ||
{...passwordController.field} | ||
/> | ||
<Button | ||
type="submit" | ||
size="L" | ||
text="로그인" | ||
isDisabled={!isValid()} | ||
isDisabled={!isValid} | ||
/> | ||
</form> | ||
); | ||
|