Skip to content

Commit

Permalink
fixes issue #7014: inserting mentions results in duplicated text (#7017)
Browse files Browse the repository at this point in the history
* fixes issue #7014: inserting mentions results in duplicated text

The issue was that event.target is not the prompt form but actually a list item element,
which doesn't have the selectionStart attribute.

I've also added a change where inserting the mention in front of a space does not result in a double space.

* check to add space only if space is at beginning of nextText
  • Loading branch information
chaowss authored Aug 11, 2024
1 parent 64a6efb commit 69873b2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions components/lib/mention/Mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ export const Mention = React.memo(
};

const selectItem = (event, suggestion) => {
const value = inputRef.current.value;
const selectionStart = event.target.selectionStart;
const input = inputRef.current;
const value = input.value;
const selectionStart = input.selectionStart;

const spaceIndex = value.indexOf(' ', triggerState.index);
const currentText = value.substring(triggerState.index, spaceIndex > -1 ? spaceIndex : selectionStart);
const selectedText = formatValue(suggestion).replace(/\s+/g, '');
Expand All @@ -218,7 +220,8 @@ export const Mention = React.memo(
const prevText = value.substring(0, triggerState.index);
const nextText = value.substring(spaceIndex > -1 ? selectionStart : triggerState.index + currentText.length);

inputRef.current.value = `${prevText}${selectedText} ${nextText}`;
inputRef.current.value = nextText[0] === ' ' ? `${prevText}${selectedText}${nextText}` : `${prevText}${selectedText} ${nextText}`;

event.target = inputRef.current;
props.onChange && props.onChange(event);
}
Expand Down

0 comments on commit 69873b2

Please sign in to comment.