From 1b40a3c4566ccc80b6a1928938e24e1aca8a815e Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Sat, 22 Mar 2025 13:15:51 +0000 Subject: [PATCH] feat: implement ChatInput component for enhanced message input and handling in ChatPanel --- components/chat-input.tsx | 71 ++++++++++++++++++++++++++++++++++++++ components/chatPanel.tsx | 28 ++++++--------- components/ui/textarea.tsx | 18 ++++++++++ 3 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 components/chat-input.tsx create mode 100644 components/ui/textarea.tsx diff --git a/components/chat-input.tsx b/components/chat-input.tsx new file mode 100644 index 0000000..f764b6b --- /dev/null +++ b/components/chat-input.tsx @@ -0,0 +1,71 @@ +"use client" + +import React, { useCallback, useRef, useEffect } from "react" +import { Button } from "@/components/ui/button" +import { Textarea } from "@/components/ui/textarea" +import { Loader2, Send } from "lucide-react" + +interface ChatInputProps { + input: string + isLoading: boolean + onSubmit: (e: React.FormEvent) => void + onChange: (e: React.ChangeEvent) => void +} + +export function ChatInput({ input, isLoading, onSubmit, onChange }: ChatInputProps) { + const textareaRef = useRef(null) + + // Auto-resize textarea based on content + const adjustTextareaHeight = useCallback(() => { + const textarea = textareaRef.current + if (textarea) { + textarea.style.height = "auto" + textarea.style.height = `${Math.min(textarea.scrollHeight, 200)}px` + } + }, []) + + useEffect(() => { + adjustTextareaHeight() + }, [input, adjustTextareaHeight]) + + // Handle keyboard shortcuts + const handleKeyDown = (e: React.KeyboardEvent) => { + if ((e.metaKey || e.ctrlKey) && e.key === "Enter") { + e.preventDefault() + const form = e.currentTarget.closest("form") + if (form && input.trim() && !isLoading) { + form.requestSubmit() + } + } + } + + return ( +
+