Skip to content

Commit

Permalink
fixes issue primefaces#7014: inserting mentions results in duplicated…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
chaowss committed Aug 10, 2024
1 parent bdf4a42 commit d692859
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 = spaceIndex > -1 ? `${prevText}${selectedText}${nextText}` : `${prevText}${selectedText} ${nextText}`;

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

0 comments on commit d692859

Please sign in to comment.