Skip to content

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

fix(select): don't render option groups emptied by search (#80197)

Maksym Yakubych84be938a6e80
diff --git a/frontend/src/metabase/ui/components/inputs/Select/Select.tsx b/frontend/src/metabase/ui/components/inputs/Select/Select.tsx
index 9b813f0b66b..c7c8b7c5db3 100644
--- a/frontend/src/metabase/ui/components/inputs/Select/Select.tsx
+++ b/frontend/src/metabase/ui/components/inputs/Select/Select.tsx
@@ -2,8 +2,9 @@ import type {
ComboboxItem,
ComboboxItemGroup,
SelectProps as MantineSelectProps,
+ OptionsFilter,
} from "@mantine/core";
-import { Select as MantineSelect } from "@mantine/core";
+import { Select as MantineSelect, defaultOptionsFilter } from "@mantine/core";
import mergeRefs from "merge-refs";
import { type Ref, forwardRef, useCallback, useMemo, useRef } from "react";
@@ -35,13 +36,22 @@ export interface SelectProps<Value extends string | null = string> extends Omit<
onChange?: (newValue: Value) => void;
}
+function dropEmptyGroups(filter: OptionsFilter): OptionsFilter {
+ return (input) =>
+ filter(input).filter((item) => !("group" in item) || item.items.length > 0);
+}
+
function SelectWrapper<Value extends string | null>(
props: SelectProps<Value>,
ref: Ref<HTMLElement>,
) {
- const { onDropdownOpen, searchable } = props;
+ const { onDropdownOpen, searchable, filter } = props;
const inputRef = useRef<HTMLInputElement>(null);
const combinedRef = useMemo(() => mergeRefs(ref, inputRef), [ref]);
+ const filterWithoutEmptyGroups = useMemo(
+ () => dropEmptyGroups(filter ?? defaultOptionsFilter),
+ [filter],
+ );
const handleDropdownOpen = useCallback(() => {
if (searchable) {
inputRef.current?.select();
@@ -54,6 +64,7 @@ function SelectWrapper<Value extends string | null>(
{...props}
// @ts-expect-error -- our tighter types are better
ref={combinedRef}
+ filter={filterWithoutEmptyGroups}
// A bit confusing prop name but it actually means "on change of search input", not Select's value
selectFirstOptionOnChange={
props.selectFirstOptionOnChange ?? props.searchable
diff --git a/frontend/src/metabase/ui/components/inputs/Select/Select.unit.spec.tsx b/frontend/src/metabase/ui/components/inputs/Select/Select.unit.spec.tsx
index afcba5c0ca3..c80183cca32 100644
--- a/frontend/src/metabase/ui/components/inputs/Select/Select.unit.spec.tsx
+++ b/frontend/src/metabase/ui/components/inputs/Select/Select.unit.spec.tsx
@@ -9,6 +9,11 @@ const DATA = [
{ value: "banana", label: "Banana" },
];
+const GROUPED_DATA = [
+ { group: "Pome", items: [{ value: "apple", label: "Apple" }] },
+ { group: "Berry", items: [{ value: "banana", label: "Banana" }] },
+];
+
type SetupOpts = Omit<Partial<SelectProps>, "onChange" | "onDropdownOpen">;
function setup({ data = DATA, ...props }: SetupOpts = {}) {
@@ -77,6 +82,41 @@ describe("Select", () => {
});
});
+ describe("grouped data", () => {
+ it("should not render groups whose options are all filtered out", async () => {
+ const { input } = setup({ data: GROUPED_DATA, searchable: true });
+
+ await userEvent.click(input);
+ await userEvent.keyboard("ban");
+
+ expect(screen.getByText("Berry")).toBeInTheDocument();
+ expect(screen.queryByText("Pome")).not.toBeInTheDocument();
+ expect(screen.getAllByRole("group")).toHaveLength(1);
+ });
+
+ it("should apply the same rule to a user-provided filter", async () => {
+ const { input } = setup({
+ data: GROUPED_DATA,
+ searchable: true,
+ filter: ({ options }) =>
+ options.map((option) =>
+ "group" in option
+ ? {
+ ...option,
+ items: option.items.filter((item) => item.value === "banana"),
+ }
+ : option,
+ ),
+ });
+
+ await userEvent.click(input);
+
+ expect(screen.getByText("Berry")).toBeInTheDocument();
+ expect(screen.queryByText("Pome")).not.toBeInTheDocument();
+ expect(screen.getAllByRole("group")).toHaveLength(1);
+ });
+ });
+
describe("non-searchable", () => {
it("should not pre-select input text on open and should still open the dropdown", async () => {
const { input } = setup({ defaultValue: "apple" });