Skip to content

Commit d434ca4

Browse files
committed
open up ChatField for a more complex usages but still supporting a simple mode by onTextSubmit handler
1 parent 4a2d8e9 commit d434ca4

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/components/Chat/ChatField.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,33 @@ import { CLASSPREFIX as eccgui } from "../../configuration/constants";
55
import { IconButton } from "../Icon/IconButton";
66
import { 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
);

src/components/Chat/stories/ChatField.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ const TemplateFull: StoryFn<typeof ChatField> = (args) => <ChatField {...args} k
1414

1515
export const Default = TemplateFull.bind({});
1616
Default.args = {
17-
onSubmit: (value) => alert(value),
17+
onTextSubmit: (value) => alert(value),
1818
};

0 commit comments

Comments
 (0)