Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatInput: add a handler for when user send a message #63

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/chat-ui-react.chatinput.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ A component that allows user to input message and send to Chat API.
**Signature:**

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

## Parameters

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

**Returns:**

Expand Down
1 change: 1 addition & 0 deletions docs/chat-ui-react.chatinputprops.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ChatInputProps
| [customCssClasses?](./chat-ui-react.chatinputprops.customcssclasses.md) | | [ChatInputCssClasses](./chat-ui-react.chatinputcssclasses.md) | _(Optional)_ CSS classes for customizing the component styling. |
| [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. |
| [inputAutoFocus?](./chat-ui-react.chatinputprops.inputautofocus.md) | | boolean | _(Optional)_ Enable auto focus for the input box. Defaults to false. |
| [onSend?](./chat-ui-react.chatinputprops.onsend.md) | | (message: string) => void | _(Optional)_ A callback which is called when user sends a message. |
| [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...". |
| [sendButtonIcon?](./chat-ui-react.chatinputprops.sendbuttonicon.md) | | JSX.Element | _(Optional)_ Custom icon for the send button. |
| [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. |
Expand Down
13 changes: 13 additions & 0 deletions docs/chat-ui-react.chatinputprops.onsend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[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)

## ChatInputProps.onSend property

A callback which is called when user sends a message.

**Signature:**

```typescript
onSend?: (message: string) => void;
```
2 changes: 1 addition & 1 deletion docs/chat-ui-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| Function | Description |
| --- | --- |
| [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. |
| [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. |
| [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. |
| [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). |
| [ChatPopUp(props)](./chat-ui-react.chatpopup.md) | A component that renders a popup button that displays and hides a panel for chat bot interactions. |
| [MessageBubble({ message, showFeedbackButtons, showTimestamp, customCssClasses, formatTimestamp, })](./chat-ui-react.messagebubble.md) | A component that displays the provided message. |
Expand Down
3 changes: 2 additions & 1 deletion etc/chat-ui-react.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ChatHeaderProps {
}

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

// @public
export interface ChatInputCssClasses {
Expand All @@ -57,6 +57,7 @@ export interface ChatInputProps {
customCssClasses?: ChatInputCssClasses;
handleError?: (e: unknown) => void;
inputAutoFocus?: boolean;
onSend?: (message: string) => void;
placeholder?: string;
sendButtonIcon?: JSX.Element;
stream?: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface ChatInputProps {
sendButtonIcon?: JSX.Element;
/** CSS classes for customizing the component styling. */
customCssClasses?: ChatInputCssClasses;
/** A callback which is called when user sends a message. */
onSend?: (message: string) => void;
}

/**
Expand All @@ -76,6 +78,7 @@ export function ChatInput({
handleError,
sendButtonIcon = <ArrowIcon />,
customCssClasses,
onSend,
}: ChatInputProps) {
const chat = useChatActions();
const [input, setInput] = useState("");
Expand All @@ -91,8 +94,9 @@ export function ChatInput({
? chat.streamNextMessage(input)
: chat.getNextMessage(input);
setInput("");
if (onSend) res.then(() => {onSend(input)});
anguyen-yext2 marked this conversation as resolved.
Show resolved Hide resolved
res.catch((e) => (handleError ? handleError(e) : defaultHandleApiError(e)));
}, [chat, input, handleError, defaultHandleApiError, stream]);
}, [chat, input, handleError, defaultHandleApiError, stream, onSend]);

const handleKeyDown = useCallback(
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
Expand Down
9 changes: 9 additions & 0 deletions tests/components/ChatInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,12 @@ it("executes custom handleError if provided", async () => {
expect(customHandleError).toBeCalledTimes(1);
expect(customHandleError).toBeCalledWith("API Error");
});

it("executes onSend if provided", async () => {
console.log = jest.fn();
anguyen-yext2 marked this conversation as resolved.
Show resolved Hide resolved
render(<ChatInput onSend={message => console.log("Message: ", message)} />);
await act(() => userEvent.type(screen.getByRole("textbox"), "test"));
const sendButton = screen.getByRole("button");
await act(() => userEvent.click(sendButton));
expect(console.log).toBeCalledWith("Message: ", "test");
});