Skip to content

Commit 6eb8211

Browse files
committed
Allow users to enter newline using shift+enter
1 parent bb8c950 commit 6eb8211

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

frontend/src/components/Chat/index.tsx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Box, TextField, Typography } from "@mui/material";
1+
import { Box, styled, TextField, Typography } from "@mui/material";
22
import { useEffect, useRef, useState } from "react";
33
import { communicationSocket } from "../../utils/communicationSocket";
44
import { useMatch } from "../../contexts/MatchContext";
@@ -34,6 +34,13 @@ type ChatProps = {
3434
isActive: boolean;
3535
};
3636

37+
const StyledTypography = styled(Typography)(({ theme }) => ({
38+
padding: theme.spacing(1, 2),
39+
borderRadius: theme.spacing(2),
40+
maxWidth: "80%",
41+
whiteSpace: "pre-line",
42+
}));
43+
3744
const Chat: React.FC<ChatProps> = ({ isActive }) => {
3845
const [messages, setMessages] = useState<Message[]>([]);
3946
const [inputValue, setInputValue] = useState("");
@@ -143,16 +150,13 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
143150
marginTop: theme.spacing(1),
144151
})}
145152
>
146-
<Typography
153+
<StyledTypography
147154
sx={(theme) => ({
148155
background: theme.palette.primary.main,
149-
padding: theme.spacing(1, 2),
150-
borderRadius: theme.spacing(2),
151-
maxWidth: "80%",
152156
})}
153157
>
154158
{msg.message}
155-
</Typography>
159+
</StyledTypography>
156160
</Box>
157161
) : (
158162
<Box
@@ -163,16 +167,13 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
163167
marginTop: theme.spacing(1),
164168
})}
165169
>
166-
<Typography
170+
<StyledTypography
167171
sx={(theme) => ({
168172
background: theme.palette.secondary.main,
169-
padding: theme.spacing(1, 2),
170-
borderRadius: theme.spacing(2),
171-
maxWidth: "80%",
172173
})}
173174
>
174175
{msg.message}
175-
</Typography>
176+
</StyledTypography>
176177
</Box>
177178
)
178179
)}
@@ -186,7 +187,7 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
186187
onChange={(e) => setInputValue(e.target.value)}
187188
onKeyDown={(e) => {
188189
const trimmedValue = inputValue.trim();
189-
if (e.key === "Enter" && trimmedValue !== "") {
190+
if (e.key === "Enter" && !e.shiftKey && trimmedValue !== "") {
190191
e.preventDefault();
191192
communicationSocket.emit(CommunicationEvents.SEND_TEXT_MESSAGE, {
192193
roomId: getMatchId(),

0 commit comments

Comments
 (0)