Skip to content

Commit a907b71

Browse files
committed
add ChatField component to enter texts
1 parent a338cac commit a907b71

7 files changed

Lines changed: 81 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010

1111
- `<ChatContent />`
1212
- displays single chat contents in a bubble, including options to add status line and avatar
13+
- `<ChatField />`
14+
- let the user input texts, calls `onSubmit` handler on enter key and submit button
1315

1416
## [24.4.1] - 2025-08-25
1517

src/components/Chat/ChatContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const ChatContent = ({
6565
{statusLine && (
6666
<HtmlContentBlock small>
6767
{statusLine}
68-
<Spacing size="small" />
68+
<Spacing size="tiny" />
6969
</HtmlContentBlock>
7070
)}
7171
{markdownProps && typeof children === "string" ? (

src/components/Chat/ChatField.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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;

src/components/Chat/_chat.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@
3939
}
4040
}
4141
}
42+
43+
.eccgui-chat__inputfield {
44+
min-height: mini-units(9);
45+
max-height: 39vh;
46+
resize: none !important;
47+
}

src/components/Chat/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./ChatContent";
2+
export * from "./ChatField";

src/components/Chat/stories/ChatContent.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const exampleImages = {
1818
};
1919

2020
export default {
21-
title: "Components/Chat",
21+
title: "Components/Chat/ChatContent",
2222
component: ChatContent,
2323
argTypes: {
2424
avatar: {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react";
2+
import { Meta, StoryFn } from "@storybook/react";
3+
4+
import { ChatField } from "../../../index";
5+
6+
export default {
7+
title: "Components/Chat/ChatField",
8+
component: ChatField,
9+
argTypes: {},
10+
} as Meta<typeof ChatField>;
11+
12+
let forceupdate = 0;
13+
const TemplateFull: StoryFn<typeof ChatField> = (args) => <ChatField {...args} key={forceupdate++} />;
14+
15+
export const Default = TemplateFull.bind({});
16+
Default.args = {
17+
onSubmit: (value) => alert(value),
18+
};

0 commit comments

Comments
 (0)