The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Update Command Suggestion selected index on MouseEnter (#62603)
Nick Fitzpatrick79cc5d8230ee
diff --git a/e2e/support/helpers/e2e-document-helpers.ts b/e2e/support/helpers/e2e-document-helpers.tsindex be06fcbf4f0..a973b5b3a34 100644--- a/e2e/support/helpers/e2e-document-helpers.ts+++ b/e2e/support/helpers/e2e-document-helpers.ts@@ -13,6 +13,10 @@ export const addToDocument = (text: string, newLine: boolean = true) => {}};+export const clearDocumentContent = () => {+ documentContent().type("{selectAll}{backspace}");+};+export const documentSuggestionDialog = () =>cy.findByRole("dialog", { name: "Mention Dialog" });@@ -22,6 +26,12 @@ export const documentSuggestionItem = (name: RegExp | string) =>export const commandSuggestionDialog = () =>cy.findByRole("dialog", { name: "Command Dialog" });+export const documentMetabotDialog = () =>+ cy.findByRole("dialog", { name: "Metabot dialog" });++export const documentMetabotSuggestionItem = (name: RegExp | string) =>+ documentMetabotDialog().findByRole("option", { name });+export const commandSuggestionItem = (name: RegExp | string) =>commandSuggestionDialog().findByRole("option", { name });diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.tsindex 4bf099798cb..a81f7f7e273 100644--- a/e2e/test/scenarios/documents/documents.cy.spec.ts+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts@@ -231,6 +231,7 @@ H.describeWithSnowplowEE("documents", () => {});cy.get("@documentId").then((id) => cy.visit(`/document/${id}`));+ H.addPostgresDatabase();});it("should support typing with a markdown syntax", () => {@@ -389,6 +390,66 @@ H.describeWithSnowplowEE("documents", () => {});});+ it("should support keyboard and mouse selection in suggestions without double highlight", () => {+ H.documentContent().click();+ H.addToDocument("/", false);++ assertOnlyOneOptionActive(/Ask Metabot/);++ cy.realPress("{downarrow}");+ cy.realPress("{downarrow}");++ //Link should be active+ assertOnlyOneOptionActive("Link");++ // Hover over Quote+ H.commandSuggestionItem(/Quote/).realHover();++ assertOnlyOneOptionActive(/Quote/);++ H.addToDocument("pro", false);++ assertOnlyOneOptionActive(/Products by Category/);++ cy.realPress("{downarrow}");+ assertOnlyOneOptionActive(/Products average/);++ H.commandSuggestionItem(/Products by Category/).realHover();++ assertOnlyOneOptionActive(/Products by Category/);++ cy.realPress("Escape");++ H.clearDocumentContent();++ H.addToDocument("@ord", false);++ cy.realPress("{downarrow}");+ cy.realPress("{downarrow}");++ assertOnlyOneOptionActive(/Orders, Count$/, "mention");++ H.documentSuggestionDialog()+ .findByRole("option", { name: /Browse all/ })+ .realHover();++ assertOnlyOneOptionActive(/Browse all/, "mention");++ cy.realPress("Escape");+ H.clearDocumentContent();+ H.addToDocument("/", false);++ H.commandSuggestionItem(/Ask Metabot/).click();+ H.addToDocument("@", false);++ assertOnlyOneOptionActive(/QA Postgres/, "metabot");+ cy.realPress("{downarrow}");+ assertOnlyOneOptionActive(/Sample/, "metabot");++ H.documentMetabotSuggestionItem(/QA Postgres/).realHover();+ assertOnlyOneOptionActive(/QA Postgres/, "metabot");+ });+it("should support adding cards and updating viz settings", () => {H.documentContent().click();H.addToDocument("/", false);@@ -616,3 +677,24 @@ H.describeWithSnowplowEE("documents", () => {});});});++const assertOnlyOneOptionActive = (+ name: string | RegExp,+ dialog: "command" | "mention" | "metabot" = "command",+) => {+ const dialogContainer =+ dialog === "command"+ ? H.commandSuggestionDialog+ : dialog === "mention"+ ? H.documentSuggestionDialog+ : H.documentMetabotDialog;++ dialogContainer()+ .findByRole("option", { name })+ .should("have.attr", "aria-selected", "true");++ dialogContainer()+ .findAllByRole("option")+ .filter("[aria-selected=true]")+ .should("have.length", 1);+};diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsxindex 58466251927..e9c9a65aaf5 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx@@ -1,5 +1,6 @@import type { Editor, Range } from "@tiptap/core";import {+ type DOMAttributes,forwardRef,useCallback,useEffect,@@ -83,8 +84,8 @@ const CommandMenuItem = forwardRef<option: CommandOption;isSelected?: boolean;onClick?: () => void;- }->(function CommandMenuItem({ option, isSelected, onClick }, ref) {+ } & DOMAttributes<HTMLButtonElement>+>(function CommandMenuItem({ option, isSelected, onClick, ...rest }, ref) {return (<UnstyledButtonref={ref}@@ -93,6 +94,7 @@ const CommandMenuItem = forwardRef<role="option"aria-selected={isSelected}aria-label={option.label}+ {...rest}><Group gap="sm" wrap="nowrap" align="center">{option.icon ? (@@ -397,6 +399,7 @@ export const CommandSuggestion = forwardRef<menuItems={searchMenuItems}selectedIndex={entitySelectedIndex}onItemSelect={entityHandlers.selectItem}+ onItemHover={entityHandlers.hoverHandler}onFooterClick={entityHandlers.openModal}query={query}searchResults={searchResults}@@ -418,6 +421,7 @@ export const CommandSuggestion = forwardRef<item={item}isSelected={selectedIndex === index}onClick={() => selectItem(index)}+ onMouseEnter={() => setSelectedIndex(index)}/>))}{searchMenuItems.length > 0 && commandOptions.length > 0 && (@@ -432,6 +436,7 @@ export const CommandSuggestion = forwardRef<option={option}isSelected={selectedIndex === index}onClick={() => selectItem(index)}+ onMouseEnter={() => setSelectedIndex(index)}/>);})}@@ -471,6 +476,7 @@ export const CommandSuggestion = forwardRef<option={option}isSelected={selectedIndex === index}onClick={() => selectItem(index)}+ onMouseEnter={() => setSelectedIndex(index)}/>);})}diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Mention/MentionSuggestion.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Mention/MentionSuggestion.tsxindex de30cb9501c..038ab8988e5 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Mention/MentionSuggestion.tsx+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Mention/MentionSuggestion.tsx@@ -79,6 +79,7 @@ const MentionSuggestionComponent = forwardRef<modal={modal}onModalSelect={handlers.handleModalSelect}onModalClose={handlers.handleModalClose}+ onItemHover={handlers.hoverHandler}/></SuggestionPaper>);diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/MetabotMention/MetabotSuggestion.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/MetabotMention/MetabotSuggestion.tsxindex b4484200242..27501f5ace8 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/MetabotMention/MetabotSuggestion.tsx+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/MetabotMention/MetabotSuggestion.tsx@@ -146,6 +146,7 @@ const MetabotMentionSuggestionComponent = forwardRef<item={item}isSelected={selectedIndex === index}onClick={() => selectItem(index)}+ onMouseEnter={() => setSelectedIndex(index)}/>))}{query.length > 0 && totalItems === 0 && !isLoading ? (diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/EntitySearchSection.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/EntitySearchSection.tsxindex ad534cc79af..03549779596 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/EntitySearchSection.tsx+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/EntitySearchSection.tsx@@ -21,6 +21,7 @@ interface EntitySearchSectionProps {modal: "question-picker" | null;onModalSelect: (item: QuestionPickerValueItem) => void;onModalClose: () => void;+ onItemHover: (index: number) => void;}export function EntitySearchSection({@@ -33,6 +34,7 @@ export function EntitySearchSection({modal,onModalSelect,onModalClose,+ onItemHover,}: EntitySearchSectionProps) {return (<>@@ -42,6 +44,7 @@ export function EntitySearchSection({item={item}isSelected={selectedIndex === index}onClick={() => onItemSelect(index)}+ onMouseEnter={() => onItemHover(index)}/>))}{query.length > 0 && searchResults.length === 0 ? (@@ -53,6 +56,7 @@ export function EntitySearchSection({<SearchResultsFooterisSelected={selectedIndex === menuItems.length}onClick={onFooterClick}+ onMouseEnter={() => onItemHover(menuItems.length)}/>{modal === "question-picker" && (diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySuggestions.ts b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySuggestions.tsindex c0331280027..9b24a23a170 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySuggestions.ts+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySuggestions.ts@@ -30,6 +30,7 @@ interface UseEntitySuggestionsResult {handleModalSelect: (item: QuestionPickerValueItem) => void;handleModalClose: () => void;openModal: () => void;+ hoverHandler: (index: number) => void;};}@@ -96,6 +97,11 @@ export function useEntitySuggestions({selectItem(selectedIndex);}, [selectItem, selectedIndex]);+ const hoverHandler = useCallback(+ (index: number) => setSelectedIndex(index),+ [],+ );+const onKeyDown = useCallback(({ event }: { event: KeyboardEvent }) => {if (event.key === "ArrowUp") {@@ -160,6 +166,7 @@ export function useEntitySuggestions({handleModalSelect,handleModalClose,openModal,+ hoverHandler,},};}diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuComponents.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuComponents.tsxindex 5f6ed765720..84a806add8c 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuComponents.tsx+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuComponents.tsx@@ -1,3 +1,4 @@+import type { DOMAttributes } from "react";import { t } from "ttag";import {@@ -12,7 +13,7 @@ import type { SearchModel } from "metabase-types/api";import S from "./MenuItems.module.css";-interface ExtraItemProps {+interface ExtraItemProps extends DOMAttributes<HTMLButtonElement> {isSelected?: boolean;onClick?: () => void;}@@ -31,16 +32,18 @@ export const MenuItemComponent = ({item,isSelected,onClick,+ ...rest}: {item: MenuItem;isSelected?: boolean;onClick?: () => void;-}) => (+} & DOMAttributes<HTMLButtonElement>) => (<UnstyledButtonclassName={S.menuItem}onClick={onClick || item.action}role="option"aria-selected={isSelected}+ {...rest}><Group gap="sm" wrap="nowrap" align="center"><Icon name={item.icon} size={16} color={item.iconColor || "inherit"} />@@ -61,12 +64,14 @@ export const MenuItemComponent = ({export const SearchResultsFooter = ({isSelected,onClick,+ ...rest}: ExtraItemProps) => (<UnstyledButtonclassName={S.menuItem}onClick={onClick}role="option"aria-selected={isSelected}+ {...rest}><Group gap="sm" wrap="nowrap" align="center"><Icon name="search" size={16} color="inherit" />diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuItems.module.css b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuItems.module.cssindex 9aae78c055e..45de637f21d 100644--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuItems.module.css+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/shared/MenuItems.module.css@@ -12,7 +12,6 @@border-radius: 6px;}-.menuItem:hover,.menuItem[aria-selected="true"] {color: var(--mb-color-brand);background-color: var(--mb-color-background-hover);