From 59012492f4eba4d21dc3db7d90331c01b8762997 Mon Sep 17 00:00:00 2001 From: Alexander Polyankin <alexander.polyankin@metabase.com> Date: Fri, 30 Jun 2023 15:18:39 +0300 Subject: [PATCH] Fix showing suggestions for an unfocused custom expression field (#31994) --- .../cc-typing-suggestion.cy.spec.js | 21 +++++++++++++++++-- .../ExpressionEditorTextfield.tsx | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/e2e/test/scenarios/custom-column/cc-typing-suggestion.cy.spec.js b/e2e/test/scenarios/custom-column/cc-typing-suggestion.cy.spec.js index 1bed09bca73..9bf8bb73ded 100644 --- a/e2e/test/scenarios/custom-column/cc-typing-suggestion.cy.spec.js +++ b/e2e/test/scenarios/custom-column/cc-typing-suggestion.cy.spec.js @@ -1,7 +1,9 @@ import { enterCustomColumnDetails, openProductsTable, + popover, restore, + summarize, } from "e2e/support/helpers"; describe("scenarios > question > custom column > typing suggestion", () => { @@ -10,16 +12,16 @@ describe("scenarios > question > custom column > typing suggestion", () => { cy.signInAsAdmin(); openProductsTable({ mode: "notebook" }); - // eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage - cy.findByText("Custom column").click(); }); it("should not suggest arithmetic operators", () => { + addCustomColumn(); enterCustomColumnDetails({ formula: "[Price] " }); cy.findByTestId("expression-suggestions-list").should("not.exist"); }); it("should correctly accept the chosen field suggestion", () => { + addCustomColumn(); enterCustomColumnDetails({ formula: "[Rating]{leftarrow}{leftarrow}{leftarrow}", }); @@ -34,6 +36,7 @@ describe("scenarios > question > custom column > typing suggestion", () => { }); it("should correctly accept the chosen function suggestion", () => { + addCustomColumn(); enterCustomColumnDetails({ formula: "LTRIM([Title])" }); // Place the cursor between "is" and "empty" @@ -47,6 +50,7 @@ describe("scenarios > question > custom column > typing suggestion", () => { }); it("should correctly insert function suggestion with the opening parenthesis", () => { + addCustomColumn(); enterCustomColumnDetails({ formula: "LOW{enter}" }); // eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage @@ -54,6 +58,7 @@ describe("scenarios > question > custom column > typing suggestion", () => { }); it("should show expression function helper if a proper function is typed", () => { + addCustomColumn(); enterCustomColumnDetails({ formula: "lower(" }); // eslint-disable-next-line no-unscoped-text-selectors -- deprecated usage @@ -74,4 +79,16 @@ describe("scenarios > question > custom column > typing suggestion", () => { "be.visible", ); }); + + it("should not show suggestions for an unfocused field (metabase#31643)", () => { + summarize({ mode: "notebook" }); + popover().findByText("Custom Expression").click(); + enterCustomColumnDetails({ formula: "Count{enter}" }); + popover().findByLabelText("Name").focus(); + cy.findByTestId("expression-suggestions-list").should("not.exist"); + }); }); + +const addCustomColumn = () => { + cy.findByTestId("action-buttons").findByText("Custom column").click(); +}; diff --git a/frontend/src/metabase/query_builder/components/expressions/ExpressionEditorTextfield/ExpressionEditorTextfield.tsx b/frontend/src/metabase/query_builder/components/expressions/ExpressionEditorTextfield/ExpressionEditorTextfield.tsx index 75e44b5bd21..1d44821b34d 100644 --- a/frontend/src/metabase/query_builder/components/expressions/ExpressionEditorTextfield/ExpressionEditorTextfield.tsx +++ b/frontend/src/metabase/query_builder/components/expressions/ExpressionEditorTextfield/ExpressionEditorTextfield.tsx @@ -407,7 +407,9 @@ class ExpressionEditorTextfield extends React.Component< }); this.setState({ helpText: helpText || null }); - this.updateSuggestions(suggestions); + if (this.state.isFocused) { + this.updateSuggestions(suggestions); + } } errorAsMarkers(errorMessage: ErrorWithMessage | null = null): IMarker[] { -- GitLab