Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ The `MessageInput` component accepts an `overrideSubmitHandler` prop, which allo
conclusion of the underlying `textarea` element's [`handleSubmit`](https://github.com/GetStream/stream-chat-react/blob/master/src/components/MessageInput/hooks/useSubmitHandler.ts)
function.

:::note
You do not have to implement your custom submit handler, if the only thing you need is to pass custom message data to the underlying API call. In that case you can use the [`handleSubmit`](https://github.com/GetStream/stream-chat-react/blob/master/src/components/MessageInput/hooks/useSubmitHandler.ts) function from the [`MessageInputContext`](../contexts/message-input-context.mdx). The `handleSubmit` function allows you to pass custom message data through its second parameter `customMessageData`. This applies to sending a new message as well as updating an existing one. In order for this to work, you will have to implement custom message input components and pass them to [`Channel`](../core-components/channel.mdx) props `EditMessageInput` or `Input` respectively.
:::

The `overrideSubmitHandler` function receives two arguments, the message to be sent and the `cid` (channel type prepended to channel id)
for the currently active channel. The message object is of the following type:

Expand Down
52 changes: 52 additions & 0 deletions src/components/MessageInput/__tests__/MessageInput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Channel } from '../../Channel/Channel';
import { MessageActionsBox } from '../../MessageActions';

import { MessageProvider } from '../../../context/MessageContext';
import { useMessageInputContext } from '../../../context/MessageInputContext';
import { useChatContext } from '../../../context/ChatContext';
import {
dispatchMessageDeletedEvent,
Expand Down Expand Up @@ -664,6 +665,57 @@ function axeNoViolations(container) {
await axeNoViolations(container);
});

it('should allow to send custom message data', async () => {
const customMessageData = { customX: 'customX' };
const CustomInputForm = () => {
const { handleChange, handleSubmit, value } = useMessageInputContext();
return (
<form>
<input onChange={handleChange} placeholder={inputPlaceholder} value={value} />
<button
onClick={(event) => {
handleSubmit(event, customMessageData);
}}
type='submit'
>
Send
</button>
</form>
);
};

const messageInputProps =
componentName === 'EditMessageForm'
? {
messageInputProps: {
message: {
text: `abc`,
},
},
}
: {};

const renderComponent = makeRenderFn(CustomInputForm);
const { container, submit } = await renderComponent(messageInputProps);

fireEvent.change(await screen.findByPlaceholderText(inputPlaceholder), {
target: {
value: 'Some text',
},
});

await act(() => submit());

await waitFor(() => {
const calledMock = componentName === 'EditMessageForm' ? editMock : submitMock;
expect(calledMock).toHaveBeenCalledWith(
expect.stringMatching(/.+:.+/),
expect.objectContaining(customMessageData),
);
});
await axeNoViolations(container);
});

it('Should use overrideSubmitHandler prop if it is defined', async () => {
const overrideMock = jest.fn().mockImplementation(() => Promise.resolve());
const customMessageData = undefined;
Expand Down
1 change: 1 addition & 0 deletions src/components/MessageInput/hooks/useSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const useSubmitHandler = <
await editMessage(({
...message,
...updatedMessage,
...customMessageData,
} as unknown) as UpdatedMessage<StreamChatGenerics>);

clearEditingState?.();
Expand Down