RadioGroup
Basic control to allow selecting only one item from a set.
Want to skip the docs? Try our MCP Server
#
Documentation
#
#
Usage
#
Use RadioGroup and Radio components together to create sets of radio buttons.
"use client";
import type { ComponentPropsWithoutRef } from "react";
import { Field, Radio, RadioGroup } from "@optiaxiom/react";
export function App({
description,
error,
label = "Input label",
required = false,
}: Pick<
ComponentPropsWithoutRef<typeof Field>,
"description" | "error" | "label" | "required"
>) {
return (
<Field
description={description}
error={error}
label={label}
required={required}
>
<RadioGroup name="usage">
<Radio value="one">Option One</Radio>
<Radio value="two">Option Two</Radio>
<Radio value="three">Option Three</Radio>
</RadioGroup>
</Field>
);
}#
Label
#
The children prop on Radio is used as the label of the radio control.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup name="label-usage">
<Radio value="one" />
<Radio value="two">Option Two</Radio>
</RadioGroup>
);
}#
Controlled
#
Use the value and defaultValue props on RadioGroup to toggle between controlled and uncontrolled usage. And combine it with onValueChange to listen for changes to the value.
"use client";
import { Button, Group, Radio, RadioGroup } from "@optiaxiom/react";
import { useState } from "react";
export function App() {
const [value, setValue] = useState("");
return (
<Group flexDirection="column" gap="16">
<RadioGroup
name="controlled-usage"
onValueChange={setValue}
value={value}
>
<Radio value="one">Option One</Radio>
<Radio value="two">Option Two</Radio>
<Radio value="three">Option Three</Radio>
</RadioGroup>
<Button disabled={!value} onClick={() => setValue("")}>
Reset
</Button>
</Group>
);
}#
Form integration
#
Use Controller from react-hook-form to integrate RadioGroup with form validation.
"use client";
import {
Button,
Field,
Group,
Input,
Radio,
RadioGroup,
} from "@optiaxiom/react";
import { Controller, useForm } from "react-hook-form";
export function App() {
const {
control,
formState: { errors },
handleSubmit,
register,
} = useForm<{
name: string;
priority: string;
}>();
return (
<form
noValidate
onSubmit={handleSubmit((data) => {
console.log(data);
})}
>
<Group flexDirection="column" gap="16">
<Field error={errors.name?.message} label="Name" required>
<Input
{...register("name", { required: "Please enter a name" })}
placeholder="Enter name"
/>
</Field>
<Controller
control={control}
name="priority"
render={({ field }) => (
<Field error={errors.priority?.message} label="Priority" required>
<RadioGroup
name="form-hook-form-usage"
onValueChange={field.onChange}
value={field.value ?? ""}
>
<Radio value="low">Low</Radio>
<Radio value="medium">Medium</Radio>
<Radio value="high">High</Radio>
</RadioGroup>
</Field>
)}
rules={{ required: "Please select a priority" }}
/>
<Button alignSelf="start" type="submit">
Submit
</Button>
</Group>
</form>
);
}#
Disabled state
#
Enable the disabled prop on the RadioGroup to toggle the disabled state of all the radio controls.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup disabled name="disabled-group-usage" value="one">
<Radio value="one">Option One</Radio>
<Radio value="two">Option Two</Radio>
</RadioGroup>
);
}You can also enable the disabled prop on each Radio to toggle the disabled state of each radio control.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup name="disabled-usage">
<Radio defaultChecked value="one">
Option One
</Radio>
<Radio disabled value="two">
Option Two
</Radio>
<Radio value="three">Option Three</Radio>
</RadioGroup>
);
}#
Description
#
Use the description prop to add helper text after the label.
import { Radio, RadioGroup } from "@optiaxiom/react";
export function App() {
return (
<RadioGroup name="description-usage">
<Radio description="Helper Text" value="one">
Option One
</Radio>
<Radio value="two">Option Two</Radio>
</RadioGroup>
);
}#
Error state
#
Wrap RadioGroup in a Field component and use the error prop to display validation errors.
"use client";
import { Button, Field, Group, Radio, RadioGroup } from "@optiaxiom/react";
import { useState } from "react";
export function App() {
const [value, setValue] = useState("");
const [error, setError] = useState("");
return (
<Group flexDirection="column" gap="16">
<Field error={error || undefined} label="Preferred contact method">
<RadioGroup
name="error-usage"
onValueChange={(val) => {
setValue(val);
setError("");
}}
value={value}
>
<Radio value="email">Email</Radio>
<Radio value="phone">Phone</Radio>
<Radio value="mail">Mail</Radio>
</RadioGroup>
</Field>
<Button
alignSelf="start"
onClick={() => {
if (!value) {
setError("Please select a contact method");
}
}}
>
Submit
</Button>
</Group>
);
}#
Common patterns
#
#
Conditional fields
#
Use the selected radio value to conditionally show additional fields.
"use client";
import { Field, Group, Input, Radio, RadioGroup } from "@optiaxiom/react";
import { useState } from "react";
export function App() {
const [method, setMethod] = useState("standard");
return (
<Group flexDirection="column" gap="16" w="224">
<Field label="Shipping method">
<RadioGroup
name="pattern-conditional-usage"
onValueChange={setMethod}
value={method}
>
<Radio value="standard">Standard</Radio>
<Radio value="express">Express</Radio>
<Radio value="pickup">Pickup</Radio>
</RadioGroup>
</Field>
{method === "express" && (
<Field label="Phone number for delivery updates">
<Input placeholder="Enter phone number" type="tel" />
</Field>
)}
{method === "pickup" && (
<Field label="Store location">
<RadioGroup name="pattern-conditional-store">
<Radio value="downtown">Downtown</Radio>
<Radio value="midtown">Midtown</Radio>
<Radio value="uptown">Uptown</Radio>
</RadioGroup>
</Field>
)}
</Group>
);
}#
Related
#
Checkbox
Basic control to allow selecting one or more items from a set.
Select
Select a value from a list of options inside a dropdown menu.
Switch
Control to allow toggling between checked and not checked state.
#
Props
#
#
RadioGroup
#
Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
defaultValueThe initial selected value in uncontrolled mode.
|
disabledWhether the input is disabled and in read only mode.
|
flexDirectionSet the element’s
Default: |
nameThe name of the form control elements. Will override any name specified on the inner Radio components.
|
onBlurHandler for Useful for integrating with third party form libraries.
|
onChangeHandler for Useful for integrating with third party form libraries.
|
onValueChangeHandler that is called when the selected value changes.
|
valueThe selected value in controlled mode.
|
#
Radio
#
Supports all Box props in addition to its own. Renders a <div> element but forwards all props to a hidden <input> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
descriptionAdd secondary text after the label.
|
nameThe name of the form control element.
|
onCheckedChangeHandler that is called when the checked state changes.
|
requiredWhether selecting this input is required.
|
valueThe value of the form control element.
|
#
Accessibility
#
#
Keyboard interactions
#
Key | Description |
|---|---|
Tab | Moves focus into and out of the radio group |
Space | Selects the focused radio if not already selected |
ArrowDownArrowRight | Moves focus to and selects the next radio |
ArrowUpArrowLeft | Moves focus to and selects the previous radio |
RadioGroup automatically handles accessibility:
- Uses proper
role="radiogroup"and individualrole="radio"attributes - Supports
aria-checkedfor selected state - Groups radios with proper keyboard navigation
#
Changelog
#
#
0.2.0
#
- Renamed
RadioGroupItemtoRadio
#
0.1.0
#
- Added component