Store value before resetting text input (#4744)

This commit is contained in:
Miguel Alatzar 2020-08-28 05:07:13 -07:00 committed by GitHub
parent 5da3ded728
commit 288caf5e48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View file

@ -146,9 +146,12 @@ export default class DraftInput extends PureComponent {
return messageLength > 0;
};
doSubmitMessage = () => {
doSubmitMessage = (message = null) => {
const {createPost, currentUserId, channelId, files, handleClearFiles, rootId} = this.props;
const value = this.input.current?.getValue() || '';
let value = message;
if (!value) {
value = this.input.current?.getValue() || '';
}
const postFiles = files.filter((f) => !f.failed);
const post = {
user_id: currentUserId,
@ -223,10 +226,10 @@ export default class DraftInput extends PureComponent {
return;
}
const value = this.input.current.getValue();
this.input.current.resetTextInput();
requestAnimationFrame(() => {
const value = this.input.current.getValue();
if (!this.isSendButtonEnabled()) {
this.input.current.setValue(value);
return;
@ -253,12 +256,12 @@ export default class DraftInput extends PureComponent {
const accept = () => {
// Remove only failed files
handleClearFailedFiles(channelId, rootId);
this.sendMessage();
this.sendMessage(value);
};
DraftUtils.alertAttachmentFail(formatMessage, accept, cancel);
} else {
this.sendMessage();
this.sendMessage(value);
}
});
}
@ -303,8 +306,7 @@ export default class DraftInput extends PureComponent {
this.input.current.changeDraft('');
};
sendMessage = () => {
const value = this.input.current?.getValue() || '';
sendMessage = (value = '') => {
const {channelMemberCountsByGroup, enableConfirmNotificationsToChannel, groupsWithAllowReference, membersCount, useGroupMentions, useChannelMentions} = this.props;
const notificationsToChannel = enableConfirmNotificationsToChannel && useChannelMentions;
const notificationsToGroups = enableConfirmNotificationsToChannel && useGroupMentions;
@ -320,10 +322,10 @@ export default class DraftInput extends PureComponent {
if (memberNotifyCount > 0) {
this.showSendToGroupsAlert(Array.from(groupMentionsSet), memberNotifyCount, channelTimezoneCount, value);
} else {
this.doSubmitMessage();
this.doSubmitMessage(value);
}
} else {
this.doSubmitMessage();
this.doSubmitMessage(value);
}
};

View file

@ -12,6 +12,8 @@ jest.mock('react-native-image-picker', () => ({
launchCamera: jest.fn(),
}));
jest.useFakeTimers();
describe('DraftInput', () => {
const baseProps = {
registerTypingAnimation: jest.fn(),
@ -95,10 +97,12 @@ describe('DraftInput', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).toBeCalled();
expect(Alert.alert).toHaveBeenCalledWith('Confirm sending notifications to entire channel', expect.anything(), expect.anything());
});
@ -118,9 +122,11 @@ describe('DraftInput', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).toBeCalled();
});
@ -139,10 +145,12 @@ describe('DraftInput', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).not.toBeCalled();
});
@ -162,10 +170,12 @@ describe('DraftInput', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).not.toHaveBeenCalled();
});
@ -185,10 +195,12 @@ describe('DraftInput', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).not.toHaveBeenCalled();
});
});