From 69873b240b0449ced1ed1bafe6e800f2de9766ff Mon Sep 17 00:00:00 2001 From: chaowss <147555653+chaowss@users.noreply.github.com> Date: Sun, 11 Aug 2024 05:50:56 -0600 Subject: [PATCH] fixes issue primefaces#7014: inserting mentions results in duplicated text (#7017) * fixes issue primefaces#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 --- components/lib/mention/Mention.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/components/lib/mention/Mention.js b/components/lib/mention/Mention.js index 9c95fce699..16e02389c4 100644 --- a/components/lib/mention/Mention.js +++ b/components/lib/mention/Mention.js @@ -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, ''); @@ -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); }