@@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
3
3
import { updateRefHeight } from '../../utils'
4
4
import { useTranslation } from 'react-i18next'
5
5
6
- export function InputBox ( { onSubmit, enabled } ) {
6
+ export function InputBox ( { onSubmit, enabled, port } ) {
7
7
const { t } = useTranslation ( )
8
8
const [ value , setValue ] = useState ( '' )
9
9
const inputRef = useRef ( null )
@@ -16,37 +16,64 @@ export function InputBox({ onSubmit, enabled }) {
16
16
if ( enabled ) inputRef . current . focus ( )
17
17
} , [ enabled ] )
18
18
19
- const onKeyDown = ( e ) => {
19
+ const handleKeyDownOrClick = ( e ) => {
20
20
e . stopPropagation ( )
21
- if ( e . keyCode === 13 && e . shiftKey === false ) {
22
- e . preventDefault ( )
23
- if ( ! value ) return
24
- onSubmit ( value )
25
- setValue ( '' )
21
+ if ( e . type === 'click' || ( e . keyCode === 13 && e . shiftKey === false ) ) {
22
+ if ( enabled ) {
23
+ e . preventDefault ( )
24
+ if ( ! value ) return
25
+ onSubmit ( value )
26
+ setValue ( '' )
27
+ } else {
28
+ e . preventDefault ( )
29
+ port . postMessage ( { stop : true } )
30
+ }
26
31
}
27
32
}
28
33
29
34
return (
30
- < textarea
31
- dir = "auto"
32
- ref = { inputRef }
33
- disabled = { ! enabled }
34
- className = "interact-input"
35
- placeholder = {
36
- enabled
37
- ? t ( 'Type your question here\nEnter to send, shift + enter to break line' )
38
- : t ( 'Wait for the answer to finish and then continue here' )
39
- }
40
- value = { value }
41
- onChange = { ( e ) => setValue ( e . target . value ) }
42
- onKeyDown = { onKeyDown }
43
- />
35
+ < div style = { { position : 'relative' } } >
36
+ < textarea
37
+ dir = "auto"
38
+ ref = { inputRef }
39
+ disabled = { false }
40
+ className = "interact-input"
41
+ placeholder = {
42
+ enabled
43
+ ? t ( 'Type your question here\nEnter to send\nShift + enter to break line' )
44
+ : t ( 'Type your question here\nEnter to stop generating\nShift + enter to break line' )
45
+ }
46
+ value = { value }
47
+ onChange = { ( e ) => setValue ( e . target . value ) }
48
+ onKeyDown = { handleKeyDownOrClick }
49
+ />
50
+ < button
51
+ style = { {
52
+ position : 'absolute' ,
53
+ top : '50%' ,
54
+ right : '8px' ,
55
+ transform : 'translateY(-50%)' ,
56
+ backgroundColor : enabled ? '#30a14e' : '#cf222e' ,
57
+ color : 'white' ,
58
+ border : '1px solid' ,
59
+ borderRadius : '6px' ,
60
+ borderColor : 'rgba(31,35,40,0.15)' ,
61
+ padding : '0.2em 0.8em' ,
62
+ fontSize : '1.2em' ,
63
+ boxShadow : '0 1px 0 rgba(31,35,40,0.1)' ,
64
+ } }
65
+ onClick = { handleKeyDownOrClick }
66
+ >
67
+ { enabled ? t ( 'Send' ) : t ( 'Stop' ) }
68
+ </ button >
69
+ </ div >
44
70
)
45
71
}
46
72
47
73
InputBox . propTypes = {
48
74
onSubmit : PropTypes . func . isRequired ,
49
75
enabled : PropTypes . bool ,
76
+ port : PropTypes . func . isRequired ,
50
77
}
51
78
52
79
export default InputBox
0 commit comments