diff --git a/src/pages/home/report/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose.js index ab5aa6fba104..39e05cded832 100644 --- a/src/pages/home/report/ReportActionCompose.js +++ b/src/pages/home/report/ReportActionCompose.js @@ -383,9 +383,19 @@ class ReportActionCompose extends React.Component { */ updateComment(comment, shouldDebounceSaveComment) { const newComment = EmojiUtils.replaceEmojis(comment); - this.setState({ - isCommentEmpty: !!newComment.match(/^(\s|`)*$/), - value: newComment, + this.setState((prevState) => { + const newState = { + isCommentEmpty: !!newComment.match(/^(\s|`)*$/), + value: newComment, + }; + if (comment !== newComment) { + const remainder = prevState.value.slice(prevState.selection.end).length; + newState.selection = { + start: newComment.length - remainder, + end: newComment.length - remainder, + }; + } + return newState; }); // Indicate that draft has been created. diff --git a/src/pages/home/report/ReportActionItemMessageEdit.js b/src/pages/home/report/ReportActionItemMessageEdit.js index 9597eaf52442..2f7fbc29db33 100644 --- a/src/pages/home/report/ReportActionItemMessageEdit.js +++ b/src/pages/home/report/ReportActionItemMessageEdit.js @@ -102,7 +102,17 @@ class ReportActionItemMessageEdit extends React.Component { */ updateDraft(draft) { const newDraft = EmojiUtils.replaceEmojis(draft); - this.setState({draft: newDraft}); + this.setState((prevState) => { + const newState = {draft: newDraft}; + if (draft !== newDraft) { + const remainder = prevState.draft.slice(prevState.selection.end).length; + newState.selection = { + start: newDraft.length - remainder, + end: newDraft.length - remainder, + }; + } + return newState; + }); // This component is rendered only when draft is set to a non-empty string. In order to prevent component // unmount when user deletes content of textarea, we set previous message instead of empty string.