@@ -4,7 +4,7 @@ import { Button } from '@/components/ui/button';
4
4
import { Input } from '@/components/ui/input' ;
5
5
import { Label } from '@/components/ui/label' ;
6
6
import { Textarea } from '@/components/ui/textarea' ;
7
- import { useState } from 'react' ;
7
+ import { useCallback , useEffect , useRef , useState } from 'react' ;
8
8
9
9
interface DiscussionFormProps {
10
10
initialName ?: string ;
@@ -16,26 +16,40 @@ export function DiscussionForm({ initialName = '', onSubmit, onCancel }: Discuss
16
16
const [ name , setName ] = useState ( initialName ) ;
17
17
const [ message , setMessage ] = useState ( '' ) ;
18
18
const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
19
+ const formRef = useRef < HTMLFormElement > ( null ) ;
19
20
20
- const handleSubmit = async ( e : React . FormEvent ) => {
21
- e . preventDefault ( ) ;
21
+ const handleSubmit = useCallback (
22
+ async ( e ?: React . FormEvent ) => {
23
+ if ( e ) {
24
+ e . preventDefault ( ) ;
25
+ }
22
26
23
- if ( ! name . trim ( ) || ! message . trim ( ) ) return ;
27
+ if ( ! name . trim ( ) || ! message . trim ( ) || isSubmitting ) return ;
24
28
25
- setIsSubmitting ( true ) ;
26
- try {
27
- const success = await onSubmit ( name , message ) ;
28
- if ( success ) {
29
- // Clear message but keep name for future submissions
30
- setMessage ( '' ) ;
29
+ setIsSubmitting ( true ) ;
30
+ try {
31
+ const success = await onSubmit ( name , message ) ;
32
+ if ( success ) {
33
+ // Clear message but keep name for future submissions
34
+ setMessage ( '' ) ;
35
+ }
36
+ } finally {
37
+ setIsSubmitting ( false ) ;
31
38
}
32
- } finally {
33
- setIsSubmitting ( false ) ;
39
+ } ,
40
+ [ name , message , onSubmit , isSubmitting ]
41
+ ) ;
42
+
43
+ const handleTextareaKeyDown = ( e : React . KeyboardEvent < HTMLTextAreaElement > ) => {
44
+ // Cmd/Ctrl + Enter to submit the form
45
+ if ( ( e . metaKey || e . ctrlKey ) && e . key === 'Enter' ) {
46
+ e . preventDefault ( ) ;
47
+ handleSubmit ( ) ;
34
48
}
35
49
} ;
36
50
37
51
return (
38
- < form onSubmit = { handleSubmit } className = "space-y-4" >
52
+ < form onSubmit = { handleSubmit } className = "space-y-4" ref = { formRef } >
39
53
< div className = "space-y-2" >
40
54
< Label htmlFor = "name" > Your Name</ Label >
41
55
< Input
@@ -56,6 +70,7 @@ export function DiscussionForm({ initialName = '', onSubmit, onCancel }: Discuss
56
70
placeholder = "Share your thoughts..."
57
71
value = { message }
58
72
onChange = { ( e ) => setMessage ( e . target . value ) }
73
+ onKeyDown = { handleTextareaKeyDown }
59
74
required
60
75
disabled = { isSubmitting }
61
76
className = "w-full min-h-[100px]"
0 commit comments