From 4a6f8c11ec722bf00f59d7b2f71f98bf2321cd24 Mon Sep 17 00:00:00 2001 From: imhwarang Date: Tue, 6 Aug 2024 00:06:17 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20react-hook-form=20=EB=8F=84?= =?UTF-8?q?=EC=9E=85=20=EB=B0=8F=20hostApplyPage=EA=B4=80=EB=A0=A8=20step?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/apis/domains/host/usePostHostApply.ts | 10 +- .../CategorySelectBox/CategorySelectBox.tsx | 2 +- src/components/common/TextArea/TextArea.tsx | 85 +++---- src/components/common/inputs/Input/Input.tsx | 6 +- .../host/components/StepOne/StepOne.style.ts | 1 + src/pages/host/components/StepOne/StepOne.tsx | 140 +++++++---- src/pages/host/components/StepTwo/StepTwo.tsx | 231 +++++++++++------- yarn.lock | 5 + 9 files changed, 293 insertions(+), 188 deletions(-) diff --git a/package.json b/package.json index 4ac8c82d..1a130929 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "react": "^18.3.1", "react-datepicker": "^7.2.0", "react-dom": "^18.3.1", + "react-hook-form": "^7.52.1", "react-router-dom": "^6.24.0", "react-spinners": "^0.14.1", "swiper": "^11.1.4" diff --git a/src/apis/domains/host/usePostHostApply.ts b/src/apis/domains/host/usePostHostApply.ts index 42428e53..a98e5c44 100644 --- a/src/apis/domains/host/usePostHostApply.ts +++ b/src/apis/domains/host/usePostHostApply.ts @@ -4,7 +4,7 @@ import { useEasyNavigate } from '@hooks'; import { components } from '@schema'; import { useQueryClient, useMutation } from '@tanstack/react-query'; import { ErrorResponse, ErrorType, MutateResponseType } from '@types'; -import { Dispatch, RefObject, SetStateAction } from 'react'; +import { Dispatch, SetStateAction } from 'react'; type HostApplyRequest = components['schemas']['SubmitterCreateRequest']; @@ -21,10 +21,10 @@ const postHostApply = async (hostApplyState: HostApplyRequest): Promise void, + // resetHostApplyState: () => void, onNext: () => void, setIsNicknameDuplicate: Dispatch>, - nicknameRef: RefObject + // nicknameRef: RefObject ) => { const queryClient = useQueryClient(); const { goGuestMyPage } = useEasyNavigate(); @@ -33,13 +33,13 @@ export const usePostHostApply = ( mutationFn: (hostApplyState: HostApplyRequest) => postHostApply(hostApplyState), onSuccess: () => { queryClient.invalidateQueries({ queryKey: [QUERY_KEY.HOST_APPLY] }); - resetHostApplyState(); + // resetHostApplyState(); onNext(); }, onError: (error: ErrorType) => { if (error.status === 40008) { setIsNicknameDuplicate(true); - nicknameRef.current?.focus(); + // nicknameRef.current?.focus(); } else { alert(error.message); goGuestMyPage(); diff --git a/src/components/common/CategorySelectBox/CategorySelectBox.tsx b/src/components/common/CategorySelectBox/CategorySelectBox.tsx index 9ab2f80d..81a604a2 100644 --- a/src/components/common/CategorySelectBox/CategorySelectBox.tsx +++ b/src/components/common/CategorySelectBox/CategorySelectBox.tsx @@ -71,7 +71,7 @@ interface Category { } interface CategorySelectBoxProps { - selectedCategories: Category; + selectedCategories: Category | undefined; onUpdateCategories: (newCategories: Category) => void; } diff --git a/src/components/common/TextArea/TextArea.tsx b/src/components/common/TextArea/TextArea.tsx index f3614b62..01f01b67 100644 --- a/src/components/common/TextArea/TextArea.tsx +++ b/src/components/common/TextArea/TextArea.tsx @@ -1,4 +1,4 @@ -import { TextareaHTMLAttributes, useState } from 'react'; +import { forwardRef, TextareaHTMLAttributes, useState } from 'react'; import { textAreaStyle, @@ -17,54 +17,49 @@ export interface TextAreaProps extends TextareaHTMLAttributes { - const [maxLengthError, setMaxLengthError] = useState(false); - const [isFocused, setIsFocused] = useState(false); - // 글자 수 세서 바로 화면에 반영하는 onChange 함수 (default) - const handleTextAreaChange = (e: React.ChangeEvent) => { - if (e.target.value.length <= maxLength) { - onChange(e); - setMaxLengthError(false); - } else { - setMaxLengthError(true); - } - }; +const TextArea = forwardRef( + ({ value, onChange, isValid, size = 'small', placeholder, errorMessage, maxLength }, ref) => { + const [maxLengthError, setMaxLengthError] = useState(false); + const [isFocused, setIsFocused] = useState(false); + // 글자 수 세서 바로 화면에 반영하는 onChange 함수 (default) + const handleTextAreaChange = (e: React.ChangeEvent) => { + if (e.target.value.length <= maxLength) { + onChange(e); + setMaxLengthError(false); + } else { + setMaxLengthError(true); + } + }; - // TODO: constants 파일에 분리하기 - // 글자 수 에러 메시지 - const textLengthErrorMessage = `* 글자 수 ${maxLength}자 이하로 입력해주세요.`; + // TODO: constants 파일에 분리하기 + // 글자 수 에러 메시지 + const textLengthErrorMessage = `* 글자 수 ${maxLength}자 이하로 입력해주세요.`; - const isError = maxLengthError || !isValid; + const isError = maxLengthError || !isValid; - return ( -
-
-