Skip to content

Commit 828bc46

Browse files
authored
fix: handle IME composition for Enter key in ChatInput (#8)
1 parent 441cd19 commit 828bc46

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/app/routes/chat/-components/chat/ChatInput.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { cn } from "@/app/lib/utils";
77
import { getModelById } from "@/server/ai/provider";
88
import type { AspectRatio } from "@/server/ai/types/api";
99
import { Image, Send, SlidersHorizontal, X, ZoomIn } from "lucide-react";
10-
import { type KeyboardEvent, useEffect, useRef, useState } from "react";
10+
import { type KeyboardEvent, useCallback, useEffect, useRef, useState } from "react";
1111
import { useTranslation } from "react-i18next";
1212
import { ImagePreferences } from "./ImagePreferences";
1313

@@ -32,6 +32,7 @@ export function ChatInput({ onSendMessage, disabled, currentProvider, currentMod
3232
const [showPreferences, setShowPreferences] = useState(false);
3333
const fileInputRef = useRef<HTMLInputElement>(null);
3434
const textareaRef = useRef<HTMLTextAreaElement>(null);
35+
const isComposingRef = useRef(false);
3536

3637
// Get current model capability and max images
3738
const { currentModelCapability, maxImages } = (() => {
@@ -97,12 +98,21 @@ export function ChatInput({ onSendMessage, disabled, currentProvider, currentMod
9798
};
9899

99100
const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
100-
if (e.key === "Enter" && !e.shiftKey) {
101+
// Ignore Enter key during IME composition (e.g., Chinese input method)
102+
if (e.key === "Enter" && !e.shiftKey && !isComposingRef.current) {
101103
e.preventDefault();
102104
handleSend();
103105
}
104106
};
105107

108+
const handleCompositionStart = useCallback(() => {
109+
isComposingRef.current = true;
110+
}, []);
111+
112+
const handleCompositionEnd = useCallback(() => {
113+
isComposingRef.current = false;
114+
}, []);
115+
106116
const handleUploadClick = () => {
107117
fileInputRef.current?.click();
108118
};
@@ -237,6 +247,8 @@ export function ChatInput({ onSendMessage, disabled, currentProvider, currentMod
237247
value={message}
238248
onChange={(e) => setMessage(e.target.value)}
239249
onKeyDown={handleKeyDown}
250+
onCompositionStart={handleCompositionStart}
251+
onCompositionEnd={handleCompositionEnd}
240252
placeholder={t("chat.typeMessage")}
241253
disabled={disabled}
242254
className={cn(

0 commit comments

Comments
 (0)