Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

GDGT-2845 Fix Table to map dropdown search with more than 2 letters (#77946)

Kamil Mielnika3f5a5bf8a3d
diff --git a/frontend/src/metabase/common/components/AccordionList/AccordionList.tsx b/frontend/src/metabase/common/components/AccordionList/AccordionList.tsx
index 8ea821233b2..1febe8d3359 100644
--- a/frontend/src/metabase/common/components/AccordionList/AccordionList.tsx
+++ b/frontend/src/metabase/common/components/AccordionList/AccordionList.tsx
@@ -370,14 +370,16 @@ export class AccordionList<
}
}
+ // TSection can't be constructed here and renderers only read optional Section fields
+ const syntheticSection: TSection = {} as TSection;
+
if (globalSearch) {
const isEmpty = rows.filter((row) => row.type === "item").length === 0;
if (isSearching && isEmpty) {
rows.unshift({
type: "no-results",
- // Unjustified type cast. FIXME
- section: {} as TSection,
+ section: syntheticSection,
sectionIndex: 0,
isLastSection: false,
});
@@ -385,11 +387,43 @@ export class AccordionList<
rows.unshift({
type: "search",
- // Unjustified type cast. FIXME
- section: {} as TSection,
+ section: syntheticSection,
sectionIndex: 0,
isLastSection: false,
});
+ } else if (isSearching) {
+ /**
+ * Without this, a search that matches nothing filters out every type-less section
+ * and collapses the list, hiding the search input, so the query can't be cleared.
+ */
+ const hasItems = rows.some((row) => row.type === "item");
+ const hasSearchRow = rows.some((row) => row.type === "search");
+ const isListSearchable = sections.some(
+ (section, sectionIndex) =>
+ sectionIsSearchable(sectionIndex) &&
+ sectionIsExpanded(sectionIndex) &&
+ section.items &&
+ section.items.length > 0 &&
+ !section.loading,
+ );
+
+ if (isListSearchable && !hasItems) {
+ if (!hasSearchRow) {
+ rows.unshift({
+ type: "search",
+ section: syntheticSection,
+ sectionIndex: 0,
+ isLastSection: false,
+ });
+ }
+
+ rows.push({
+ type: "no-results",
+ section: syntheticSection,
+ sectionIndex: 0,
+ isLastSection: false,
+ });
+ }
}
return rows;
diff --git a/frontend/src/metabase/common/components/AccordionList/AccordionList.unit.spec.tsx b/frontend/src/metabase/common/components/AccordionList/AccordionList.unit.spec.tsx
index cfc08d23b0b..5f9d29dac98 100644
--- a/frontend/src/metabase/common/components/AccordionList/AccordionList.unit.spec.tsx
+++ b/frontend/src/metabase/common/components/AccordionList/AccordionList.unit.spec.tsx
@@ -5,12 +5,13 @@ import { fireEvent, render, screen } from "__support__/ui";
import { HoverCard } from "metabase/ui";
import { AccordionList } from "./AccordionList";
+import type { Section } from "./types";
type Item = {
name: string;
};
-const SECTIONS = [
+const SECTIONS: Section<Item>[] = [
{
name: "Widgets",
items: [{ name: "Foo" }, { name: "Bar" }],
@@ -156,6 +157,71 @@ describe("AccordionList", () => {
expect(screen.getByText("Didn't find any results")).toBeInTheDocument();
});
+ describe("when a search matches nothing without globalSearch (GDGT-2845)", () => {
+ const TYPELESS_SECTION = [{ items: [{ name: "Foo" }, { name: "Bar" }] }];
+
+ it("keeps the search box and shows an empty state for a type-less section", () => {
+ render(<AccordionList sections={TYPELESS_SECTION} searchable />);
+
+ const SEARCH_FIELD = screen.getByPlaceholderText("Find...");
+ fireEvent.change(SEARCH_FIELD, { target: { value: "Quu" } });
+
+ expect(screen.getByPlaceholderText("Find...")).toBeInTheDocument();
+ expect(screen.getByText("Didn't find any results")).toBeInTheDocument();
+ assertAbsence(["Foo", "Bar"]);
+ });
+
+ it("keeps the search box and shows an empty state for a typed section", () => {
+ const sections: Section<Item>[] = [
+ { name: "Back", type: "back", items: [{ name: "Foo" }] },
+ ];
+ render(<AccordionList sections={sections} searchable />);
+
+ const SEARCH_FIELD = screen.getByPlaceholderText("Find...");
+ fireEvent.change(SEARCH_FIELD, { target: { value: "Quu" } });
+
+ expect(screen.getByPlaceholderText("Find...")).toBeInTheDocument();
+ expect(screen.getByText("Didn't find any results")).toBeInTheDocument();
+ assertAbsence(["Foo"]);
+ });
+
+ it("keeps the search box and shows an empty state for an alwaysExpanded list", () => {
+ render(
+ <AccordionList sections={TYPELESS_SECTION} searchable alwaysExpanded />,
+ );
+
+ const SEARCH_FIELD = screen.getByPlaceholderText("Find...");
+ fireEvent.change(SEARCH_FIELD, { target: { value: "Quu" } });
+
+ expect(screen.getByPlaceholderText("Find...")).toBeInTheDocument();
+ expect(screen.getByText("Didn't find any results")).toBeInTheDocument();
+ assertAbsence(["Foo", "Bar"]);
+ });
+
+ it("shows matches again once the query is cleared", () => {
+ render(<AccordionList sections={TYPELESS_SECTION} searchable />);
+
+ const SEARCH_FIELD = screen.getByPlaceholderText("Find...");
+ fireEvent.change(SEARCH_FIELD, { target: { value: "Quu" } });
+ assertAbsence(["Foo", "Bar"]);
+
+ fireEvent.change(SEARCH_FIELD, { target: { value: "" } });
+ assertPresence(["Foo", "Bar"]);
+ expect(
+ screen.queryByText("Didn't find any results"),
+ ).not.toBeInTheDocument();
+ });
+
+ it("does not show an empty state when the list is not searchable", () => {
+ render(<AccordionList sections={TYPELESS_SECTION} searchable={false} />);
+
+ expect(screen.queryByPlaceholderText("Find...")).not.toBeInTheDocument();
+ expect(
+ screen.queryByText("Didn't find any results"),
+ ).not.toBeInTheDocument();
+ });
+ });
+
describe("with the `renderItemWrapper` prop", () => {
it("should be able to wrap the list items in components like popovers", async () => {
const renderItemWrapper = (itemContent: ReactNode) => {
diff --git a/frontend/src/metabase/querying/common/components/DataSelector/DataSelectorFieldPicker/DataSelectorFieldPicker.tsx b/frontend/src/metabase/querying/common/components/DataSelector/DataSelectorFieldPicker/DataSelectorFieldPicker.tsx
index a7e0cd7c6b6..3c5354a893a 100644
--- a/frontend/src/metabase/querying/common/components/DataSelector/DataSelectorFieldPicker/DataSelectorFieldPicker.tsx
+++ b/frontend/src/metabase/querying/common/components/DataSelector/DataSelectorFieldPicker/DataSelectorFieldPicker.tsx
@@ -90,8 +90,6 @@ export const DataSelectorFieldPicker = ({
maxHeight={Infinity}
width="100%"
searchable={hasFiltering}
- // keep the search box + "no results" state visible when a search matches no fields
- globalSearch={hasFiltering}
onChange={(item: { field: Field }) => onChangeField(item.field)}
itemIsSelected={checkIfItemIsSelected}
itemIsClickable={(item: FieldWithName) => Boolean(item.field)}