Skip to content

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

Remove deprecated radio form (#64679)

Nemanja Glumac645543ff3e14
diff --git a/frontend/src/metabase/actions/components/ActionFormFieldWidget/ActionFormFieldWidget.tsx b/frontend/src/metabase/actions/components/ActionFormFieldWidget/ActionFormFieldWidget.tsx
index 52a12a1e683..e5830287cd7 100644
--- a/frontend/src/metabase/actions/components/ActionFormFieldWidget/ActionFormFieldWidget.tsx
+++ b/frontend/src/metabase/actions/components/ActionFormFieldWidget/ActionFormFieldWidget.tsx
@@ -4,16 +4,43 @@ import { forwardRef } from "react";
import type { ActionFormFieldProps } from "metabase/actions/types";
import FormInputWidget from "metabase/common/components/FormInput";
import FormNumericInputWidget from "metabase/common/components/FormNumericInput";
-import type { FormRadioProps } from "metabase/common/components/FormRadio";
-import FormRadioWidget from "metabase/common/components/FormRadio";
import FormSelectWidget from "metabase/common/components/FormSelect";
import FormTextAreaWidget from "metabase/common/components/FormTextArea";
import FormBooleanWidget from "metabase/common/components/FormToggle";
+import { FormRadioGroup, type FormRadioGroupProps } from "metabase/forms";
+import { Radio, Stack } from "metabase/ui";
import type { InputComponentType } from "metabase-types/api";
-const VerticalRadio = (props: FormRadioProps) => (
- <FormRadioWidget {...props} vertical />
-);
+type RadioOption = {
+ name: string;
+ // Radio.Group in our forms provides string values; stringify numeric values when rendering
+ value: string | number;
+};
+
+interface FormRadioProps extends FormRadioGroupProps {
+ title?: string;
+ options: RadioOption[];
+ actions?: ReactNode;
+ description?: ReactNode;
+ optional?: boolean;
+}
+
+const VerticalRadio = (props: FormRadioProps) => {
+ return (
+ <FormRadioGroup name={props.name} label={props.title}>
+ <Stack gap="sm" mt="xs">
+ {props.options.map((r) => (
+ // The value that comes from the `FormRadioGroup` wrapper is a string.
+ // It is crucial to match it properly or otherwise a radio button will
+ // not render as checked/selected.
+ // This is important because this component is used to render "Inline select"
+ // for both the string and the numeric variables!
+ <Radio label={r.name} key={r.value} value={r.value.toString()} />
+ ))}
+ </Stack>
+ </FormRadioGroup>
+ );
+};
const WIDGETS: Record<InputComponentType, FunctionComponent<any>> = {
text: FormInputWidget,
diff --git a/frontend/src/metabase/common/components/FormRadio/FormRadio.stories.tsx b/frontend/src/metabase/common/components/FormRadio/FormRadio.stories.tsx
deleted file mode 100644
index c03baf93373..00000000000
--- a/frontend/src/metabase/common/components/FormRadio/FormRadio.stories.tsx
+++ /dev/null
@@ -1,47 +0,0 @@
-import type { StoryFn } from "@storybook/react";
-
-import { Form, FormProvider } from "metabase/forms";
-
-import FormRadio from "./FormRadio";
-
-const TEST_OPTIONS = [
- { name: "Line", value: "line" },
- { name: "Area", value: "area" },
- { name: "Bar", value: "bar" },
-];
-
-export default {
- title: "Components/Ask Before Using/FormRadio",
- component: FormRadio,
-};
-
-// TODO: mark this story as deprecated
-const Template: StoryFn<typeof FormRadio> = (args) => {
- const initialValues = { value: undefined };
- const handleSubmit = () => undefined;
-
- return (
- <FormProvider initialValues={initialValues} onSubmit={handleSubmit}>
- <Form>
- <FormRadio {...args} name="value" options={TEST_OPTIONS} />
- </Form>
- </FormProvider>
- );
-};
-
-export const Default = {
- render: Template,
-
- args: {
- title: "Title",
- },
-};
-
-export const WithDescription = {
- render: Template,
-
- args: {
- title: "Title",
- description: "Description",
- },
-};
diff --git a/frontend/src/metabase/common/components/FormRadio/FormRadio.tsx b/frontend/src/metabase/common/components/FormRadio/FormRadio.tsx
deleted file mode 100644
index b20cf421484..00000000000
--- a/frontend/src/metabase/common/components/FormRadio/FormRadio.tsx
+++ /dev/null
@@ -1,67 +0,0 @@
-import { useField } from "formik";
-import type { Key, ReactNode, Ref } from "react";
-import { forwardRef } from "react";
-
-import FormField from "metabase/common/components/FormField";
-import type { RadioOption, RadioProps } from "metabase/common/components/Radio";
-import Radio from "metabase/common/components/Radio";
-
-export interface FormRadioProps<
- TValue extends Key = string,
- TOption = RadioOption<TValue>,
-> extends Omit<
- RadioProps<TValue, TOption>,
- "value" | "error" | "onChange" | "onBlur"
- > {
- name: string;
- title?: string;
- actions?: ReactNode;
- description?: ReactNode;
- optional?: boolean;
-}
-
-/**
- * @deprecated: use FormRadioGroup from "metabase/forms"
- */
-const FormRadio = forwardRef(function FormRadio<
- TValue extends Key,
- TOption = RadioOption<TValue>,
->(
- {
- name,
- className,
- style,
- title,
- actions,
- description,
- optional,
- ...props
- }: FormRadioProps<TValue, TOption>,
- ref: Ref<HTMLDivElement>,
-) {
- const [{ value, onBlur }, { error, touched }, { setValue }] = useField(name);
-
- return (
- <FormField
- ref={ref}
- className={className}
- style={style}
- title={title}
- actions={actions}
- description={description}
- error={touched ? error : undefined}
- optional={optional}
- >
- <Radio
- {...props}
- name={name}
- value={value}
- onChange={setValue}
- onBlur={onBlur}
- />
- </FormField>
- );
-});
-
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default FormRadio;
diff --git a/frontend/src/metabase/common/components/FormRadio/FormRadio.unit.spec.tsx b/frontend/src/metabase/common/components/FormRadio/FormRadio.unit.spec.tsx
deleted file mode 100644
index 6994c193786..00000000000
--- a/frontend/src/metabase/common/components/FormRadio/FormRadio.unit.spec.tsx
+++ /dev/null
@@ -1,70 +0,0 @@
-import userEvent from "@testing-library/user-event";
-import { Form, Formik } from "formik";
-import * as Yup from "yup";
-
-import { render, screen, waitFor } from "__support__/ui";
-
-import FormRadio from "./FormRadio";
-
-const TEST_SCHEMA = Yup.object({
- value: Yup.string().notOneOf(["bar"], "error"),
-});
-
-const TEST_OPTIONS = [
- { name: "Line", value: "line" },
- { name: "Area", value: "area" },
- { name: "Bar", value: "bar" },
-];
-
-interface TestFormRadioProps {
- initialValue?: string;
- onSubmit: () => void;
-}
-
-const TestFormRadio = ({ initialValue, onSubmit }: TestFormRadioProps) => {
- return (
- <Formik
- initialValues={{ value: initialValue }}
- validationSchema={TEST_SCHEMA}
- onSubmit={onSubmit}
- >
- <Form>
- <FormRadio name="value" options={TEST_OPTIONS} title="Label" />
- <button type="submit">Submit</button>
- </Form>
- </Formik>
- );
-};
-
-describe("FormRadio", () => {
- it("should use the initial value from the form", () => {
- const onSubmit = jest.fn();
-
- render(<TestFormRadio initialValue="line" onSubmit={onSubmit} />);
-
- expect(screen.getByRole("radio", { name: "Line" })).toBeChecked();
- });
-
- it("should propagate the changed value to the form", async () => {
- const onSubmit = jest.fn();
-
- render(<TestFormRadio onSubmit={onSubmit} />);
- await userEvent.click(screen.getByRole("radio", { name: "Line" }));
- await userEvent.click(screen.getByText("Submit"));
-
- await waitFor(() => {
- const values = { value: "line" };
- expect(onSubmit).toHaveBeenCalledWith(values, expect.anything());
- });
- });
-
- it("should be validated on blur", async () => {
- const onSubmit = jest.fn();
-
- render(<TestFormRadio initialValue="line" onSubmit={onSubmit} />);
- await userEvent.click(screen.getByRole("radio", { name: "Bar" }));
- await userEvent.tab();
-
- expect(await screen.findByText(": error")).toBeInTheDocument();
- });
-});
diff --git a/frontend/src/metabase/common/components/FormRadio/index.ts b/frontend/src/metabase/common/components/FormRadio/index.ts
deleted file mode 100644
index 64ba2499a7f..00000000000
--- a/frontend/src/metabase/common/components/FormRadio/index.ts
+++ /dev/null
@@ -1,3 +0,0 @@
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export { default } from "./FormRadio";
-export type { FormRadioProps } from "./FormRadio";