Skip to content

Commit

Permalink
Merge pull request #38361 from allroundexperts/fix-38358
Browse files Browse the repository at this point in the history
fix: allow suggestion menu to close once a mention is chosen
  • Loading branch information
mountiny authored Mar 18, 2024
2 parents cda1b10 + 6eeb994 commit 9e40a11
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/pages/home/report/ReportActionCompose/SuggestionMention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ function SuggestionMention(
shouldExcludeTextAreaNodes: false,
});

// Used to store the selection index of the last inserted mention
const suggestionInsertionIndexRef = useRef<number | null>(null);

// Used to detect if the selection has changed since the last suggestion insertion
// If so, we reset the suggestionInsertionIndexRef
const hasSelectionChanged = !(selection.end === selection.start && selection.start === suggestionInsertionIndexRef.current);
if (hasSelectionChanged) {
suggestionInsertionIndexRef.current = null;
}

// Used to decide whether to block the suggestions list from showing to prevent flickering
const shouldBlockCalc = useRef(false);

Expand Down Expand Up @@ -89,10 +99,12 @@ function SuggestionMention(
const commentAfterMention = value.slice(suggestionValues.atSignIndex + suggestionValues.mentionPrefix.length + 1);

updateComment(`${commentBeforeAtSign}${mentionCode} ${SuggestionsUtils.trimLeadingSpace(commentAfterMention)}`, true);
const selectionPosition = suggestionValues.atSignIndex + mentionCode.length + CONST.SPACE_LENGTH;
setSelection({
start: suggestionValues.atSignIndex + mentionCode.length + CONST.SPACE_LENGTH,
end: suggestionValues.atSignIndex + mentionCode.length + CONST.SPACE_LENGTH,
start: selectionPosition,
end: selectionPosition,
});
suggestionInsertionIndexRef.current = selectionPosition;
setSuggestionValues((prevState) => ({
...prevState,
suggestedMentions: [],
Expand Down Expand Up @@ -167,6 +179,14 @@ function SuggestionMention(
if (searchValue && !displayText.toLowerCase().includes(searchValue.toLowerCase())) {
return false;
}

// Given the mention is inserted by user, we don't want to show the mention options unless the
// selection index changes. In that case, suggestionInsertionIndexRef.current will be null.
// See https://github.com/Expensify/App/issues/38358 for more context
if (suggestionInsertionIndexRef.current) {
return false;
}

return true;
});

Expand Down Expand Up @@ -247,7 +267,6 @@ function SuggestionMention(

if (!isCursorBeforeTheMention && isMentionCode(suggestionWord)) {
const suggestions = getMentionOptions(personalDetails, prefix);

nextState.suggestedMentions = suggestions;
nextState.shouldShowSuggestionMenu = !!suggestions.length;
}
Expand All @@ -268,7 +287,6 @@ function SuggestionMention(
// See: https://github.com/facebook/react-native/pull/36930#issuecomment-1593028467
return;
}

calculateMentionSuggestion(selection.end);
}, [selection, value, previousValue, calculateMentionSuggestion]);

Expand Down

0 comments on commit 9e40a11

Please sign in to comment.