FIX: correctly closes search menu on escape (#29848)

Prior to this fix the menu would not close if a child was in focus, and the search suggestions had a special implementation to handle this. The fix now relies on trapping the keydown escape event on the top dip of the search menu.
This commit is contained in:
Joffrey JAFFEUX 2024-11-21 07:39:47 +01:00 committed by GitHub
parent 429cf656e7
commit 0b210e0fc3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 18 deletions

View File

@ -1,4 +1,9 @@
<div class={{this.classNames}} {{did-insert this.setupEventListeners}}> <div
class={{this.classNames}}
{{did-insert this.setupEventListeners}}
{{! template-lint-disable no-invalid-interactive }}
{{on "keydown" this.onKeydown}}
>
<div class="search-input"> <div class="search-input">
{{#if this.search.inTopicContext}} {{#if this.search.inTopicContext}}
<DButton <DButton

View File

@ -105,6 +105,13 @@ export default class SearchMenu extends Component {
return false; return false;
} }
@action
onKeydown(event) {
if (event.key === "Escape") {
this.close();
}
}
@action @action
close() { close() {
if (this.args?.onClose) { if (this.args?.onClose) {

View File

@ -70,19 +70,13 @@ export default class AssistantItem extends Component {
return; return;
} }
if (e.key === "Escape") {
this.args.closeSearchMenu();
e.preventDefault();
return false;
}
if (e.key === "Enter") { if (e.key === "Enter") {
this.itemSelected(); this.itemSelected();
} }
this.search.handleArrowUpOrDown(e); if (e.key === "ArrowUp" || e.key === "ArrowDown") {
e.stopPropagation(); this.search.handleArrowUpOrDown(e);
e.preventDefault(); }
} }
@action @action

View File

@ -11,7 +11,6 @@
@withInLabel={{@withInLabel}} @withInLabel={{@withInLabel}}
@isIntersection={{true}} @isIntersection={{true}}
@searchAllTopics={{true}} @searchAllTopics={{true}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="tag-intersection" @typeClass="tag-intersection"
@ -27,7 +26,6 @@
@slug={{get this.fullSlugForCategoryMap result.model.id}} @slug={{get this.fullSlugForCategoryMap result.model.id}}
@withInLabel={{@withInLabel}} @withInLabel={{@withInLabel}}
@searchAllTopics={{true}} @searchAllTopics={{true}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="category" @typeClass="category"
@ -39,7 +37,6 @@
@slug={{concat this.prefix "#" result.name}} @slug={{concat this.prefix "#" result.name}}
@withInLabel={{@withInLabel}} @withInLabel={{@withInLabel}}
@searchAllTopics={{true}} @searchAllTopics={{true}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="tag" @typeClass="tag"
@ -55,7 +52,6 @@
@slug={{concat this.prefix "@" this.user.username}} @slug={{concat this.prefix "@" this.user.username}}
@suffix={{i18n "search.in_topics_posts"}} @suffix={{i18n "search.in_topics_posts"}}
@searchAllTopics={{true}} @searchAllTopics={{true}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="user" @typeClass="user"
@ -65,7 +61,6 @@
@user={{this.user}} @user={{this.user}}
@slug={{concat this.prefix "@" this.user.username}} @slug={{concat this.prefix "@" this.user.username}}
@suffix={{i18n "search.in_this_topic"}} @suffix={{i18n "search.in_this_topic"}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="user" @typeClass="user"
@ -76,7 +71,6 @@
@user={{result}} @user={{result}}
@slug={{concat this.prefix "@" result.username}} @slug={{concat this.prefix "@" result.username}}
@searchAllTopics={{true}} @searchAllTopics={{true}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="user" @typeClass="user"
@ -93,7 +87,6 @@
@slug={{concat this.prefix item}} @slug={{concat this.prefix item}}
@label={{item}} @label={{item}}
@searchAllTopics={{true}} @searchAllTopics={{true}}
@closeSearchMenu={{@closeSearchMenu}}
@searchTermChanged={{@searchTermChanged}} @searchTermChanged={{@searchTermChanged}}
@suggestionKeyword={{@suggestionKeyword}} @suggestionKeyword={{@suggestionKeyword}}
@typeClass="shortcut" @typeClass="shortcut"

View File

@ -2,6 +2,7 @@ import {
click, click,
currentURL, currentURL,
fillIn, fillIn,
focus,
triggerEvent, triggerEvent,
triggerKeyEvent, triggerKeyEvent,
visit, visit,
@ -415,6 +416,15 @@ acceptance("Search - Anonymous", function (needs) {
.dom(".search-menu .search-result-topic .item .topic-title img[alt='+1']") .dom(".search-menu .search-result-topic .item .topic-title img[alt='+1']")
.exists(":+1: in the topic title is properly converted to an emoji"); .exists(":+1: in the topic title is properly converted to an emoji");
}); });
test("pressing escape correctly closes the menu", async function (assert) {
await visit("/");
await click("#search-button");
await focus(".show-advanced-search");
await triggerKeyEvent("#search-term", "keydown", "Escape");
assert.dom(".search-menu-panel").doesNotExist();
});
}); });
acceptance("Search - Authenticated", function (needs) { acceptance("Search - Authenticated", function (needs) {
@ -1253,7 +1263,7 @@ acceptance("Search - assistant", function (needs) {
); );
}); });
test("topic results - soft loads the topic results after closing then search menu", async function (assert) { test("topic results - soft loads the topic results after closing the search menu", async function (assert) {
await visit("/"); await visit("/");
await click("#search-button"); await click("#search-button");
await fillIn("#search-term", "Development mode"); await fillIn("#search-term", "Development mode");