diff --git a/.changeset/seven-kangaroos-remember.md b/.changeset/seven-kangaroos-remember.md new file mode 100644 index 00000000..4497960d --- /dev/null +++ b/.changeset/seven-kangaroos-remember.md @@ -0,0 +1,5 @@ +--- +"react-mentions": patch +--- + +Fixed native undo/redo in case the input was not focused before the operation diff --git a/src/MentionsInput.js b/src/MentionsInput.js index 9efc60aa..68e6b73d 100755 --- a/src/MentionsInput.js +++ b/src/MentionsInput.js @@ -517,13 +517,23 @@ class MentionsInput extends React.Component { let newPlainTextValue = ev.target.value + let selectionStartBefore = this.state.selectionStart; + if(selectionStartBefore == null) { + selectionStartBefore = ev.target.selectionStart; + } + + let selectionEndBefore = this.state.selectionEnd; + if(selectionEndBefore == null) { + selectionEndBefore = ev.target.selectionEnd; + } + // Derive the new value to set by applying the local change in the textarea's plain text let newValue = applyChangeToValue( value, newPlainTextValue, { - selectionStartBefore: this.state.selectionStart, - selectionEndBefore: this.state.selectionEnd, + selectionStartBefore, + selectionEndBefore, selectionEndAfter: ev.target.selectionEnd, }, config