Skip to content

Commit 507dbe2

Browse files
committed
Refactor: Comment 리팩토링
1 parent cce4f8e commit 507dbe2

2 files changed

Lines changed: 23 additions & 18 deletions

File tree

src/components/Comment/index.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
import { StyledText } from '../Text/StyledText';
2-
import { CommentLayout, SendContainer, CommentTextarea, SendImg } from './styles';
3-
import Send from '../../assets/default/send-comment.svg';
1+
import { StyledText } from '@components/Text/StyledText';
2+
import { CommentLayout, SendContainer, CommentTextarea, SendButton } from './styles';
3+
import send from '@assets/default/send-comment.svg';
44
import React, { useEffect, useRef, useState } from 'react';
55
import { CommentProps } from './dto';
66

7-
const Comment: React.FC<CommentProps> = ({ content, sendComment, isModal }) => {
7+
const Comment: React.FC<CommentProps> = ({ content, sendComment, isModal = false }) => {
88
const [comment, setComment] = useState('');
99
const textareaRef = useRef<HTMLTextAreaElement>(null);
1010

11-
// textarea 높이 조정 함수
11+
// textarea 높이 조정
1212
const adjustTextareaHeight = () => {
1313
if (textareaRef.current) {
1414
textareaRef.current.style.height = '1.2rem'; // 초기 높이 설정
1515
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`; // 스크롤 높이에 맞춰 재조정
1616
}
1717
};
1818

19-
useEffect(() => {
20-
adjustTextareaHeight();
21-
}, [comment]); // comment가 변경될 때만 높이 재조정
22-
23-
const handleChangeComment = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
19+
const handleCommentChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
2420
if (e.target.value.length <= 100) {
2521
setComment(e.target.value);
2622
}
2723
};
2824

29-
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
25+
const handleEnterKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
3026
if (comment === '') {
3127
e.preventDefault();
3228
return;
@@ -48,18 +44,25 @@ const Comment: React.FC<CommentProps> = ({ content, sendComment, isModal }) => {
4844
}
4945
};
5046

47+
// comment가 변경될 때만 높이 재조정
48+
useEffect(() => {
49+
adjustTextareaHeight();
50+
}, [comment]);
51+
5152
return (
5253
<CommentLayout $isModal={isModal}>
53-
<StyledText $textTheme={{ style: 'body1-regular', lineHeight: 1.5 }}>{content}</StyledText>
54+
<StyledText $textTheme={{ style: 'body1-regular' }}>{content}</StyledText>
5455
<SendContainer>
5556
<CommentTextarea
5657
ref={textareaRef}
5758
value={comment}
58-
onChange={handleChangeComment}
59-
onKeyDown={handleKeyDown}
59+
onChange={handleCommentChange}
60+
onKeyDown={handleEnterKeyDown}
6061
maxLength={100}
6162
/>
62-
<SendImg src={Send} onClick={handleSendButtonClick} />
63+
<SendButton onClick={handleSendButtonClick}>
64+
<img src={send} alt="메시지 전송 아이콘" />
65+
</SendButton>
6366
</SendContainer>
6467
</CommentLayout>
6568
);

src/components/Comment/styles.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styled from 'styled-components';
22

3-
export const CommentLayout = styled.div<{ $isModal: boolean | undefined }>`
3+
export const CommentLayout = styled.div<{ $isModal: boolean }>`
44
margin: 1.38rem auto 1.25rem auto;
55
width: 100%;
66
display: flex;
@@ -34,9 +34,11 @@ export const CommentTextarea = styled.textarea`
3434
line-height: 1.2rem;
3535
`;
3636

37-
export const SendImg = styled.img`
37+
export const SendButton = styled.button`
3838
width: 2.5rem;
3939
height: 2.5rem;
40-
cursor: pointer;
40+
display: flex;
41+
justify-content: center;
42+
align-items: center;
4143
margin-right: 0.62rem;
4244
`;

0 commit comments

Comments
 (0)