Skip to content

Commit d301378

Browse files
committed
Switched HTML input element with HTML textarea element; Adjust ref for messages; Create handler and useEffect for resizing textarea
1 parent d00250a commit d301378

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

frontend/src/pages/Chat/components/InputField.tsx

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import { useCallback, useEffect } from "react";
12
import useMessages, { type IMessage } from "../../../hooks/useMessages";
23

34
interface Props {
45
setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>;
56
isLoading: boolean;
67
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
78
value: string;
8-
inputRef: React.RefObject<HTMLInputElement | null>;
9-
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
9+
inputRef: React.RefObject<HTMLTextAreaElement | null>;
10+
onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
1011
}
1112

1213
export default function InputField({
@@ -26,7 +27,9 @@ export default function InputField({
2627
const userMessageId = Date.now().toString();
2728
const botMessageId = (Date.now() + 1).toString();
2829

29-
onChange({ target: { value: "" } } as React.ChangeEvent<HTMLInputElement>);
30+
onChange({
31+
target: { value: "" },
32+
} as React.ChangeEvent<HTMLTextAreaElement>);
3033
setIsLoading(true);
3134

3235
// Add user message
@@ -60,10 +63,8 @@ export default function InputField({
6063
// Update only the bot's message
6164
setMessages((prev) =>
6265
prev.map((msg) =>
63-
msg.messageId === botMessageId
64-
? { ...msg, content: fullText }
65-
: msg,
66-
),
66+
msg.messageId === botMessageId ? { ...msg, content: fullText } : msg
67+
)
6768
);
6869
}
6970
} catch (error) {
@@ -75,33 +76,46 @@ export default function InputField({
7576
...msg,
7677
content: "Sorry, I encountered an error. Please try again.",
7778
}
78-
: msg,
79-
),
79+
: msg
80+
)
8081
);
8182
} finally {
8283
setIsLoading(false);
8384
}
8485
};
8586

87+
const resizeTextArea = useCallback(() => {
88+
const inputElement = inputRef.current;
89+
if (inputElement !== null) {
90+
inputElement.style.height = "auto";
91+
inputElement.style.height = `${inputElement.scrollHeight}px`;
92+
}
93+
}, [inputRef]);
94+
95+
useEffect(() => {
96+
resizeTextArea();
97+
}, [value, resizeTextArea]);
98+
8699
return (
87-
<div className="flex gap-2 mt-4 h-11 items-stretch mx-auto max-w-[700px]">
88-
<input
89-
type="text"
100+
<div className="flex gap-2 mt-4 justify-center items-center mx-auto max-w-[700px]">
101+
<textarea
90102
value={value}
91103
onChange={onChange}
104+
onInput={resizeTextArea}
92105
onKeyDown={(e) => {
93106
if (e.key === "Enter") {
94107
e.preventDefault();
95108
handleSend();
96109
}
97110
}}
98-
className="w-full p-3 border-1 border-[#ddd] rounded-md box-border transition-colors duration-300 focus:outline-0 focus:border-[#4a90e2] focus:shadow-[0_0_0_2px_rgba(74,144,226,0.2)]"
111+
rows={1}
112+
className="overflow-auto resize-none max-h-22 w-full px-3 py-2 border-1 border-[#ddd] rounded-md box-border transition-colors duration-300 focus:outline-0 focus:border-[#4a90e2] focus:shadow-[0_0_0_2px_rgba(74,144,226,0.2)]"
99113
placeholder="Type your message here..."
100114
disabled={isLoading}
101115
ref={inputRef}
102116
/>
103117
<button
104-
className="px-6 bg-[#1F584F] hover:bg-[#4F8B82] text-white rounded-md cursor-pointer transition-color duration-300"
118+
className="px-6 h-10 bg-[#1F584F] hover:bg-[#4F8B82] text-white rounded-md cursor-pointer transition-color duration-300"
105119
onClick={handleSend}
106120
disabled={isLoading || !value.trim()}
107121
>

frontend/src/pages/Chat/components/MessageWindow.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function MessageWindow({
2525
const [isLoading, setIsLoading] = useState(false);
2626
const [inputValue, setInputValue] = useState("");
2727
const { handleNewSession } = useSession();
28-
const inputRef = useRef<HTMLInputElement | null>(null);
28+
const inputRef = useRef<HTMLTextAreaElement | null>(null);
2929
const messagesRef = useRef<HTMLDivElement | null>(null);
3030

3131
const handleClearSession = () => {
@@ -56,18 +56,18 @@ export default function MessageWindow({
5656

5757
return (
5858
<>
59-
<div className="flex-1">
59+
<div
60+
className={`flex-1 ${
61+
isOngoing ? "overflow-y-scroll" : "overflow-y-none"
62+
}`}
63+
ref={messagesRef}
64+
>
6065
{isError ? (
6166
<div className="flex items-center justify-center h-full text-center">
6267
Error fetching chat history. Try refreshing...
6368
</div>
6469
) : (
65-
<div
66-
className={`max-h-[calc(100dvh-240px)] sm:max-h-[calc(100dvh-20rem)] mx-auto max-w-[700px] ${
67-
isOngoing ? "overflow-y-scroll" : "overflow-y-none"
68-
}`}
69-
ref={messagesRef}
70-
>
70+
<div className="max-h-[calc(100dvh-240px)] sm:max-h-[calc(100dvh-20rem)] mx-auto max-w-[700px]">
7171
{isOngoing ? (
7272
<div className="flex flex-col gap-4">
7373
{messages.map((message) => (

0 commit comments

Comments
 (0)