Merge pull request from MareStare/feat/hide-autocomplete-when-no-suggestions

Hide the autocomplete popup entirely if there are no items to show
This commit is contained in:
liamwhite 2025-03-19 08:12:54 -04:00 committed by GitHub
commit 51ba0deb47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 2 deletions

View file

@ -294,7 +294,7 @@ export default tsEslint.config(
'error',
{
// Custom `expectStuff()` functions must also count as assertions.
assertFunctionNames: ['expect*', '*.expect*'],
assertFunctionNames: ['expect*', '*.expect*', 'assert*'],
},
],
},

View file

@ -61,7 +61,16 @@ describe('Suggestions', () => {
[popup, input] = mockBaseSuggestionsPopup();
expect(document.querySelector('.autocomplete')).toBeInstanceOf(HTMLElement);
expect(popup.isHidden).toBe(false);
assert(popup.isHidden);
});
it('should hide the popup when there are no suggestions to show', () => {
[popup, input] = mockBaseSuggestionsPopup();
popup.setSuggestions({ history: [], tags: [] });
popup.showForElement(input);
assert(popup.isHidden);
});
it('should render suggestions', () => {

View file

@ -321,6 +321,13 @@ export class SuggestionsPopupComponent {
}
showForElement(targetElement: HTMLElement) {
if (this.items.length === 0) {
// Hide the popup because there are no suggestions to show. We have to do it
// explicitly, because a border is still rendered even for an empty popup.
this.hide();
return;
}
this.container.style.position = 'absolute';
this.container.style.left = `${targetElement.offsetLeft}px`;