Skip to content

Commit

Permalink
Send file based messages even with no text (#769)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefukami authored Jul 11, 2023
1 parent e677c03 commit de817fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/components/message-input/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ describe('MessageInput', () => {
expect(onSubmit).toHaveBeenCalledWith('Hello', [], []);
});

it('submits message when Enter is pressed and files have been added', () => {
const onSubmit = jest.fn();
const dropzoneToMedia = (files) => files;
const wrapper = subject({ onSubmit, dropzoneToMedia });
const dropzone = wrapper.find(Dropzone).shallow();

const input = dropzone.find(MentionsInput);
wrapper.find(Dropzone).simulate('drop', [{ name: 'file1' }]);
input.simulate('keydown', { preventDefault() {}, key: Key.Enter, shiftKey: false });

expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit).toHaveBeenCalledWith('', [], [{ name: 'file1' }]);
});

it('submits message when send icon is clicked', () => {
const onSubmit = jest.fn();
const wrapper = subject({ onSubmit, placeholder: 'Speak' });
Expand Down
7 changes: 5 additions & 2 deletions src/components/message-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Properties extends PublicPropertiesContainer {
addPasteListener: (listener: EventListenerOrEventListenerObject) => void;
removePasteListener: (listener: EventListenerOrEventListenerObject) => void;
};
dropzoneToMedia?: (files: any[]) => Media[];
}

interface State {
Expand Down Expand Up @@ -121,7 +122,7 @@ export class MessageInput extends React.Component<Properties, State> {

onSend = (): void => {
const { mentionedUserIds, value, media } = this.state;
if (value) {
if (value || media.length) {
this.props.onSubmit(value, mentionedUserIds, media);
this.setState({ value: '', mentionedUserIds: [], media: [] });
}
Expand Down Expand Up @@ -226,7 +227,9 @@ export class MessageInput extends React.Component<Properties, State> {
};

imagesSelected = (acceptedFiles): void => {
const newImages: Media[] = dropzoneToMedia(acceptedFiles);
const newImages: Media[] = this.props.dropzoneToMedia
? this.props.dropzoneToMedia(acceptedFiles)
: dropzoneToMedia(acceptedFiles);
if (newImages.length) {
this.mediaSelected(newImages);
}
Expand Down

0 comments on commit de817fb

Please sign in to comment.