Skip to content

Commit 1224f5f

Browse files
authored
fix: Add multiple mentions (#939)
* fix: Stop matching mentions at next @ * refactor: Remove unneccessary nested template string * Fix: avoid trim when checking for tokens This is a **hack** to avoid an issue with the regex we use to match mentions expecting a user to have two names, to determine that a the input is/contains a mention. * fix: get start of new token based on caret placement The regex used for this so far matches an `@` and then any following charactes except whitespace and `@`, in practice one word. Therefore, we don't actually know that we search for the right trigger. This looks for the previous trigger backwards in the string to be modified, limited at where the current caret is.
1 parent bab26b2 commit 1224f5f

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

package/src/components/AutoCompleteInput/AutoCompleteInput.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,7 @@ const AutoCompleteInputWithContext = <
288288

289289
const textToModify = text.slice(0, selectionEnd.current);
290290

291-
const startOfTokenPosition = textToModify.search(
292-
/**
293-
* It's important to escape the trigger char for chars like [, (,...
294-
*/
295-
new RegExp(`\\${trigger}${`[^\\${trigger}${'\\s'}]`}*$`),
296-
);
291+
const startOfTokenPosition = textToModify.lastIndexOf(trigger, selectionEnd.current);
297292

298293
const newCaretPosition = computeCaretPosition(newTokenString, startOfTokenPosition);
299294

@@ -335,7 +330,7 @@ const AutoCompleteInputWithContext = <
335330
};
336331

337332
const handleMentions = ({ tokenMatch }: { tokenMatch: RegExpMatchArray | null }) => {
338-
const lastToken = tokenMatch?.[tokenMatch.length - 1].trim();
333+
const lastToken = tokenMatch?.[tokenMatch.length - 1];
339334
const handleMentionsTrigger =
340335
(lastToken && Object.keys(triggerSettings).find((trigger) => trigger === lastToken[0])) ||
341336
null;
@@ -401,7 +396,7 @@ const AutoCompleteInputWithContext = <
401396
} else if (giphyEnabled && !(await handleCommand(text))) {
402397
const mentionTokenMatch = text
403398
.slice(0, selectionEnd.current)
404-
.match(/(?!^|\W)?@[^\s]*\s?[^\s]*$/g);
399+
.match(/(?!^|\W)?@[^\s@]*\s?[^\s@]*$/g);
405400
if (mentionTokenMatch) {
406401
handleMentions({ tokenMatch: mentionTokenMatch });
407402
} else {

0 commit comments

Comments
 (0)