Skip to content

Commit 143983c

Browse files
handleSend with Enter (#57)
Co-authored-by: safipatel1 <safipatel1@gmail.com>
1 parent 5aa7554 commit 143983c

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

app/classrooms/[classroomId]/chat/MessageBox.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export default function MessageBox({
104104
value={value}
105105
onChange={(e) => setValue(e.target.value)}
106106
placeholder="Type your message..."
107+
onEnter={handleSend}
107108
/>
108109
<Button
109110
onClick={handleSend}

shared/components/ui/chat/chat-input.tsx

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@ import * as React from "react";
22
import { Textarea } from "@shared/components/ui/textarea";
33
import { cn } from "@shared/lib/utils";
44

5-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
65
interface ChatInputProps
7-
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}
6+
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
7+
//leave as optional
8+
onEnter?: () => void;
9+
}
810

911
const ChatInput = React.forwardRef<HTMLTextAreaElement, ChatInputProps>(
10-
({ className, ...props }, ref) => (
11-
<Textarea
12-
autoComplete="off"
13-
ref={ref}
14-
name="message"
15-
className={cn(
16-
"flex h-16 max-h-12 w-full resize-none items-center rounded-md bg-background px-4 py-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
17-
className
18-
)}
19-
{...props}
20-
/>
21-
)
12+
({ className, onKeyDown, onEnter, ...props }, ref) => {
13+
const handleKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => {
14+
//if it is only the enter key
15+
if (event.key === "Enter" && !event.shiftKey) {
16+
//prevent new line
17+
event.preventDefault();
18+
if (onEnter) {
19+
onEnter();
20+
}
21+
}
22+
if (onKeyDown) {
23+
onKeyDown(event);
24+
}
25+
};
26+
return (
27+
<Textarea
28+
autoComplete="off"
29+
ref={ref}
30+
name="message"
31+
onKeyDown={handleKeyDown}
32+
className={cn(
33+
"flex h-16 max-h-12 w-full resize-none items-center rounded-md bg-background px-4 py-3 text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
34+
className
35+
)}
36+
{...props}
37+
/>
38+
);
39+
}
2240
);
2341
ChatInput.displayName = "ChatInput";
2442

0 commit comments

Comments
 (0)