Skip to content

Commit

Permalink
Allow users to enter newline using shift+enter
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolelim02 committed Oct 30, 2024
1 parent bb8c950 commit 6eb8211
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions frontend/src/components/Chat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box, TextField, Typography } from "@mui/material";
import { Box, styled, TextField, Typography } from "@mui/material";
import { useEffect, useRef, useState } from "react";
import { communicationSocket } from "../../utils/communicationSocket";
import { useMatch } from "../../contexts/MatchContext";
Expand Down Expand Up @@ -34,6 +34,13 @@ type ChatProps = {
isActive: boolean;
};

const StyledTypography = styled(Typography)(({ theme }) => ({
padding: theme.spacing(1, 2),
borderRadius: theme.spacing(2),
maxWidth: "80%",
whiteSpace: "pre-line",
}));

const Chat: React.FC<ChatProps> = ({ isActive }) => {
const [messages, setMessages] = useState<Message[]>([]);
const [inputValue, setInputValue] = useState("");
Expand Down Expand Up @@ -143,16 +150,13 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
marginTop: theme.spacing(1),
})}
>
<Typography
<StyledTypography
sx={(theme) => ({
background: theme.palette.primary.main,
padding: theme.spacing(1, 2),
borderRadius: theme.spacing(2),
maxWidth: "80%",
})}
>
{msg.message}
</Typography>
</StyledTypography>
</Box>
) : (
<Box
Expand All @@ -163,16 +167,13 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
marginTop: theme.spacing(1),
})}
>
<Typography
<StyledTypography
sx={(theme) => ({
background: theme.palette.secondary.main,
padding: theme.spacing(1, 2),
borderRadius: theme.spacing(2),
maxWidth: "80%",
})}
>
{msg.message}
</Typography>
</StyledTypography>
</Box>
)
)}
Expand All @@ -186,7 +187,7 @@ const Chat: React.FC<ChatProps> = ({ isActive }) => {
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={(e) => {
const trimmedValue = inputValue.trim();
if (e.key === "Enter" && trimmedValue !== "") {
if (e.key === "Enter" && !e.shiftKey && trimmedValue !== "") {
e.preventDefault();
communicationSocket.emit(CommunicationEvents.SEND_TEXT_MESSAGE, {
roomId: getMatchId(),
Expand Down

0 comments on commit 6eb8211

Please sign in to comment.