Skip to content

Commit 60a5147

Browse files
authored
cmd+enter or ctrl+enterでメッセージ送信 (#266)
* cmd+enter or ctrl+enterでメッセージ送信 * onkeydownでsubmit処理を共通化 * biome
1 parent e4b723b commit 60a5147

File tree

1 file changed

+26
-20
lines changed
  • web/src/app/[locale]/(auth)/chat/[id]

1 file changed

+26
-20
lines changed

web/src/app/[locale]/(auth)/chat/[id]/page.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ function MessageInput({ room }: { room: string }) {
3333
const [input, setInput] = useState<string>("");
3434
const [submitting, setSubmitting] = useState<boolean>(false);
3535
const isSendButtonDisabled = submitting || input === "";
36+
37+
const handleSubmit = async (ev: React.FormEvent<HTMLFormElement>) => {
38+
ev.preventDefault();
39+
if (submitting) return;
40+
setSubmitting(true);
41+
setInput("");
42+
await client.chat.rooms[":room"].messages.$post({
43+
header: { Authorization },
44+
param: {
45+
room: room,
46+
},
47+
json: {
48+
content: input,
49+
isPhoto: false,
50+
},
51+
});
52+
setSubmitting(false);
53+
};
54+
3655
return (
3756
<div className="">
38-
<form
39-
className="inline"
40-
onSubmit={async (ev) => {
41-
ev.preventDefault();
42-
if (submitting) return;
43-
setSubmitting(true);
44-
setInput("");
45-
await client.chat.rooms[":room"].messages.$post({
46-
header: { Authorization },
47-
param: {
48-
room: room,
49-
},
50-
json: {
51-
content: input,
52-
isPhoto: false,
53-
},
54-
});
55-
setSubmitting(false);
56-
}}
57-
>
57+
<form className="inline" onSubmit={handleSubmit}>
5858
<div className="fixed bottom-[64px] flex w-full flex-row justify-around gap-2 border-gray-300 border-t bg-white p-4 sm:bottom-0">
5959
<textarea
6060
className="field-sizing-content h-auto max-h-[200px] min-h-[40px] w-full resize-none rounded-xl border border-gray-300 p-2 leading-relaxed focus:outline-none focus:ring-2 focus:ring-blue-500"
@@ -63,6 +63,12 @@ function MessageInput({ room }: { room: string }) {
6363
onChange={(ev) => {
6464
setInput(ev.target.value);
6565
}}
66+
onKeyDown={(ev) => {
67+
if ((ev.ctrlKey || ev.metaKey) && ev.key === "Enter") {
68+
ev.preventDefault();
69+
ev.currentTarget.form?.requestSubmit();
70+
}
71+
}}
6672
/>
6773
<button type="submit" className="" disabled={isSendButtonDisabled}>
6874
<AiOutlineSend size={30} color={isSendButtonDisabled ? "gray" : "#0b8bee"} />

0 commit comments

Comments
 (0)