A platform for community discussion. Free, open, simple.
FIX: Ensure search keyboard nav within a topic works with Ask AI (#43164)
Natalie Tay5d37dd34f092
diff --git a/frontend/discourse/app/components/search-menu/results.gjs b/frontend/discourse/app/components/search-menu/results.gjsindex 0271caa2fc4..1c48f2c5125 100644--- a/frontend/discourse/app/components/search-menu/results.gjs+++ b/frontend/discourse/app/components/search-menu/results.gjs@@ -91,7 +91,7 @@ export default class Results extends Component {}}/>{{#unless this.inTopicContext}}- {{#if @suggestionKeyword}}+ {{#if (and @suggestionKeyword this.showIndexedResults)}}<Assistant@suggestionKeyword={{@suggestionKeyword}}@results={{@suggestionResults}}diff --git a/frontend/discourse/app/services/search.js b/frontend/discourse/app/services/search.jsindex 0e8b90c8880..7aa523dd521 100644--- a/frontend/discourse/app/services/search.js+++ b/frontend/discourse/app/services/search.js@@ -83,44 +83,49 @@ export default class Search extends Service {return;}- let links = document.querySelectorAll(".search-menu .results a");- let results = document.querySelectorAll(- ".search-menu .results .search-link"+ const focusableItems = document.querySelectorAll(+ ".search-menu .results a, .search-menu .results [data-search-menu-navigation-item]"+ );+ const navigationItems = document.querySelectorAll(+ ".search-menu .results .search-link, .search-menu .results [data-search-menu-navigation-item]");- if (!results.length) {+ if (!navigationItems.length) {return;}- let prevResult;- let result;+ let previousNavigationItem;+ let navigationItem;- links.forEach((item) => {- if (item.classList.contains("search-link")) {- prevResult = item;+ focusableItems.forEach((item) => {+ if (+ item.classList.contains("search-link") ||+ item.hasAttribute("data-search-menu-navigation-item")+ ) {+ previousNavigationItem = item;}if (item === focused) {- result = prevResult;+ navigationItem = previousNavigationItem;}});let index = -1;- if (result) {- index = Array.prototype.indexOf.call(results, result);+ if (navigationItem) {+ index = Array.prototype.indexOf.call(navigationItems, navigationItem);}if (index === -1 && e.key === "ArrowDown") {- // change focus from the search input to the first result item- const firstResult = results[0] || links[0];+ // change focus from the search input to the first navigation item+ const firstResult = navigationItems[0] || focusableItems[0];firstResult.focus();} else if (index === 0 && e.key === "ArrowUp") {this.focusSearchInput();} else if (index > -1) {- // change focus to the next result item if present+ // change focus to the next navigation item if presentindex += e.key === "ArrowDown" ? 1 : -1;- if (index >= 0 && index < results.length) {- results[index].focus();+ if (index >= 0 && index < navigationItems.length) {+ navigationItems[index].focus();}}diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjsindex 33b6947a9bf..959d8e31f25 100644--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs@@ -1,5 +1,6 @@import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";+import { on } from "@ember/modifier";import { action, get } from "@ember/object";import { service } from "@ember/service";import { MODIFIER_REGEXP } from "discourse/components/search-menu";@@ -356,24 +357,30 @@ export default class AiDiscoveriesSearchOptions extends Component {<template>{{#if this.query}}- <div class="ai-discoveries-search-options">+ {{! eslint-disable ember/template-no-invalid-interactive }}+ <div+ class="ai-discoveries-search-options"+ {{on "keydown" this.search.handleArrowUpOrDown}}+ >{{#each this.options key="kind" as |option|}}<DButtonclass="btn-default btn-small ai-discoveries-search-options__option --{{option.kind}}{{if option.active 'is-active'}}"+ data-search-menu-navigation-item+ @action={{option.action}}@icon={{option.icon}}@label={{option.label}}@translatedLabel={{option.translatedLabel}}@translatedTitle={{option.title}}- @action={{option.action}}/>{{/each}}<DButtonclass="btn-default btn-small ai-discoveries-search-options__option --advanced"+ data-search-menu-navigation-item+ @action={{@openAdvancedSearch}}+ @ariaLabel="search.open_advanced"@icon="sliders"@translatedTitle={{this.advancedTitle}}- @ariaLabel="search.open_advanced"- @action={{@openAdvancedSearch}}/></div>{{/if}}diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.jsindex 05c4f63fe15..6ab8d2662d9 100644--- a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js@@ -23,6 +23,8 @@ import {import { i18n } from "discourse-i18n";acceptance("AI Discoveries - welcome search", function (needs) {+ let submittedRequestId;+needs.user({can_use_ask_ai: true,});@@ -33,6 +35,26 @@ acceptance("AI Discoveries - welcome search", function (needs) {ai_ask_ai_agent: -41,});+ needs.pretender((server, helper) => {+ server.get("/discourse-ai/credits/status", () => helper.response({}));+ server.get("/discourse-ai/discoveries/recent", () =>+ helper.response({ recent_asks: [] })+ );+ server.get("/tags/filter/search", () =>+ helper.response({+ results: [{ id: 87, text: "film", name: "film", slug: "film" }],+ })+ );+ server.post("/discourse-ai/discoveries/reply", (request) => {+ submittedRequestId = helper.parsePostData(request.requestBody).request_id;+ return helper.response({ request_id: submittedRequestId });+ });+ });++ needs.hooks.beforeEach(() => {+ submittedRequestId = undefined;+ });+test("the placeholder offers both ways to resolve a term", async function (assert) {await visit("/");@@ -43,6 +65,25 @@ acceptance("AI Discoveries - welcome search", function (needs) {i18n("discourse_ai.discobot_discoveries.search_placeholder"));});++ test("tag suggestions step aside for an Ask AI answer", async function (assert) {+ await visit("/");+ await fillIn(+ "#welcome-banner-search-input",+ "となりのトトロ #general #film"+ );+ await waitFor(".search-menu-assistant .search-menu-assistant-item");++ find(".ai-discoveries-search-options__option.--ask").dispatchEvent(+ new MouseEvent("click", { bubbles: true })+ );+ await waitFor(".ai-discobot-discoveries");+ await waitUntil(() => submittedRequestId);++ assert+ .dom(".search-menu-assistant .search-menu-assistant-item")+ .doesNotExist("the tag suggestion no longer appears below the answer");+ });});acceptance("AI Discoveries - header search", function (needs) {@@ -396,6 +437,26 @@ acceptance("AI Discoveries - header search", function (needs) {assert.dom(".search-result-topic").exists("and searches beyond the topic");});+ test("topic scope can be selected with keyboard navigation", async function (assert) {+ updateCurrentUser({ user_option: { ai_ask_ai_default: true } });++ await visit("/t/internationalization-localization/280");+ await click("#search-button");+ await fillIn("#icon-search-input", "dev");++ await triggerKeyEvent("#icon-search-input", "keyup", "ArrowDown");+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");++ assert+ .dom(".ai-discoveries-search-options__option.--topic")+ .isFocused("arrow navigation focuses the topic scope option");++ await triggerKeyEvent(document.activeElement, "keydown", "Enter");+ assert+ .dom(".ai-discoveries-search-options__option.--topic")+ .hasClass("is-active", "enter scopes the search to the topic");+ });+test("still offers itself from a message inbox", async function (assert) {await visit("/");// the inbox sets this, and it used to disqualify the menu outright