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
GDGT-3405: Use SegmentedControl for chart setting segmented controls (#85016)
Will Fryba4e0c5ab02d
diff --git a/e2e/test/scenarios/visualizations/charts/boxplot.cy.spec.js b/e2e/test/scenarios/visualizations/charts/boxplot.cy.spec.jsindex cb70ea1e9bc..7ec4134866b 100644--- a/e2e/test/scenarios/visualizations/charts/boxplot.cy.spec.js+++ b/e2e/test/scenarios/visualizations/charts/boxplot.cy.spec.js@@ -136,17 +136,17 @@ describe("scenarios > visualizations > boxplot", () => {cy.findByText("Show values on data points").click();});- // After enabling, "Values to display" option appears with segmented buttons+ // After enabling, "Values to display" option appears as a segmented controlH.leftSidebar().within(() => {cy.findByText("Values to display").should("exist");- cy.findByRole("button", { name: "Median only" }).should("exist");+ cy.findByRole("radio", { name: "Median only" }).should("be.checked");});// With "Median only", the median labels show (2025 median is 35), but not the other valuesH.echartsContainer().findAllByText("35").should("exist");H.echartsContainer().findByText("412").should("not.exist");- H.leftSidebar().findByRole("button", { name: "All" }).click();+ H.leftSidebar().findByText("All").click();// Verify label value appearsH.echartsContainer().findByText("412").should("exist");@@ -154,15 +154,11 @@ describe("scenarios > visualizations > boxplot", () => {H.leftSidebar().findByText("Hide overlapping labels").click();H.echartsContainer().findByText("91.75").should("exist");- // Test "Auto formatting" segmented button+ // Test "Auto formatting" segmented controlH.leftSidebar().within(() => {cy.findByText("Auto formatting").should("exist");- cy.findByRole("button", { name: "Full" }).click();- cy.findByRole("button", { name: "Full" }).should(- "have.attr",- "data-variant",- "filled",- );+ cy.findByText("Full").click();+ cy.findByRole("radio", { name: "Full" }).should("be.checked");});H.leftSidebar().findByText("Goal line").click();diff --git a/e2e/test/scenarios/visualizer/basics.cy.spec.ts b/e2e/test/scenarios/visualizer/basics.cy.spec.tsindex 018f1f9c67d..e9e999f48aa 100644--- a/e2e/test/scenarios/visualizer/basics.cy.spec.ts+++ b/e2e/test/scenarios/visualizer/basics.cy.spec.ts@@ -76,7 +76,7 @@ describe("scenarios > visualizer > basics", () => {H.verticalWell().findByText("Count").should("exist");H.horizontalWell().findByText("Created At: Month").should("exist");- cy.findByDisplayValue("line").should("be.checked");+ H.assertCurrentVisualization("line");cy.button("Add to dashboard").click();});diff --git a/e2e/test/scenarios/visualizer/cartesian.cy.spec.ts b/e2e/test/scenarios/visualizer/cartesian.cy.spec.tsindex 4b51f1d2733..9dabbec4a5c 100644--- a/e2e/test/scenarios/visualizer/cartesian.cy.spec.ts+++ b/e2e/test/scenarios/visualizer/cartesian.cy.spec.ts@@ -581,7 +581,7 @@ describe("scenarios > visualizer > cartesian", () => {});H.horizontalWell().findByText("Product → Category").should("exist");- cy.findByDisplayValue("bar").should("be.checked");+ H.assertCurrentVisualization("bar");H.echartsContainer().within(() => {// x-axis labelsdiff --git a/frontend/src/metabase/dashboard/components/DashboardChartSettings/DashboardChartSettings.unit.spec.tsx b/frontend/src/metabase/dashboard/components/DashboardChartSettings/DashboardChartSettings.unit.spec.tsxindex f67f372bf8a..243f02036ad 100644--- a/frontend/src/metabase/dashboard/components/DashboardChartSettings/DashboardChartSettings.unit.spec.tsx+++ b/frontend/src/metabase/dashboard/components/DashboardChartSettings/DashboardChartSettings.unit.spec.tsx@@ -135,7 +135,7 @@ describe("DashboardChartSettings trend line customization", () => {await userEvent.click(within(screen.getByTestId("chart-settings-widget-graph.trendline_style"),- ).getByRole("button", { name: /line_style_dashed/i }),+ ).getByRole("radio", { name: "Dashed" }),);await userEvent.click(screen.getByRole("button", { name: "Done" }));diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.module.css b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.module.cssdeleted file mode 100644index ce48e737be1..00000000000--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.module.css+++ /dev/null@@ -1,22 +0,0 @@-.root {- display: flex;- align-items: center;- width: 100%;- overflow: hidden;- border: 1px solid var(--mb-color-button-filled-brand-default);- border-radius: 6px;- background-color: var(--mb-color-background_page-primary);-}--.root .item[data-variant] {- flex: 1 0 0;- min-width: 0;- height: 38px;- border-width: 0;- border-radius: 0;- box-shadow: none;-- &:not(:first-child) {- border-inline-start: 1px solid var(--mb-color-button-filled-brand-default);- }-}diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.tsxindex 973b32f31f8..0d2c8503a62 100644--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.tsx+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.tsx@@ -1,25 +1,44 @@-import { Box, Button, Icon } from "metabase/ui";-import type { ChartSettingSegmentedControlProps } from "metabase/viz-core";--import S from "./ChartSettingSegmentedControl.module.css";+import { SegmentedControl, type SegmentedControlItem } from "metabase/ui";+import {+ type ChartSettingSegmentedControlOption,+ type ChartSettingSegmentedControlProps,+ encodeWidgetValue,+} from "metabase/viz-core";export const ChartSettingSegmentedControl = ({options,onChange,value,-}: ChartSettingSegmentedControlProps) => (- <Box className={S.root}>- {options.map((elem) => (- <Button- className={S.item}- px="xxs"- variant={value === elem.value ? "filled" : "default"}- key={elem.value}- leftSection={elem.icon ? <Icon name={elem.icon} /> : undefined}- onClick={() => onChange(elem.value)}- >- {elem.icon ? null : elem.name}- </Button>- ))}- </Box>-);+}: ChartSettingSegmentedControlProps) => {+ const handleChange = (encodedValue: string) => {+ const option = options.find(+ (option) => encodeWidgetValue(option.value) === encodedValue,+ );+ if (option) {+ onChange(option.value);+ }+ };++ return (+ <SegmentedControl+ fullWidth+ data={options.map(getSegmentedControlItem)}+ value={encodeWidgetValue(value)}+ onChange={handleChange}+ />+ );+};++function getSegmentedControlItem({+ name,+ value,+ icon,+}: ChartSettingSegmentedControlOption): SegmentedControlItem<string> {+ const encodedValue = encodeWidgetValue(value);++ if (icon === undefined) {+ return { value: encodedValue, label: name };+ }++ return { value: encodedValue, icon, ariaLabel: name };+}diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.unit.spec.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.unit.spec.tsxindex bf3bed8e569..250c560de0c 100644--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.unit.spec.tsx+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentedControl/ChartSettingSegmentedControl.unit.spec.tsx@@ -1,21 +1,26 @@import userEvent from "@testing-library/user-event";import { render, screen } from "__support__/ui";+import type {+ ChartSettingSegmentedControlOption,+ ChartSettingSegmentedControlProps,+} from "metabase/viz-core";import { ChartSettingSegmentedControl } from "./ChartSettingSegmentedControl";-const defaultOptions = [- { name: "Option 1", value: "value1" },- { name: "Option 2", value: "value2" },- { name: "Option 3", value: "value3" },-];+interface SetupOpts {+ options: ChartSettingSegmentedControlOption[];+ value: ChartSettingSegmentedControlProps["value"];+}-function setup() {- const value = "value1";- const onChange = jest.fn();+function setup({ options, value }: SetupOpts) {+ const onChange = jest.fn<+ void,+ [ChartSettingSegmentedControlProps["value"]]+ >();render(<ChartSettingSegmentedControl- options={defaultOptions}+ options={options}value={value}onChange={onChange}/>,@@ -25,42 +30,57 @@ function setup() {}describe("ChartSettingSegmentedControl", () => {- it("should render all options", () => {- setup();-- const buttons = screen.getAllByRole("button");- expect(buttons).toHaveLength(defaultOptions.length);-- defaultOptions.forEach((option, index) => {- expect(buttons[index]).toHaveTextContent(option.name);+ it("should show the selected option and report the clicked one", async () => {+ const { onChange } = setup({+ options: [+ { name: "Auto", value: "auto" },+ { name: "Compact", value: "compact" },+ { name: "Full", value: "full" },+ ],+ value: "auto",});- });- it("should call onChange when clicking an option", async () => {- const { onChange } = setup();- const user = userEvent.setup();+ expect(screen.getByRole("radio", { name: "Auto" })).toBeChecked();+ expect(screen.getByRole("radio", { name: "Compact" })).not.toBeChecked();- const buttons = screen.getAllByRole("button");- await user.click(buttons[1]);+ await userEvent.click(screen.getByText("Compact"));expect(onChange).toHaveBeenCalledTimes(1);- expect(onChange).toHaveBeenCalledWith(defaultOptions[1].value);+ expect(onChange).toHaveBeenCalledWith("compact");});- it("should handle keyboard navigation and selection", async () => {- const { onChange } = setup();+ it("should support boolean and null option values", async () => {+ const { onChange } = setup({+ options: [+ { name: "Auto", value: null },+ { name: "On", value: true },+ { name: "Off", value: false },+ ],+ value: false,+ });++ expect(screen.getByRole("radio", { name: "Off" })).toBeChecked();++ await userEvent.click(screen.getByText("On"));+ expect(onChange).toHaveBeenLastCalledWith(true);- const buttons = screen.getAllByRole("button");- await userEvent.tab();- expect(buttons[0]).toHaveFocus();+ await userEvent.click(screen.getByText("Auto"));+ expect(onChange).toHaveBeenLastCalledWith(null);+ });++ it("should name icon options by their name without a tooltip", async () => {+ setup({+ options: [+ { name: "Solid", value: "solid", icon: "line_style_solid" },+ { name: "Dashed", value: "dashed", icon: "line_style_dashed" },+ ],+ value: "solid",+ });- await userEvent.keyboard("{Enter}");- expect(onChange).toHaveBeenCalledWith(defaultOptions[0].value);+ expect(screen.getByRole("radio", { name: "Solid" })).toBeChecked();- await userEvent.tab();- expect(buttons[1]).toHaveFocus();+ await userEvent.hover(screen.getByRole("img", { name: "Dashed" }));- await userEvent.keyboard(" ");- expect(onChange).toHaveBeenCalledWith(defaultOptions[1].value);+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();});});diff --git a/frontend/src/metabase/visualizations/components/settings/GraphTrendLineSettings.unit.spec.tsx b/frontend/src/metabase/visualizations/components/settings/GraphTrendLineSettings.unit.spec.tsxindex 66f079eca22..b20a69bf4f6 100644--- a/frontend/src/metabase/visualizations/components/settings/GraphTrendLineSettings.unit.spec.tsx+++ b/frontend/src/metabase/visualizations/components/settings/GraphTrendLineSettings.unit.spec.tsx@@ -64,7 +64,7 @@ const getColorWidget = () =>screen.getByTestId("chart-settings-widget-graph.trendline_color");const getDashedOption = () =>- within(getStyleWidget()).getByRole("button", { name: /line_style_dashed/i });+ within(getStyleWidget()).getByRole("radio", { name: "Dashed" });describe("graph trend line settings for a single series", () => {describe("in the query builder", () => {@@ -83,11 +83,7 @@ describe("graph trend line settings for a single series", () => {it("should write the style into the series settings under the metric key", async () => {const { onChange } = setup();- await userEvent.click(- within(getStyleWidget()).getByRole("img", {- name: /line_style_dashed/i,- }),- );+ await userEvent.click(getDashedOption());const [settings] = onChange.mock.lastCall ?? [];expect(settings).toMatchObject({@@ -134,7 +130,7 @@ describe("graph trend line settings for a single series", () => {expect(within(getColorWidget()).getByLabelText(CUSTOM_COLOR),).toBeInTheDocument();- expect(getDashedOption()).toHaveAttribute("data-variant", "filled");+ expect(getDashedOption()).toBeChecked();});});});diff --git a/frontend/src/metabase/viz-core/index.ts b/frontend/src/metabase/viz-core/index.tsindex 6ce57aad844..c6fa657e7a2 100644--- a/frontend/src/metabase/viz-core/index.ts+++ b/frontend/src/metabase/viz-core/index.ts@@ -498,6 +498,7 @@ export type {ChartSettingGoalInputProps,ChartSettingMaxCategoriesProps,ChartSettingOrderedItem,+ ChartSettingSegmentedControlOption,ChartSettingSegmentedControlProps,ChartSettingSegmentsEditorProps,ChartSettingSeriesOrderItem,diff --git a/frontend/src/metabase/viz-core/lib/settings/graph.ts b/frontend/src/metabase/viz-core/lib/settings/graph.tsindex b80a041a880..eeaa087eb0a 100644--- a/frontend/src/metabase/viz-core/lib/settings/graph.ts+++ b/frontend/src/metabase/viz-core/lib/settings/graph.ts@@ -62,6 +62,7 @@ import type {import type {ChartSettingEnumToggleProps,ChartSettingSegmentedControlProps,+ ChartSettingSegmentedControlValue,} from "../../types/widget-props";import { dimensionIsNumeric } from "../numeric";import { getMaxDimensionsSupported, getMaxMetricsSupported } from "../registry";@@ -550,8 +551,8 @@ export const GRAPH_TREND_SETTINGS: VisualizationSettingsDefinitions = {icon: "line_style_dotted" as const,},],- onChange: (value: string) => {- if (isLineStyle(value)) {+ onChange: (value: ChartSettingSegmentedControlValue) => {+ if (typeof value === "string" && isLineStyle(value)) {updateSingleSeriesTrendLineSetting(series,vizSettings,diff --git a/frontend/src/metabase/viz-core/lib/settings/series.ts b/frontend/src/metabase/viz-core/lib/settings/series.tsindex 7136ce33fdd..fc8803502af 100644--- a/frontend/src/metabase/viz-core/lib/settings/series.ts+++ b/frontend/src/metabase/viz-core/lib/settings/series.ts@@ -106,9 +106,9 @@ export function seriesSetting({},getProps: () => ({options: [- { value: "line", icon: "line" },- { value: "area", icon: "area" },- { value: "bar", icon: "bar" },+ { name: t`Line`, value: "line", icon: "line" },+ { name: t`Area`, value: "area", icon: "area" },+ { name: t`Bar`, value: "bar", icon: "bar" },],}),getHidden: (single, settings, _extra) =>@@ -147,9 +147,9 @@ export function seriesSetting({widget: "segmentedControl",getProps: () => ({options: [- { icon: "straight", value: "linear" },- { icon: "curved", value: "cardinal" },- { icon: "stepped", value: "step-after" },+ { name: t`Straight`, value: "linear", icon: "straight" },+ { name: t`Curved`, value: "cardinal", icon: "curved" },+ { name: t`Stepped`, value: "step-after", icon: "stepped" },],}),getHidden: (_single, settings) =>@@ -167,9 +167,9 @@ export function seriesSetting({widget: "segmentedControl",getProps: () => ({options: [- { icon: "line_style_solid", value: "solid" },- { icon: "line_style_dashed", value: "dashed" },- { icon: "line_style_dotted", value: "dotted" },+ { name: t`Solid`, value: "solid", icon: "line_style_solid" },+ { name: t`Dashed`, value: "dashed", icon: "line_style_dashed" },+ { name: t`Dotted`, value: "dotted", icon: "line_style_dotted" },],}),getDefault: (_series, settings) => getSeriesDefaultLineStyle(settings),diff --git a/frontend/src/metabase/viz-core/types/widget-props.ts b/frontend/src/metabase/viz-core/types/widget-props.tsindex d0a4668f890..13e90b3e91b 100644--- a/frontend/src/metabase/viz-core/types/widget-props.ts+++ b/frontend/src/metabase/viz-core/types/widget-props.ts@@ -36,10 +36,18 @@ export type ChartSettingEnumToggleProps<T extends string> = {uncheckedValue: T;};+export type ChartSettingSegmentedControlValue = string | boolean | null;++export type ChartSettingSegmentedControlOption = {+ name: string;+ value: ChartSettingSegmentedControlValue;+ icon?: IconName;+};+export type ChartSettingSegmentedControlProps = {- options: { name: string; value: string; icon?: IconName }[];- onChange: (value: string) => void;- value: string;+ options: ChartSettingSegmentedControlOption[];+ onChange: (value: ChartSettingSegmentedControlValue) => void;+ value: ChartSettingSegmentedControlValue;};export type ChartSettingGoalInputProps = {