-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,58 @@ | ||
import './chat.css'; | ||
import { useState, useRef, useEffect } from 'react'; | ||
|
||
export default function Chat() { | ||
// State for the input text and messages | ||
const [text, setText] = useState(''); | ||
const [messages, setMessages] = useState([]); | ||
|
||
// Refs for the container and the last message | ||
const messagesEndRef = useRef(null); | ||
|
||
// Scroll to the bottom whenever messages change | ||
useEffect(() => { | ||
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); | ||
}, [messages]); | ||
|
||
// Handle the click event to add a new message | ||
const handleClick = () => { | ||
if (text.trim()) { | ||
setMessages([...messages, text]); | ||
setText(''); | ||
} | ||
}; | ||
|
||
// Handle the change event for the textarea | ||
const handleChange = (event) => { | ||
setText(event.target.value); | ||
}; | ||
|
||
return ( | ||
<div className='chatui'> | ||
<div className='chatui' id="chatmain"> | ||
<nav> | ||
<header> | ||
<h1>person chatting with</h1> | ||
<h1>Person chatting with</h1> | ||
</header> | ||
</nav> | ||
<main className='chats-display'> | ||
<h1>chats display</h1> | ||
{messages.map((msg, index) => ( | ||
<div key={index} className="container"> | ||
<span>{msg}</span> | ||
</div> | ||
))} | ||
{/* Empty div to scroll to */} | ||
<div ref={messagesEndRef} /> | ||
</main> | ||
<footer className='chatbar'> | ||
<textarea name="negotiate" id="chat" placeholder='enter text'></textarea> | ||
<button>Send</button> | ||
<textarea | ||
name="negotiate" | ||
id="chat" | ||
placeholder='Enter text' | ||
value={text} | ||
onChange={handleChange} | ||
/> | ||
<button onClick={handleClick}>Send</button> | ||
</footer> | ||
</div> | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters