|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import { TestableComponent } from "../../components/interfaces"; |
| 4 | +import { CLASSPREFIX as eccgui } from "../../configuration/constants"; |
| 5 | +import { IconButton } from "../Icon/IconButton"; |
| 6 | +import { TextArea, TextAreaProps } from "../TextField/TextArea"; |
| 7 | + |
| 8 | +export interface ChatFieldProps extends Pick<TextAreaProps, "className">, TestableComponent { |
| 9 | + /** |
| 10 | + * Default input to start with. |
| 11 | + */ |
| 12 | + children?: string; |
| 13 | + /** |
| 14 | + * Callback handler to process the input of the field when `Enter` is pressed or the submit button is clicked. |
| 15 | + */ |
| 16 | + onSubmit: (value: string) => void; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Component to input chat text. |
| 21 | + * Based on `TextArea` component. |
| 22 | + */ |
| 23 | +export const ChatField = ({ className, onSubmit, ...otherTextAreaProps }: ChatFieldProps) => { |
| 24 | + const chatvalue = React.useRef<string>(otherTextAreaProps.children ?? ""); |
| 25 | + |
| 26 | + const onContentChange = (value: string) => { |
| 27 | + chatvalue.current = value; |
| 28 | + }; |
| 29 | + |
| 30 | + const onEnter = (e: React.KeyboardEvent<HTMLTextAreaElement>) => { |
| 31 | + if (e.keyCode === 13 && e.shiftKey === false) { |
| 32 | + e.preventDefault(); |
| 33 | + onSubmit(chatvalue.current); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <TextArea |
| 39 | + fill |
| 40 | + autoResize |
| 41 | + className={`${eccgui}-chat__inputfield` + (className ? ` ${className}` : "")} |
| 42 | + onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 43 | + onContentChange(e.target.value); |
| 44 | + }} |
| 45 | + onKeyDown={onEnter} |
| 46 | + rightElement={<IconButton name={"navigation-forth"} onClick={() => onSubmit(chatvalue.current)} />} |
| 47 | + {...otherTextAreaProps} |
| 48 | + /> |
| 49 | + ); |
| 50 | +}; |
| 51 | + |
| 52 | +export default ChatField; |
0 commit comments