Skip to content

Commit

Permalink
Merge pull request #248 from SWM-FIRE/FIRE-736-채팅-input-text-area-적용하기
Browse files Browse the repository at this point in the history
FIRE-736 fix input to textarea
  • Loading branch information
haryung-lee authored Oct 10, 2022
2 parents be632ab + 4edafc8 commit 9f15d58
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions src/components/atoms/chatting/SendChat.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useRef } from 'react';
import styled from 'styled-components';
import { ReactComponent as MessageSend } from '../../../assets/svg/MessageSend.svg';
import roomSocket from '../../../adapters/roomSocket';
Expand All @@ -13,34 +13,58 @@ export default function SendChat({
}) {
const [newMessage, setNewMessage] = useState('');
const newSocket = roomId === 'lobby' ? lobbySocket.socket : roomSocket.socket;
const inputRef = useRef(null);

const onMessageChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const onMessageChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
event.preventDefault();
setNewMessage(() => event.target.value);
autoGrow();
};

const autoGrow = () => {
if (inputRef.current) {
inputRef.current.style.height = '2rem';
inputRef.current.style.height = inputRef.current.scrollHeight
.toString()
.concat('px');
}
};

const onSubmit = (
event: React.FormEvent<HTMLFormElement> | React.KeyboardEvent,
event: React.FormEvent<HTMLFormElement> | KeyboardEvent,
) => {
event.preventDefault();
// console.log('new 메세지 : ', newMessage);
if (newMessage.trim() === '') return;
newSocket.emit('chatMessage', {
room: roomId,
sender: uid,
message: newMessage,
createdAt: new Date(),
});

setNewMessage('');
if (inputRef.current) {
inputRef.current.style.height = '2rem';
}
};

const onKeyDown = (event) => {
if (event.key === 'Enter') {
event.preventDefault();
onSubmit(event);
}
};

return (
<SubmitMessage onSubmit={onSubmit} roomId={roomId}>
<Input
placeholder="Write your message...."
value={newMessage}
onChange={onMessageChange}
roomId={roomId}
ref={inputRef}
rows={1}
onChange={onMessageChange}
onKeyDown={onKeyDown}
/>
<Button>
<MessageSend />
Expand All @@ -60,13 +84,18 @@ const SubmitMessage = styled.form<{ roomId: string }>`
z-index: 999;
`;

const Input = styled.input<{ roomId: string }>`
const Input = styled.textarea<{ roomId: string }>`
width: calc(100% - 2.7rem);
font-size: 1.3rem;
background-color: ${(props) =>
props.roomId !== 'lobby' ? ({ theme }) => theme.input : '#313540'};
font-family: IBMPlexSansKRRegular;
color: rgba(255, 255, 255, 1);
min-height: 2rem;
max-height: 13rem;
::-webkit-scrollbar {
width: 0.3rem;
}
&:focus {
outline: none;
Expand Down

0 comments on commit 9f15d58

Please sign in to comment.