Skip to content

Commit 6d262b7

Browse files
authored
Merge pull request #84105 from ryntgh/issue-83510
Fix: Mentions no longer auto-insert trailing space
2 parents 85b4a45 + b885cfe commit 6d262b7

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/CONST/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4161,6 +4161,7 @@ const CONST = {
41614161
SPECIAL_CHAR: /[,/?"{}[\]()&^%;`$=#<>!*]/g,
41624162
FIRST_SPACE: /.+?(?=\s)/,
41634163
TRAILING_DOTS: /\.$/,
4164+
STARTS_WITH_PUNCTUATION: /^[.,!?]/,
41644165

41654166
get SPECIAL_CHAR_OR_EMOJI() {
41664167
return new RegExp(`[~\\n\\s]|(_\\b(?!$))|${this.SPECIAL_CHAR.source}|${this.EMOJI.source}`, 'gu');

src/pages/inbox/report/ReportActionCompose/SuggestionMention.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,11 @@ function SuggestionMention({
242242
suggestionValues.atSignIndex + Math.max(mentionToReplace.length, suggestionValues.mentionPrefix.length + suggestionValues.prefixType.length),
243243
);
244244

245-
updateComment(`${commentBeforeAtSign}${mentionCode}${dotToAppend}${trimLeadingSpace(commentAfterMention)}`, true);
246-
const selectionPosition = suggestionValues.atSignIndex + mentionCode.length + CONST.SPACE_LENGTH;
245+
const trimmedCommentAfterMention = trimLeadingSpace(commentAfterMention);
246+
const spacer = !trimmedCommentAfterMention || !CONST.REGEX.STARTS_WITH_PUNCTUATION.test(trimmedCommentAfterMention) ? ' ' : '';
247+
248+
updateComment(`${commentBeforeAtSign}${mentionCode}${dotToAppend}${spacer}${trimmedCommentAfterMention}`, true);
249+
const selectionPosition = suggestionValues.atSignIndex + mentionCode.length + dotToAppend.length + spacer.length;
247250
setSelection({
248251
start: selectionPosition,
249252
end: selectionPosition,

tests/unit/SuggestionMentionTest.tsx

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe('SuggestionMention', () => {
161161
);
162162
});
163163

164-
it('preserves trailing punctuation dot when selected mention does not include dotted prefix', async () => {
164+
it('preserves trailing punctuation dot and inserts trailing space when selected mention does not include dotted prefix', async () => {
165165
mockPersonalDetails = {};
166166
mockPersonalDetails[2] = {
167167
accountID: 2,
@@ -178,11 +178,11 @@ describe('SuggestionMention', () => {
178178

179179
act(() => onSelect(0));
180180

181-
expect(updateComment).toHaveBeenCalledWith('@adam@example.com.', true);
182-
expect(setSelection).toHaveBeenCalledWith({start: 18, end: 18});
181+
expect(updateComment).toHaveBeenCalledWith('@adam@example.com. ', true);
182+
expect(setSelection).toHaveBeenCalledWith({start: 19, end: 19});
183183
});
184184

185-
it('does not append an extra trailing dot when selected mention already matches dotted prefix', async () => {
185+
it('does not append an extra trailing dot and inserts trailing space when selected mention already matches dotted prefix', async () => {
186186
mockPersonalDetails = {};
187187
mockPersonalDetails[2] = {
188188
accountID: 2,
@@ -199,7 +199,49 @@ describe('SuggestionMention', () => {
199199

200200
act(() => onSelect(0));
201201

202-
expect(updateComment).toHaveBeenCalledWith('@a.smith@example.com', true);
202+
expect(updateComment).toHaveBeenCalledWith('@a.smith@example.com ', true);
203203
expect(setSelection).toHaveBeenCalledWith({start: 21, end: 21});
204204
});
205+
206+
it('does not insert trailing space when mention is followed by a comma (punctuation)', async () => {
207+
mockPersonalDetails = {};
208+
mockPersonalDetails[2] = {
209+
accountID: 2,
210+
login: 'adam@example.com',
211+
firstName: 'Adam',
212+
lastName: 'Tester',
213+
};
214+
215+
const updateComment = jest.fn();
216+
const {setSelection} = renderSuggestionMention('@a, thanks', updateComment, {start: 2, end: 2});
217+
218+
await waitFor(() => expect(mockMentionSuggestionsSpy).toHaveBeenCalled());
219+
const {onSelect} = getLastMentionSuggestionsProps();
220+
221+
act(() => onSelect(0));
222+
223+
expect(updateComment).toHaveBeenCalledWith('@adam@example.com, thanks', true);
224+
expect(setSelection).toHaveBeenCalledWith({start: 17, end: 17});
225+
});
226+
227+
it('inserts trailing space when mention is followed by a regular word', async () => {
228+
mockPersonalDetails = {};
229+
mockPersonalDetails[2] = {
230+
accountID: 2,
231+
login: 'adam@example.com',
232+
firstName: 'Adam',
233+
lastName: 'Tester',
234+
};
235+
236+
const updateComment = jest.fn();
237+
const {setSelection} = renderSuggestionMention('@a thanks', updateComment, {start: 2, end: 2});
238+
239+
await waitFor(() => expect(mockMentionSuggestionsSpy).toHaveBeenCalled());
240+
const {onSelect} = getLastMentionSuggestionsProps();
241+
242+
act(() => onSelect(0));
243+
244+
expect(updateComment).toHaveBeenCalledWith('@adam@example.com thanks', true);
245+
expect(setSelection).toHaveBeenCalledWith({start: 18, end: 18});
246+
});
205247
});

0 commit comments

Comments
 (0)