Skip to content

Commit

Permalink
people autocomplete: Fix it doesn't pop if @ entered in new line.
Browse files Browse the repository at this point in the history
We only consider `@` when the preceding char is either ` `, `\n`, `:`
or `#`. Because every email as `@` in it and we don't want to trigger
suggestions in such cases.

Fixes: #3289
  • Loading branch information
jainkuniya authored and gnprice committed Feb 14, 2019
1 parent 68b18ec commit 4842382
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/autocomplete/__tests__/getAutocompleteFilter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ describe('getAutocompleteFilter', () => {
});
});

test('get respective lastWordPrefix even when keyword is entered in new line', () => {
selection = { start: 1, end: 1 };
expect(getAutocompleteFilter('\n@', selection)).toEqual({ filter: '', lastWordPrefix: '' });
expect(getAutocompleteFilter('\n#', selection)).toEqual({ filter: '', lastWordPrefix: '' });
expect(getAutocompleteFilter('\n:', selection)).toEqual({ filter: '', lastWordPrefix: '' });

selection = { start: 2, end: 2 };
expect(getAutocompleteFilter('\n@', selection)).toEqual({ filter: '', lastWordPrefix: '@' });
expect(getAutocompleteFilter('\n#', selection)).toEqual({ filter: '', lastWordPrefix: '#' });
expect(getAutocompleteFilter('\n:', selection)).toEqual({ filter: '', lastWordPrefix: ':' });
});

test('get #streams filters', () => {
selection = { start: 1, end: 1 };
expect(getAutocompleteFilter('#', selection)).toEqual({ filter: '', lastWordPrefix: '#' });
Expand Down
4 changes: 2 additions & 2 deletions src/autocomplete/getAutocompleteFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export default (text: string, selection: InputSelectionType) => {
const lastIndex: number = Math.max(
text.lastIndexOf(':'),
text.lastIndexOf('#'),
[' ', '#', ':'].includes(text[text.lastIndexOf('@') - 1]) || text.lastIndexOf('@') === 0 // to make sure `@` is not the part of email
['\n', ' ', '#', ':'].includes(text[text.lastIndexOf('@') - 1]) || text.lastIndexOf('@') === 0 // to make sure `@` is not the part of email
? text.lastIndexOf('@')
: -1,
);

const lastWordPrefix: string = lastIndex !== -1 ? text[lastIndex] : '';
const filter: string =
text.length > lastIndex + 1 && text[lastIndex + 1] !== ' '
text.length > lastIndex + 1 && !['\n', ' '].includes(text[lastIndex + 1])
? text.substring(lastIndex + 1, text.length)
: '';

Expand Down

0 comments on commit 4842382

Please sign in to comment.