Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor(web): Clean up `RadioGroup` (#16939)

Ben Bachem03ae4fa07d4c
diff --git a/web/.storybook/main.ts b/web/.storybook/main.ts
index 3089ee8e74..0d2e88a99b 100644
--- a/web/.storybook/main.ts
+++ b/web/.storybook/main.ts
@@ -23,6 +23,7 @@ const DESIGN_COMPONENT_STORIES = [
"LangfuseLogo/LangfuseLogo",
"PasswordInput/PasswordInput",
"Progress/Progress",
+ "RadioGroup/RadioGroup",
"SearchInput/SearchInput",
"Spinner/Spinner",
"Switch/Switch",
diff --git a/web/src/components/design-system/RadioGroup/RadioGroup.stories.tsx b/web/src/components/design-system/RadioGroup/RadioGroup.stories.tsx
new file mode 100644
index 0000000000..49b5f81dd7
--- /dev/null
+++ b/web/src/components/design-system/RadioGroup/RadioGroup.stories.tsx
@@ -0,0 +1,93 @@
+import { expect, fn, userEvent, within } from "storybook/test";
+
+import preview from "../../../../.storybook/preview";
+import { RadioGroup } from "./RadioGroup";
+
+const meta = preview.meta({
+ component: RadioGroup,
+ args: {
+ onValueChange: fn(),
+ },
+});
+
+const options = [
+ { value: "apple", label: "Apple" },
+ { value: "banana", label: "Banana" },
+ { value: "cherry", label: "Cherry" },
+] as const;
+
+function LabeledItems({
+ disabled = false,
+ idPrefix,
+}: {
+ disabled?: boolean;
+ idPrefix: string;
+}) {
+ return (
+ <>
+ {options.map((option) => {
+ const id = `${idPrefix}-${option.value}`;
+
+ return (
+ <label
+ key={option.value}
+ className="flex items-center gap-2"
+ htmlFor={id}
+ >
+ <RadioGroup.Item disabled={disabled} id={id} value={option.value} />
+ {option.label}
+ </label>
+ );
+ })}
+ </>
+ );
+}
+
+export const Default = meta.story({
+ args: {
+ children: <LabeledItems idPrefix="default" />,
+ defaultValue: "apple",
+ },
+});
+
+export const Disabled = meta.story({
+ args: {
+ children: <LabeledItems disabled idPrefix="disabled" />,
+ defaultValue: "apple",
+ },
+});
+
+export const TestSelectsOption = meta.story({
+ name: "(Test) Selects Option",
+ args: {
+ children: <LabeledItems idPrefix="selects-option" />,
+ defaultValue: "apple",
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const banana = canvas.getByRole("radio", { name: "Banana" });
+
+ await expect(canvas.getByRole("radio", { name: "Apple" })).toBeChecked();
+ await expect(banana).not.toBeChecked();
+ await userEvent.click(banana);
+ await expect(banana).toBeChecked();
+ await expect(args.onValueChange).toHaveBeenCalledWith("banana");
+ },
+});
+
+export const TestDoesNotSelectDisabled = meta.story({
+ name: "(Test) Does Not Select Disabled",
+ args: {
+ children: <LabeledItems disabled idPrefix="disabled-option" />,
+ defaultValue: "apple",
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const banana = canvas.getByRole("radio", { name: "Banana" });
+
+ await userEvent.click(banana);
+ await expect(banana).not.toBeChecked();
+ await expect(canvas.getByRole("radio", { name: "Apple" })).toBeChecked();
+ await expect(args.onValueChange).not.toHaveBeenCalled();
+ },
+});
diff --git a/web/src/components/design-system/RadioGroup/RadioGroup.tsx b/web/src/components/design-system/RadioGroup/RadioGroup.tsx
new file mode 100644
index 0000000000..229f9c0c83
--- /dev/null
+++ b/web/src/components/design-system/RadioGroup/RadioGroup.tsx
@@ -0,0 +1,48 @@
+"use client";
+
+import * as React from "react";
+import * as RadioGroupPrimitive from "@radix-ui/react-radio-group";
+
+type RadioGroupProps = Pick<
+ React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root>,
+ "children" | "defaultValue" | "onValueChange" | "value"
+>;
+
+const RadioGroupRoot = React.forwardRef<
+ React.ComponentRef<typeof RadioGroupPrimitive.Root>,
+ RadioGroupProps
+>((props, ref) => {
+ return (
+ <RadioGroupPrimitive.Root className="grid gap-2" {...props} ref={ref} />
+ );
+});
+RadioGroupRoot.displayName = RadioGroupPrimitive.Root.displayName;
+
+type RadioGroupItemProps = Pick<
+ React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>,
+ "aria-controls" | "disabled" | "id" | "value"
+>;
+
+const RadioGroupItem = React.forwardRef<
+ React.ComponentRef<typeof RadioGroupPrimitive.Item>,
+ RadioGroupItemProps
+>((props, ref) => {
+ return (
+ <RadioGroupPrimitive.Item
+ ref={ref}
+ className="border-control-border focus-visible:ring-ring data-[state=checked]:border-control-fill aspect-square h-4 w-4 rounded-full border shadow-sm focus:outline-hidden focus-visible:ring-1 disabled:cursor-not-allowed disabled:opacity-50"
+ {...props}
+ >
+ <RadioGroupPrimitive.Indicator className="flex items-center justify-center">
+ <div className="bg-control-fill h-2 w-2 rounded-full" />
+ </RadioGroupPrimitive.Indicator>
+ </RadioGroupPrimitive.Item>
+ );
+});
+RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName;
+
+const RadioGroup = Object.assign(RadioGroupRoot, {
+ Item: RadioGroupItem,
+});
+
+export { RadioGroup };
diff --git a/web/src/components/ui/radio-group.tsx b/web/src/components/ui/radio-group.tsx
deleted file mode 100644
index 9028a7191e..0000000000
--- a/web/src/components/ui/radio-group.tsx
+++ /dev/null
@@ -1,43 +0,0 @@
-/* eslint-disable @repo/no-style-props */
-"use client";
-
-import * as React from "react";
-import * as RadioGroupPrimitive from "@radix-ui/react-radio-group";
-import { cn } from "@/src/utils/tailwind";
-
-const RadioGroup = React.forwardRef<
- React.ComponentRef<typeof RadioGroupPrimitive.Root>,
- React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Root>
->(({ className, ...props }, ref) => {
- return (
- <RadioGroupPrimitive.Root
- className={cn("grid gap-2", className)}
- {...props}
- ref={ref}
- />
- );
-});
-RadioGroup.displayName = RadioGroupPrimitive.Root.displayName;
-
-const RadioGroupItem = React.forwardRef<
- React.ComponentRef<typeof RadioGroupPrimitive.Item>,
- React.ComponentPropsWithoutRef<typeof RadioGroupPrimitive.Item>
->(({ className, ...props }, ref) => {
- return (
- <RadioGroupPrimitive.Item
- ref={ref}
- className={cn(
- "border-control-border focus-visible:ring-ring data-[state=checked]:border-control-fill aspect-square h-4 w-4 rounded-full border shadow-sm focus:outline-hidden focus-visible:ring-1 disabled:cursor-not-allowed disabled:opacity-50",
- className,
- )}
- {...props}
- >
- <RadioGroupPrimitive.Indicator className="flex items-center justify-center">
- <div className="bg-control-fill h-2 w-2 rounded-full" />
- </RadioGroupPrimitive.Indicator>
- </RadioGroupPrimitive.Item>
- );
-});
-RadioGroupItem.displayName = RadioGroupPrimitive.Item.displayName;
-
-export { RadioGroup, RadioGroupItem };
diff --git a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx
index 67c6d1da7e..2d9f4e8103 100644
--- a/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx
+++ b/web/src/features/batch-actions/components/AddObservationsToDatasetDialog/components/MappingModeSelector.tsx
@@ -1,5 +1,5 @@
import { Label } from "@/src/components/ui/label";
-import { RadioGroup, RadioGroupItem } from "@/src/components/ui/radio-group";
+import { RadioGroup } from "@/src/components/design-system/RadioGroup/RadioGroup";
import type { MappingMode } from "../types";
type MappingModeSelectorProps = {
@@ -18,7 +18,7 @@ export function MappingModeSelector({
return (
<RadioGroup value={value} onValueChange={(v) => onChange(v as MappingMode)}>
<div className="hover:bg-muted/50 flex items-center space-x-3 rounded-md border px-3">
- <RadioGroupItem value="full" id={`${fieldName}-full`} />
+ <RadioGroup.Item value="full" id={`${fieldName}-full`} />
<Label
htmlFor={`${fieldName}-full`}
className="flex-1 cursor-pointer py-3 text-sm font-bold"
@@ -27,7 +27,7 @@ export function MappingModeSelector({
</Label>
</div>
<div className="hover:bg-muted/50 flex items-center space-x-3 rounded-md border px-3">
- <RadioGroupItem value="custom" id={`${fieldName}-custom`} />
+ <RadioGroup.Item value="custom" id={`${fieldName}-custom`} />
<Label
htmlFor={`${fieldName}-custom`}
className="flex-1 cursor-pointer py-3 text-sm font-bold"
@@ -37,7 +37,7 @@ export function MappingModeSelector({
</div>
{fieldName !== "input" && (
<div className="hover:bg-muted/50 flex items-center space-x-3 rounded-md border px-3">
- <RadioGroupItem
+ <RadioGroup.Item
value="none"
id={`${fieldName}-none`}
disabled={fieldName === "input"}
diff --git a/web/src/features/evals/v2/components/Evaluators/EvaluatorSavedDialog/EvaluatorSavedDialog.tsx b/web/src/features/evals/v2/components/Evaluators/EvaluatorSavedDialog/EvaluatorSavedDialog.tsx
index a1e2a9d2ed..a5fdd50f16 100644
--- a/web/src/features/evals/v2/components/Evaluators/EvaluatorSavedDialog/EvaluatorSavedDialog.tsx
+++ b/web/src/features/evals/v2/components/Evaluators/EvaluatorSavedDialog/EvaluatorSavedDialog.tsx
@@ -13,7 +13,7 @@ import {
DialogHeader,
DialogTitle,
} from "@/src/components/ui/dialog";
-import { RadioGroup, RadioGroupItem } from "@/src/components/ui/radio-group";
+import { RadioGroup } from "@/src/components/design-system/RadioGroup/RadioGroup";
import {
Tooltip,
TooltipContent,
@@ -110,12 +110,13 @@ export function EvaluatorSavedDialog({
)}
>
<div className="flex items-start gap-2">
- <RadioGroupItem
- id={id}
- value={option.value}
- aria-controls={contentId}
- className="mt-0.5"
- />
+ <div className="mt-0.5">
+ <RadioGroup.Item
+ id={id}
+ value={option.value}
+ aria-controls={contentId}
+ />
+ </div>
<div className="min-w-0 flex-1">
<label
htmlFor={id}
diff --git a/web/src/features/prompts/components/duplicate-folder.tsx b/web/src/features/prompts/components/duplicate-folder.tsx
index c9c2359c3d..30732346b8 100644
--- a/web/src/features/prompts/components/duplicate-folder.tsx
+++ b/web/src/features/prompts/components/duplicate-folder.tsx
@@ -22,7 +22,7 @@ import {
FormLabel,
FormMessage,
} from "@/src/components/ui/form";
-import { RadioGroup, RadioGroupItem } from "@/src/components/ui/radio-group";
+import { RadioGroup } from "@/src/components/design-system/RadioGroup/RadioGroup";
import { Checkbox } from "@/src/components/design-system/Checkbox/Checkbox";
import { useForm } from "react-hook-form";
import { z } from "zod/v4";
@@ -162,14 +162,13 @@ export function DuplicateFolder({ folderPath }: { folderPath: string }) {
<FormLabel>Version settings</FormLabel>
<FormControl>
<RadioGroup
- {...field}
+ value={field.value}
onValueChange={field.onChange}
defaultValue={field.value}
- className="flex flex-col space-y-1"
>
<FormItem className="flex items-center space-y-0 space-x-3">
<FormControl>
- <RadioGroupItem value={CopySettings.LATEST_ONLY} />
+ <RadioGroup.Item value={CopySettings.LATEST_ONLY} />
</FormControl>
<FormLabel className="font-normal">
Copy only the latest version of each prompt
@@ -177,7 +176,9 @@ export function DuplicateFolder({ folderPath }: { folderPath: string }) {
</FormItem>
<FormItem className="flex items-center space-y-0 space-x-3">
<FormControl>
- <RadioGroupItem value={CopySettings.ALL_VERSIONS} />
+ <RadioGroup.Item
+ value={CopySettings.ALL_VERSIONS}
+ />
</FormControl>
<FormLabel className="font-normal">
Copy all versions and labels
diff --git a/web/src/features/prompts/components/duplicate-prompt.tsx b/web/src/features/prompts/components/duplicate-prompt.tsx
index 9885501da5..746cee83ff 100644
--- a/web/src/features/prompts/components/duplicate-prompt.tsx
+++ b/web/src/features/prompts/components/duplicate-prompt.tsx
@@ -29,7 +29,7 @@ import { useForm } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { Input } from "@/src/components/ui/input";
-import { RadioGroup, RadioGroupItem } from "@/src/components/ui/radio-group";
+import { RadioGroup } from "@/src/components/design-system/RadioGroup/RadioGroup";
import { usePromptNameValidation } from "@/src/features/prompts/hooks/usePromptNameValidation";
enum CopySettings {
@@ -132,14 +132,13 @@ const DuplicatePromptForm: React.FC<{
<FormLabel>Settings</FormLabel>
<FormControl>
<RadioGroup
- {...field}
+ value={field.value}
onValueChange={field.onChange}
defaultValue={field.value}
- className="flex flex-col space-y-1"
>
<FormItem className="flex items-center space-y-0 space-x-3">
<FormControl>
- <RadioGroupItem value={CopySettings.SINGLE_VERSION} />
+ <RadioGroup.Item value={CopySettings.SINGLE_VERSION} />
</FormControl>
<FormLabel className="font-normal">
Copy only version {promptVersion}
@@ -147,7 +146,7 @@ const DuplicatePromptForm: React.FC<{
</FormItem>
<FormItem className="flex items-center space-y-0 space-x-3">
<FormControl>
- <RadioGroupItem value={CopySettings.ALL_VERSIONS} />
+ <RadioGroup.Item value={CopySettings.ALL_VERSIONS} />
</FormControl>
<FormLabel className="font-normal">
Copy all prompt versions and labels
diff --git a/web/src/features/support-chat/SupportForm.tsx b/web/src/features/support-chat/SupportForm.tsx
index ccf87c0267..a20d2cfd93 100644
--- a/web/src/features/support-chat/SupportForm.tsx
+++ b/web/src/features/support-chat/SupportForm.tsx
@@ -28,7 +28,6 @@ import {
FormLabel,
FormMessage,
} from "@/src/components/ui/form";
-import { RadioGroup } from "@/src/components/ui/radio-group";
import {
AlertDialog,
AlertDialogAction,
@@ -329,11 +328,7 @@ export function SupportForm({
<FormItem>
<FormLabel>Message Type</FormLabel>
<FormControl>
- <RadioGroup
- className="grid grid-cols-3 gap-2"
- value={field.value ?? "Question"}
- onValueChange={field.onChange}
- >
+ <div className="grid grid-cols-3 gap-2">
{MESSAGE_TYPES.map((v) => (
<Button
key={v}
@@ -347,7 +342,7 @@ export function SupportForm({
</span>
</Button>
))}
- </RadioGroup>
+ </div>
</FormControl>
<FormDescription className="sr-only">
Choose the type of your message.