From d69285966de67c6cfcb2e8f4ade05ba5ddad6810 Mon Sep 17 00:00:00 2001 From: Xiaozheng Wu Date: Fri, 9 Aug 2024 21:55:52 -0600 Subject: [PATCH] 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. --- 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..cc3a02c812 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 = spaceIndex > -1 ? `${prevText}${selectedText}${nextText}` : `${prevText}${selectedText} ${nextText}`; + event.target = inputRef.current; props.onChange && props.onChange(event); }