Skip to content

Commit f061174

Browse files
committed
Change chatroom input field to be controlled component and update local state. Change chatRoom.tsx to ChatRoom.tsx
1 parent 34f4604 commit f061174

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

app/src/components/bottom/BottomTabs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import CustomizationPanel from '../../containers/CustomizationPanel';
88
import CreationPanel from './CreationPanel';
99
import ContextManager from '../ContextAPIManager/ContextManager';
1010
import StateManager from '../StateManagement/StateManagement';
11-
import Chatroom from './chatRoom';
11+
import Chatroom from './ChatRoom';
1212
import Box from '@mui/material/Box';
1313
import Tree from '../../tree/TreeChart';
1414
import FormControl from '@mui/material/FormControl';

app/src/components/bottom/chatRoom.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import React from 'react';
2+
import { useState } from 'react';
23
import { useSelector } from 'react-redux';
34
import { RootState } from '../../redux/store';
45
import { emitEvent } from '../../helperFunctions/socket';
56

67
const Chatroom = (props): JSX.Element => {
78
const userName = useSelector((store: RootState) => store.roomSlice.userName);
89
const roomCode = useSelector((store: RootState) => store.roomSlice.roomCode);
9-
1010
const messages = useSelector((store: RootState) => store.roomSlice.messages);
1111

12+
const [inputContent, setInputContent] = useState('');
13+
1214
const wrapperStyles = {
1315
border: `2px solid #f2fbf8`,
1416
borderRadius: '8px',
@@ -49,13 +51,12 @@ const Chatroom = (props): JSX.Element => {
4951

5052
const handleSubmit = (e) => {
5153
e.preventDefault();
52-
const messageInput = document.getElementById(
53-
'message-input'
54-
) as HTMLInputElement;
55-
const message = messageInput.value.trim();
56-
if (message !== '') {
57-
emitEvent('send-chat-message', roomCode, { userName, message });
58-
messageInput.value = '';
54+
if (inputContent !== '') {
55+
emitEvent('send-chat-message', roomCode, {
56+
userName,
57+
message: inputContent
58+
});
59+
setInputContent('');
5960
}
6061
};
6162

@@ -107,7 +108,13 @@ const Chatroom = (props): JSX.Element => {
107108
style={inputContainerStyles}
108109
onSubmit={handleSubmit}
109110
>
110-
<input type="text" id="message-input" style={inputStyles} />
111+
<input
112+
type="text"
113+
id="message-input"
114+
onChange={(e) => setInputContent(e.target.value)}
115+
value={inputContent}
116+
style={inputStyles}
117+
/>
111118
<button type="submit" id="send-button" style={buttonStyles}>
112119
Send
113120
</button>

0 commit comments

Comments
 (0)