Skip to content

Commit c98d4be

Browse files
Prevent message submission on Enter during IME composition (#1908)
1 parent f71bbdf commit c98d4be

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

app/frontend/src/components/QuestionInput/QuestionInput.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { useState, useEffect, useContext } from "react";
22
import { Stack, TextField } from "@fluentui/react";
33
import { Button, Tooltip } from "@fluentui/react-components";
44
import { Send28Filled } from "@fluentui/react-icons";
5-
import { useMsal } from "@azure/msal-react";
65

76
import styles from "./QuestionInput.module.css";
87
import { SpeechInput } from "./SpeechInput";
@@ -21,6 +20,7 @@ interface Props {
2120
export const QuestionInput = ({ onSend, disabled, placeholder, clearOnSend, initQuestion, showSpeechInput }: Props) => {
2221
const [question, setQuestion] = useState<string>("");
2322
const { loggedIn } = useContext(LoginContext);
23+
const [isComposing, setIsComposing] = useState(false);
2424

2525
useEffect(() => {
2626
initQuestion && setQuestion(initQuestion);
@@ -39,12 +39,21 @@ export const QuestionInput = ({ onSend, disabled, placeholder, clearOnSend, init
3939
};
4040

4141
const onEnterPress = (ev: React.KeyboardEvent<Element>) => {
42+
if (isComposing) return;
43+
4244
if (ev.key === "Enter" && !ev.shiftKey) {
4345
ev.preventDefault();
4446
sendQuestion();
4547
}
4648
};
4749

50+
const handleCompositionStart = () => {
51+
setIsComposing(true);
52+
};
53+
const handleCompositionEnd = () => {
54+
setIsComposing(false);
55+
};
56+
4857
const onQuestionChange = (_ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string) => {
4958
if (!newValue) {
5059
setQuestion("");
@@ -72,6 +81,8 @@ export const QuestionInput = ({ onSend, disabled, placeholder, clearOnSend, init
7281
value={question}
7382
onChange={onQuestionChange}
7483
onKeyDown={onEnterPress}
84+
onCompositionStart={handleCompositionStart}
85+
onCompositionEnd={handleCompositionEnd}
7586
/>
7687
<div className={styles.questionInputButtonsContainer}>
7788
<Tooltip content="Submit question" relationship="label">

0 commit comments

Comments
 (0)