Skip to content

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

Fixed extraneous item in filter popover in embeds (#70634)

Sébastien92001b00de46
diff --git a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
index 7334cebc5a2..b12f0a5971d 100644
--- a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
+++ b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js
@@ -12,7 +12,7 @@ import {
questionDetailsWithDefaults,
} from "./shared/embedding-dashboard";
-const { ORDERS, PEOPLE, PRODUCTS, ORDERS_ID } = SAMPLE_DATABASE;
+const { ORDERS, PEOPLE, PEOPLE_ID, PRODUCTS, ORDERS_ID } = SAMPLE_DATABASE;
describe("scenarios > embedding > static embedding dashboard", () => {
beforeEach(() => {
@@ -1383,6 +1383,84 @@ describe("scenarios > embedding > dashboard appearance", () => {
});
});
});
+
+ it("should not show raw parameter value in static-list filter dropdown when using initial-parameters", () => {
+ const staticListFilter = {
+ name: "Number",
+ slug: "number",
+ id: "static-list-id",
+ type: "number/=",
+ sectionId: "number",
+ values_source_type: "static-list",
+ values_source_config: {
+ values: [
+ ["1", "Option A"],
+ ["2", "Option B"],
+ ],
+ },
+ };
+
+ H.createQuestionAndDashboard({
+ questionDetails: {
+ name: "Static list filter question",
+ query: {
+ "source-table": PEOPLE_ID,
+ expressions: { Thing: ["*", 1, 1] },
+ },
+ },
+ dashboardDetails: {
+ parameters: [staticListFilter],
+ enable_embedding: true,
+ embedding_params: {
+ [staticListFilter.slug]: "enabled",
+ },
+ },
+ }).then(({ body: { id, card_id, dashboard_id } }) => {
+ cy.request("PUT", `/api/dashboard/${dashboard_id}`, {
+ dashcards: [
+ {
+ id,
+ card_id,
+ row: 0,
+ col: 0,
+ size_x: 24,
+ size_y: 9,
+ parameter_mappings: [
+ {
+ parameter_id: staticListFilter.id,
+ card_id,
+ target: [
+ "dimension",
+ ["expression", "Thing", { "base-type": "type/Integer" }],
+ { "stage-number": 0 },
+ ],
+ },
+ ],
+ },
+ ],
+ });
+
+ H.visitEmbeddedPage(
+ {
+ resource: { dashboard: dashboard_id },
+ params: {},
+ },
+ {
+ setFilters: { [staticListFilter.slug]: "1" },
+ },
+ );
+
+ H.filterWidget().findByText("Option A").should("be.visible");
+
+ H.filterWidget().findByText("Option A").click();
+ H.popover().within(() => {
+ cy.findByText("Option A").should("exist");
+ cy.findByText("Option B").should("exist");
+ // The raw numeric value "1" should NOT appear as a separate option
+ cy.findByText("1").should("not.exist");
+ });
+ });
+ });
});
function openFilterOptions(name) {
diff --git a/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/ListField.tsx b/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/ListField.tsx
index 94909be3dc6..f65825c9228 100644
--- a/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/ListField.tsx
+++ b/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/ListField.tsx
@@ -21,7 +21,11 @@ import {
OptionsList,
} from "./ListField.styled";
import type { ListFieldProps, Option } from "./types";
-import { getOptionDisplayName, optionMatchesFilter } from "./utils";
+import {
+ getOptionDisplayName,
+ normalizeValuesToOptionKeys,
+ optionMatchesFilter,
+} from "./utils";
const DEBOUNCE_FILTER_TIME = delay(100);
@@ -44,9 +48,15 @@ export const ListField = ({
isDashboardFilter,
isLoading,
}: ListFieldProps) => {
- const [selectedValues, setSelectedValues] = useState(new Set(value));
+ const normalizedValue = useMemo(
+ () => normalizeValuesToOptionKeys(value, options),
+ [value, options],
+ );
+ const [selectedValues, setSelectedValues] = useState(
+ new Set(normalizedValue),
+ );
const [addedOptions, setAddedOptions] = useState<Option[]>(() =>
- createOptionsFromValuesWithoutOptions(value, options),
+ createOptionsFromValuesWithoutOptions(normalizedValue, options),
);
const augmentedOptions = useMemo(() => {
diff --git a/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/utils.ts b/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/utils.ts
index 1127c894a1e..9045b896472 100644
--- a/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/utils.ts
+++ b/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/ListField/utils.ts
@@ -34,3 +34,22 @@ export const removeDiacritics = (value: string) =>
export const getOptionDisplayName = (option: Option | RowValue[]) =>
String(option.at(-1));
+
+/**
+ * Coerces selected values to match option key types via string comparison.
+ * This fixes a type mismatch where URL query params are normalized to numbers
+ * (e.g. `1`) but static-list option keys remain strings (e.g. `"1"`), causing
+ * `Map.has()` / `Set.has()` to miss the match and create synthetic options.
+ */
+export function normalizeValuesToOptionKeys(
+ values: RowValue[],
+ options: Option[],
+): RowValue[] {
+ const optionKeysByString = new Map(
+ options.map((option) => [String(option[0]), option[0]]),
+ );
+ return values.map((value) => {
+ const matchingKey = optionKeysByString.get(String(value));
+ return matchingKey !== undefined ? matchingKey : value;
+ });
+}
diff --git a/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/SingleSelectListField/SingleSelectListField.tsx b/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/SingleSelectListField/SingleSelectListField.tsx
index e1c5937d1e1..c03b3dd1a8b 100644
--- a/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/SingleSelectListField/SingleSelectListField.tsx
+++ b/frontend/src/metabase/parameters/components/widgets/ParameterFieldWidget/FieldValuesWidget/SingleSelectListField/SingleSelectListField.tsx
@@ -13,7 +13,11 @@ import { PLUGIN_CONTENT_TRANSLATION } from "metabase/plugins";
import { Flex } from "metabase/ui";
import type { RowValue } from "metabase-types/api";
-import { getOptionDisplayName, optionMatchesFilter } from "../ListField/utils";
+import {
+ getOptionDisplayName,
+ normalizeValuesToOptionKeys,
+ optionMatchesFilter,
+} from "../ListField/utils";
import {
EmptyStateContainer,
@@ -48,9 +52,13 @@ const SingleSelectListField = ({
isLoading,
checkedColor,
}: SingleSelectListFieldProps) => {
- const [selectedValue, setSelectedValue] = useState(value?.[0]);
+ const normalizedValue = useMemo(
+ () => normalizeValuesToOptionKeys(value, options),
+ [value, options],
+ );
+ const [selectedValue, setSelectedValue] = useState(normalizedValue?.[0]);
const [addedOptions, setAddedOptions] = useState<Option[]>(() =>
- createOptionsFromValuesWithoutOptions(value, options),
+ createOptionsFromValuesWithoutOptions(normalizedValue, options),
);
const tc = useTranslateContent();
const sortByTranslation =