Skip to content

Commit b7e2c85

Browse files
ChatInput: add a handler for when user send a message (#63)
ChatInput: add a handler for when user send a message J=CLIP-573 TEST=auto,manual verified that if provided, onSend is executed after the user sends a message
1 parent 51f9a9f commit b7e2c85

File tree

7 files changed

+35
-6
lines changed

7 files changed

+35
-6
lines changed

docs/chat-ui-react.chatinput.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ A component that allows user to input message and send to Chat API.
99
**Signature:**
1010

1111
```typescript
12-
export declare function ChatInput({ placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, }: ChatInputProps): React.JSX.Element;
12+
export declare function ChatInput({ placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, onSend, }: ChatInputProps): React.JSX.Element;
1313
```
1414

1515
## Parameters
1616

1717
| Parameter | Type | Description |
1818
| --- | --- | --- |
19-
| { placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, } | [ChatInputProps](./chat-ui-react.chatinputprops.md) | |
19+
| { placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, onSend, } | [ChatInputProps](./chat-ui-react.chatinputprops.md) | |
2020

2121
**Returns:**
2222

docs/chat-ui-react.chatinputprops.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface ChatInputProps
1919
| [customCssClasses?](./chat-ui-react.chatinputprops.customcssclasses.md) | | [ChatInputCssClasses](./chat-ui-react.chatinputcssclasses.md) | _(Optional)_ CSS classes for customizing the component styling. |
2020
| [handleError?](./chat-ui-react.chatinputprops.handleerror.md) | | (e: unknown) => void | _(Optional)_ A function which is called when an error occurs from Chat API while processing the user's message. By default, the error is logged to the console and an error message is added to state. |
2121
| [inputAutoFocus?](./chat-ui-react.chatinputprops.inputautofocus.md) | | boolean | _(Optional)_ Enable auto focus for the input box. Defaults to false. |
22+
| [onSend?](./chat-ui-react.chatinputprops.onsend.md) | | (message: string) => void | _(Optional)_ A callback which is called when user sends a message. |
2223
| [placeholder?](./chat-ui-react.chatinputprops.placeholder.md) | | string | _(Optional)_ The input's placeholder text when no text has been entered by the user. Defaults to "Type a message...". |
2324
| [sendButtonIcon?](./chat-ui-react.chatinputprops.sendbuttonicon.md) | | JSX.Element | _(Optional)_ Custom icon for the send button. |
2425
| [stream?](./chat-ui-react.chatinputprops.stream.md) | | boolean | _(Optional)_ Enable streaming behavior by making a request to Chat Streaming API. This feature is experimental, and is subject to change. Defaults to false. |
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@yext/chat-ui-react](./chat-ui-react.md) &gt; [ChatInputProps](./chat-ui-react.chatinputprops.md) &gt; [onSend](./chat-ui-react.chatinputprops.onsend.md)
4+
5+
## ChatInputProps.onSend property
6+
7+
A callback which is called when user sends a message.
8+
9+
**Signature:**
10+
11+
```typescript
12+
onSend?: (message: string) => void;
13+
```

docs/chat-ui-react.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| Function | Description |
1010
| --- | --- |
1111
| [ChatHeader({ title, showRestartButton, restartButtonIcon, showCloseButton, closeButtonIcon, onClose, customCssClasses, })](./chat-ui-react.chatheader.md) | A component that renders the header of a chat bot panel, including the title and a button to reset the conversation. |
12-
| [ChatInput({ placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, })](./chat-ui-react.chatinput.md) | A component that allows user to input message and send to Chat API. |
12+
| [ChatInput({ placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, onSend, })](./chat-ui-react.chatinput.md) | A component that allows user to input message and send to Chat API. |
1313
| [ChatPanel(props)](./chat-ui-react.chatpanel.md) | A component that renders a full panel for chat bot interactions. This includes the message bubbles for the conversation, input box with send button, and header (if provided). |
1414
| [ChatPopUp(props)](./chat-ui-react.chatpopup.md) | A component that renders a popup button that displays and hides a panel for chat bot interactions. |
1515
| [MessageBubble({ message, showFeedbackButtons, showTimestamp, customCssClasses, formatTimestamp, })](./chat-ui-react.messagebubble.md) | A component that displays the provided message. |

etc/chat-ui-react.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface ChatHeaderProps {
4040
}
4141

4242
// @public
43-
export function ChatInput({ placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, }: ChatInputProps): React_2.JSX.Element;
43+
export function ChatInput({ placeholder, stream, inputAutoFocus, handleError, sendButtonIcon, customCssClasses, onSend, }: ChatInputProps): React_2.JSX.Element;
4444

4545
// @public
4646
export interface ChatInputCssClasses {
@@ -57,6 +57,7 @@ export interface ChatInputProps {
5757
customCssClasses?: ChatInputCssClasses;
5858
handleError?: (e: unknown) => void;
5959
inputAutoFocus?: boolean;
60+
onSend?: (message: string) => void;
6061
placeholder?: string;
6162
sendButtonIcon?: JSX.Element;
6263
stream?: boolean;

src/components/ChatInput.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export interface ChatInputProps {
5656
sendButtonIcon?: JSX.Element;
5757
/** CSS classes for customizing the component styling. */
5858
customCssClasses?: ChatInputCssClasses;
59+
/** A callback which is called when user sends a message. */
60+
onSend?: (message: string) => void;
5961
}
6062

6163
/**
@@ -76,6 +78,7 @@ export function ChatInput({
7678
handleError,
7779
sendButtonIcon = <ArrowIcon />,
7880
customCssClasses,
81+
onSend,
7982
}: ChatInputProps) {
8083
const chat = useChatActions();
8184
const [input, setInput] = useState("");
@@ -91,8 +94,10 @@ export function ChatInput({
9194
? chat.streamNextMessage(input)
9295
: chat.getNextMessage(input);
9396
setInput("");
94-
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
95-
}, [chat, input, handleError, defaultHandleApiError, stream]);
97+
res.then(() => {
98+
onSend?.(input)
99+
}).catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
100+
}, [chat, input, handleError, defaultHandleApiError, stream, onSend]);
96101

97102
const handleKeyDown = useCallback(
98103
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {

tests/components/ChatInput.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,12 @@ it("executes custom handleError if provided", async () => {
205205
expect(customHandleError).toBeCalledTimes(1);
206206
expect(customHandleError).toBeCalledWith("API Error");
207207
});
208+
209+
it("executes onSend if provided", async () => {
210+
const onSendCb = jest.fn();
211+
render(<ChatInput onSend={message => onSendCb(message)} />);
212+
await act(() => userEvent.type(screen.getByRole("textbox"), "test"));
213+
const sendButton = screen.getByRole("button");
214+
await act(() => userEvent.click(sendButton));
215+
expect(onSendCb).toBeCalledWith("test");
216+
});

0 commit comments

Comments
 (0)