Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] 리뷰 작성 페이지에서 동적으로 에러가 생기는 경우, 해당 에러를 읽어주는 기능 #891

Merged
merged 9 commits into from
Oct 22, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CheckboxItem } from '@/components';
import { useModals } from '@/hooks';
import { useMultipleChoice } from '@/pages/ReviewWritingPage/form/hooks';
import { useMultipleChoice, useFocusMessage } from '@/pages/ReviewWritingPage/form/hooks';
import { StrengthUnCheckModal } from '@/pages/ReviewWritingPage/modals/components';
import { ReviewWritingCardQuestion } from '@/types';

Expand Down Expand Up @@ -28,6 +28,8 @@ const MultipleChoiceAnswer = ({ question }: MultipleChoiceAnswerProps) => {
},
);

const { messageRef } = useFocusMessage<HTMLParagraphElement>({ isMessageShown: isOpenLimitGuide });

const handleModalCancelButtonClick = () => {
closeModal(MODAL_KEY.confirm);
};
Expand All @@ -51,7 +53,9 @@ const MultipleChoiceAnswer = ({ question }: MultipleChoiceAnswerProps) => {
))}
<S.LimitGuideMessage>
{isOpenLimitGuide && (
<p data-testid="limitGuideMessage">😅 최대 {question.optionGroup?.maxCount}개까지 선택가능해요</p>
<p data-testid="limitGuideMessage" tabIndex={-1} ref={messageRef} aria-live="assertive">
😅 최대 {question.optionGroup?.maxCount}개까지 선택가능해요
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이모지는 읽어주지 않는게 좋을 것 같아요. 이모지를 이미지로 빼서, alt ="" 로 하는 것 추천합니다.

</p>
)}
</S.LimitGuideMessage>
{isOpen(MODAL_KEY.confirm) && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useTextAnswer } from '@/pages/ReviewWritingPage/form/hooks';
import { useFocusMessage, useTextAnswer } from '@/pages/ReviewWritingPage/form/hooks';
import { ReviewWritingCardQuestion } from '@/types';

import * as S from './styles';
Expand All @@ -12,6 +12,8 @@ const TextAnswer = ({ question }: TextAnswerProps) => {
question,
});

const { messageRef } = useFocusMessage<HTMLParagraphElement>({ isMessageShown: errorMessage !== '' });

const textLength = `${text.length} / ${maxLength}`;

return (
Expand All @@ -24,7 +26,9 @@ const TextAnswer = ({ question }: TextAnswerProps) => {
onBlur={handleTextAnswerBlur}
/>
<S.TextareaInfoContainer>
<S.ReviewTextareaError>{errorMessage}</S.ReviewTextareaError>
<S.ReviewTextareaError tabIndex={-1} ref={messageRef} aria-live="assertive">
{errorMessage}
</S.ReviewTextareaError>
<S.ReviewTextLength>{textLength}</S.ReviewTextLength>
</S.TextareaInfoContainer>
</S.TextareaContainer>
Expand Down
1 change: 1 addition & 0 deletions frontend/src/pages/ReviewWritingPage/form/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as useMutateReview } from './useMutateReview';
export { default as useCurrentCardIndex } from './useCurrentCardIndex';
export { default as useNavigateBlocker } from './useNavigateBlocker';
export { default as useResetFormRecoil } from './useResetFormRecoil';
export { default as useFocusMessage } from './useFocusMessage';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useRef, useEffect } from 'react';

interface useMessageFocusProps {
isMessageShown: boolean;
}

const useFocusMessage = <T extends HTMLElement = HTMLElement>({ isMessageShown }: useMessageFocusProps) => {
const messageRef = useRef<T | null>(null);

useEffect(() => {
if (isMessageShown && messageRef.current) {
messageRef.current.focus();
}
}, [isMessageShown]);

return {
messageRef,
};
};

export default useFocusMessage;