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
3 changes: 3 additions & 0 deletions src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type ChannelPropsForwardedToComponentContext = Pick<
| 'ReactionsListModal'
| 'SendButton'
| 'StartRecordingAudioButton'
| 'TextareaComposer'
| 'ThreadHead'
| 'ThreadHeader'
| 'ThreadStart'
Expand Down Expand Up @@ -1229,6 +1230,7 @@ const ChannelInner = (
StartRecordingAudioButton: props.StartRecordingAudioButton,
StopAIGenerationButton: props.StopAIGenerationButton,
StreamedMessageText: props.StreamedMessageText,
TextareaComposer: props.TextareaComposer,
ThreadHead: props.ThreadHead,
ThreadHeader: props.ThreadHeader,
ThreadStart: props.ThreadStart,
Expand Down Expand Up @@ -1291,6 +1293,7 @@ const ChannelInner = (
props.StartRecordingAudioButton,
props.StopAIGenerationButton,
props.StreamedMessageText,
props.TextareaComposer,
props.ThreadHead,
props.ThreadHeader,
props.ThreadStart,
Expand Down
4 changes: 4 additions & 0 deletions src/components/Channel/__tests__/Channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Thread } from '../../Thread';
import { MessageProvider } from '../../../context';
import { MessageActionsBox } from '../../MessageActions';
import { DEFAULT_THREAD_PAGE_SIZE } from '../../../constants/limits';
import { generateMessageDraft } from '../../../mock-builders/generator/messageDraft';

jest.mock('../../Loading', () => ({
LoadingErrorIndicator: jest.fn(() => <div />),
Expand Down Expand Up @@ -173,6 +174,9 @@ describe('Channel', () => {
pinnedMessages,
user,
}));
jest.spyOn(channel, 'getDraft').mockResolvedValue({
draft: generateMessageDraft({ channel, channel_cid: channel.cid }),
});
});

afterEach(() => {
Expand Down
1 change: 0 additions & 1 deletion src/components/MessageInput/EditMessageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export const EditMessageModal = ({
>
<MessageInput
clearEditingState={clearEditingState}
grow
hideSendButton
Input={EditMessageInput}
{...additionalMessageInputProps}
Expand Down
4 changes: 1 addition & 3 deletions src/components/MessageInput/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type MessageInputProps = {
*/
additionalTextareaProps?: Omit<
React.TextareaHTMLAttributes<HTMLTextAreaElement>,
'defaultValue'
'defaultValue' | 'style' | 'disabled' | 'value'
>;
/**
* When enabled, recorded messages won’t be sent immediately.
Expand All @@ -55,8 +55,6 @@ export type MessageInputProps = {
emojiSearchIndex?: ComponentContextValue['emojiSearchIndex'];
/** If true, focuses the text input on component mount */
focus?: boolean;
/** If true, expands the text input vertically for new lines */
grow?: boolean;
/** Allows to hide MessageInput's send button. */
hideSendButton?: boolean;
/** Custom UI component handling how the message input is rendered, defaults to and accepts the same props as [MessageInputFlat](https://github.com/GetStream/stream-chat-react/blob/master/src/components/MessageInput/MessageInputFlat.tsx) */
Expand Down
5 changes: 3 additions & 2 deletions src/components/MessageInput/MessageInputFlat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
QuotedMessagePreviewHeader,
} from './QuotedMessagePreview';
import { LinkPreviewList as DefaultLinkPreviewList } from './LinkPreviewList';
import { TextareaComposer } from '../TextareaComposer';
import { TextareaComposer as DefaultTextareaComposer } from '../TextareaComposer';
import { AIStates, useAIState } from '../AIStateIndicator';
import { RecordingAttachmentType } from '../MediaRecorder/classes';

Expand Down Expand Up @@ -53,7 +53,8 @@ export const MessageInputFlat = () => {
SendButton = DefaultSendButton,
StartRecordingAudioButton = DefaultStartRecordingAudioButton,
StopAIGenerationButton: StopAIGenerationButtonOverride,
} = useComponentContext('MessageInputFlat');
TextareaComposer = DefaultTextareaComposer,
} = useComponentContext();
const { channel } = useChatContext('MessageInputFlat');
const { aiState } = useAIState(channel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ export const useCreateMessageInputContext = (value: MessageInputContextValue) =>
cooldownRemaining,
emojiSearchIndex,
focus,
grow,
handleSubmit,
hideSendButton,
insertText,
isThreadInput,
maxRows,
minRows,
Expand All @@ -39,10 +37,8 @@ export const useCreateMessageInputContext = (value: MessageInputContextValue) =>
cooldownRemaining,
emojiSearchIndex,
focus,
grow,
handleSubmit,
hideSendButton,
insertText,
isThreadInput,
maxRows,
minRows,
Expand Down
8 changes: 3 additions & 5 deletions src/components/MessageInput/hooks/useMessageInputControls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type React from 'react';
import { useMessageInputText } from './useMessageInputText';
import { useTextareaRef } from './useTextareaRef';
import { useSubmitHandler } from './useSubmitHandler';
import { usePasteHandler } from './usePasteHandler';
import { useMediaRecorder } from '../../MediaRecorder/hooks/useMediaRecorder';
Expand All @@ -12,7 +12,6 @@ export type MessageInputHookProps = {
event?: React.BaseSyntheticEvent,
customMessageData?: Omit<UpdatedMessage, 'mentioned_users'>,
) => void;
insertText: (textToInsert: string) => void;
onPaste: (event: React.ClipboardEvent<HTMLTextAreaElement>) => void;
recordingController: RecordingController;
textareaRef: React.MutableRefObject<HTMLTextAreaElement | null | undefined>;
Expand All @@ -24,7 +23,7 @@ export const useMessageInputControls = (
const { asyncMessagesMultiSendEnabled, audioRecordingConfig, audioRecordingEnabled } =
props;

const { insertText, textareaRef } = useMessageInputText(props);
const { textareaRef } = useTextareaRef(props);

const { handleSubmit } = useSubmitHandler(props);

Expand All @@ -35,11 +34,10 @@ export const useMessageInputControls = (
recordingConfig: audioRecordingConfig,
});

const { onPaste } = usePasteHandler(insertText);
const { onPaste } = usePasteHandler();

return {
handleSubmit,
insertText,
onPaste,
recordingController,
textareaRef,
Expand Down
58 changes: 0 additions & 58 deletions src/components/MessageInput/hooks/useMessageInputText.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/components/MessageInput/hooks/usePasteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useCallback } from 'react';
import { useMessageComposer } from './useMessageComposer';
import { dataTransferItemsToFiles } from '../../ReactFileUtilities';

export const usePasteHandler = (insertText: (textToInsert: string) => void) => {
const { attachmentManager } = useMessageComposer();
export const usePasteHandler = () => {
const { attachmentManager, textComposer } = useMessageComposer();
const onPaste = useCallback(
(clipboardEvent: React.ClipboardEvent<HTMLTextAreaElement>) => {
(async (event) => {
Expand All @@ -29,13 +29,13 @@ export const usePasteHandler = (insertText: (textToInsert: string) => void) => {

if (plainTextPromise) {
const pastedText = await plainTextPromise;
insertText(pastedText);
textComposer.insertText({ text: pastedText });
} else {
attachmentManager.uploadFiles(fileLikes);
}
})(clipboardEvent);
},
[attachmentManager, insertText],
[attachmentManager, textComposer],
);

return { onPaste };
Expand Down
17 changes: 17 additions & 0 deletions src/components/MessageInput/hooks/useTextareaRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect, useRef } from 'react';
import type { MessageInputProps } from '../MessageInput';

export const useTextareaRef = (props: MessageInputProps) => {
const { focus } = props;
const textareaRef = useRef<HTMLTextAreaElement>(undefined);
// Focus
useEffect(() => {
if (focus && textareaRef.current) {
textareaRef.current.focus();
}
}, [focus]);

return {
textareaRef,
};
};
52 changes: 37 additions & 15 deletions src/components/TextareaComposer/TextareaComposer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import debounce from 'lodash.debounce';
import clsx from 'clsx';
import type { ChangeEventHandler, TextareaHTMLAttributes, UIEventHandler } from 'react';
import type {
ChangeEventHandler,
SyntheticEvent,
TextareaHTMLAttributes,
UIEventHandler,
} from 'react';
import { useMemo } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import Textarea from 'react-textarea-autosize';
import { useMessageComposer } from '../MessageInput';
Expand Down Expand Up @@ -40,52 +47,49 @@
const defaultShouldSubmit = (event: React.KeyboardEvent<HTMLTextAreaElement>) =>
event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing;

export type TextComposerProps = Omit<
export type TextareaComposerProps = Omit<
TextareaHTMLAttributes<HTMLTextAreaElement>,
'style' | 'defaultValue' | 'disabled'
'style' | 'defaultValue' | 'disabled' | 'value'
> & {
closeSuggestionsOnClickOutside?: boolean;
containerClassName?: string;
dropdownClassName?: string;
grow?: boolean;
itemClassName?: string;
listClassName?: string;
maxRows?: number;
minRows?: number;
shouldSubmit?: (event: React.KeyboardEvent<HTMLTextAreaElement>) => boolean;
};

export const TextareaComposer = ({
className,
closeSuggestionsOnClickOutside,
containerClassName,
// dropdownClassName, // todo: X find a different way to prevent prop drilling
grow: growProp,
// itemClassName, // todo: X find a different way to prevent prop drilling
listClassName,
maxRows: maxRowsProp = 1,
minRows: minRowsProp,
onBlur,
onChange,
onKeyDown,
onScroll,
onSelect,
placeholder: placeholderProp,
shouldSubmit: shouldSubmitProp,
...restProps
}: TextComposerProps) => {
...restTextareaProps
}: TextareaComposerProps) => {
const { t } = useTranslationContext();
const { AutocompleteSuggestionList = DefaultSuggestionList } = useComponentContext();
const {
additionalTextareaProps,
cooldownRemaining,
grow: growContext,
handleSubmit,
maxRows: maxRowsContext,
minRows: minRowsContext,
onPaste,
shouldSubmit: shouldSubmitContext,
textareaRef,
} = useMessageInputContext();

const grow = growProp ?? growContext;
const maxRows = maxRowsProp ?? maxRowsContext;
const minRows = minRowsProp ?? minRowsContext;
const placeholder = placeholderProp ?? additionalTextareaProps?.placeholder;
const shouldSubmit = shouldSubmitProp ?? shouldSubmitContext ?? defaultShouldSubmit;

Expand Down Expand Up @@ -205,6 +209,22 @@
[onScroll, textComposer],
);

const setSelectionDebounced = useMemo(
() =>
debounce(
(e: SyntheticEvent<HTMLTextAreaElement>) => {
onSelect?.(e);
textComposer.setSelection({

Check warning on line 217 in src/components/TextareaComposer/TextareaComposer.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/TextareaComposer/TextareaComposer.tsx#L216-L217

Added lines #L216 - L217 were not covered by tests
end: (e.target as HTMLTextAreaElement).selectionEnd,
start: (e.target as HTMLTextAreaElement).selectionStart,
});
},
100,
{ leading: false, trailing: true },
),
[onSelect, textComposer],
);

useEffect(() => {
// FIXME: find the real reason for cursor being set to the end on each change
// This is a workaround to prevent the cursor from jumping
Expand Down Expand Up @@ -235,7 +255,7 @@
ref={containerRef}
>
<Textarea
{...restProps}
{...{ ...additionalTextareaProps, ...restTextareaProps }}
aria-label={cooldownRemaining ? t('Slow Mode ON') : placeholder}
className={clsx(
'rta__textarea',
Expand All @@ -244,14 +264,16 @@
)}
data-testid='message-input'
disabled={!enabled || !!cooldownRemaining}
maxRows={grow ? maxRows : 1}
maxRows={maxRows}
minRows={minRows}
onBlur={onBlur}
onChange={changeHandler}
onCompositionEnd={onCompositionEnd}
onCompositionStart={onCompositionStart}
onKeyDown={keyDownHandler}
onPaste={onPaste}
onScroll={scrollHandler}
onSelect={setSelectionDebounced}
placeholder={placeholder || t('Type your message')}
ref={(ref) => {
textareaRef.current = ref;
Expand Down
Loading