@@ -5,32 +5,33 @@ import { CLASSPREFIX as eccgui } from "../../configuration/constants";
55import { IconButton } from "../Icon/IconButton" ;
66import { TextArea , TextAreaProps } from "../TextField/TextArea" ;
77
8- export interface ChatFieldProps extends Pick < TextAreaProps , "className" > , TestableComponent {
8+ export interface ChatFieldProps extends TextAreaProps , TestableComponent {
99 /**
1010 * Default input to start with.
1111 */
1212 children ?: string ;
1313 /**
1414 * Callback handler to process the input of the field when `Enter` is pressed or the submit button is clicked.
15+ * If you use it together with your own handlers for `onChange` and `onKeyDown` it won't work properly.
1516 */
16- onSubmit : ( value : string ) => void ;
17+ onTextSubmit ? : ( value : string ) => void ;
1718}
1819
1920/**
2021 * Component to input chat text.
2122 * Based on `TextArea` component.
2223 */
23- export const ChatField = ( { className, onSubmit , ...otherTextAreaProps } : ChatFieldProps ) => {
24+ export const ChatField = ( { className, onTextSubmit , rightElement , ...otherTextAreaProps } : ChatFieldProps ) => {
2425 const chatvalue = React . useRef < string > ( otherTextAreaProps . children ?? "" ) ;
2526
2627 const onContentChange = ( value : string ) => {
2728 chatvalue . current = value ;
2829 } ;
2930
3031 const onEnter = ( e : React . KeyboardEvent < HTMLTextAreaElement > ) => {
31- if ( e . keyCode === 13 && e . shiftKey === false ) {
32+ if ( e . keyCode === 13 && e . shiftKey === false && onTextSubmit ) {
3233 e . preventDefault ( ) ;
33- onSubmit ( chatvalue . current ) ;
34+ onTextSubmit ( chatvalue . current ) ;
3435 }
3536 } ;
3637
@@ -42,8 +43,17 @@ export const ChatField = ({ className, onSubmit, ...otherTextAreaProps }: ChatFi
4243 onChange = { ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
4344 onContentChange ( e . target . value ) ;
4445 } }
45- onKeyDown = { onEnter }
46- rightElement = { < IconButton name = { "operation-send" } onClick = { ( ) => onSubmit ( chatvalue . current ) } /> }
46+ onKeyDown = { onTextSubmit ? onEnter : undefined }
47+ rightElement = {
48+ ( onTextSubmit || rightElement ) && (
49+ < >
50+ { onTextSubmit && (
51+ < IconButton name = { "operation-send" } onClick = { ( ) => onTextSubmit ( chatvalue . current ) } />
52+ ) }
53+ { rightElement }
54+ </ >
55+ )
56+ }
4757 { ...otherTextAreaProps }
4858 />
4959 ) ;
0 commit comments