Skip to content

Commit

Permalink
feat: react-hook-form 사용을 위한 NicknameInput 컴포넌트 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoo9734 committed Dec 20, 2024
1 parent 8d0b4f3 commit c59ceb7
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/components/Input/NicknameInput.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
'use client';

import { Input } from '@nextui-org/react';

import { useEffect, useState } from 'react';
import { useFormContext } from 'react-hook-form';

import useGetValidateNickName from '@/apis/user/validateNickname';
import Icon from '../Icon/Icon';

const NicknameInput = () => {
const [nickname, setNickname] = useState('');
const [validationMessage, setValidationMessage] = useState('');
const [validationMessageColor, setValidationMessageColor] = useState('');
const { register, watch } = useFormContext();

const nickname = watch('nickname');

const { isValid, message, isLoading } = useGetValidateNickName(nickname);

const MAX_NICKNAME_LENGTH = 10;

Expand All @@ -18,20 +23,23 @@ const NicknameInput = () => {
nickname.length > MAX_NICKNAME_LENGTH;

useEffect(() => {
if (nickname === '모멘티아') {
// TODO: 닉네임 상태 확인 (API 호출 로직 추가)
setValidationMessage('중복된 닉네임이 존재합니다.');
setValidationMessageColor('text-system-error');
if (nickname === '') {
setValidationMessage('');
setValidationMessageColor('');
} else if (isNicknameInvalid(nickname)) {
setValidationMessage('사용할 수 없는 문자 또는 길이를 초과했습니다.');
setValidationMessageColor('text-system-error');
} else if (nickname) {
} else if (isLoading) {
setValidationMessage('닉네임 검증 중...');
setValidationMessageColor('text-gray-400');
} else if (!isValid) {
setValidationMessage(message || '중복된 닉네임이 존재합니다.');
setValidationMessageColor('text-system-error');
} else {
setValidationMessage('사용 가능한 닉네임입니다.');
setValidationMessageColor('text-system-success');
} else {
setValidationMessage('');
}
}, [nickname]);
}, [nickname, isLoading, isValid, message]);

const currentNicknameLength = nickname.length;
const nicknameLengthColor =
Expand All @@ -42,18 +50,16 @@ const NicknameInput = () => {
: 'text-white';

return (
<>
<div>
<Input
{...register('nickname', { required: true })}
type='text'
label='닉네임'
labelPlacement='outside'
placeholder='닉네임을 입력해주세요.'
maxLength={MAX_NICKNAME_LENGTH}
value={nickname}
onValueChange={(newNickname) => setNickname(newNickname)}
className='w-78.25'
classNames={{
label: 'custom-label',
label: 'custom-label text-gray-400',
input: 'placeholder:text-gray-700',
inputWrapper: ['bg-gray-900', 'rounded-md'],
}}
Expand Down Expand Up @@ -89,7 +95,7 @@ const NicknameInput = () => {
</p>
</div>
)}
</>
</div>
);
};

Expand Down

0 comments on commit c59ceb7

Please sign in to comment.