Data Display
Forms
Navigation
Radio Group
A set of checkable buttons where no more than one of the buttons can be checked at a time.
import { Label } from "@/components/ui/label";
import { Radio, RadioGroup } from "@/components/ui/radio-group";
export default function Particle() {
return (
<RadioGroup defaultValue="next">
<Label>
<Radio value="next" /> Next.js
</Label>
<Label>
<Radio value="vite" /> Vite
</Label>
<Label>
<Radio value="astro" /> Astro
</Label>
</RadioGroup>
);
}
Installation
pnpm dlx cnippet@latest add radio-group
Usage
import { Label } from "@/components/ui/label"
import { Radio, RadioGroup } from "@/components/ui/radio-group"<RadioGroup defaultValue="next">
<Label>
<Radio value="next" /> Next.js
</Label>
<Label>
<Radio value="vite" /> Vite
</Label>
<Label>
<Radio value="astro" /> Astro
</Label>
</RadioGroup>Examples
For accessible labelling and validation, prefer using the Field component to wrap radio buttons. See the related example: Radio Group field.
With Description
import { Label } from "@/components/ui/label";
import { Radio, RadioGroup } from "@/components/ui/radio-group";
export default function Particle() {
return (
<RadioGroup defaultValue="r-1">
<Label className="flex items-start gap-2 rounded-lg border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50">
<Radio value="r-1" />
<div className="flex flex-col gap-1">
<p>Email</p>
<p className="text-muted-foreground text-xs">
Receive notifications via email.
</p>
</div>
</Label>
<Label className="flex items-start gap-2 rounded-lg border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50">
<Radio value="r-2" />
<div className="flex flex-col gap-1">
<p>SMS</p>
<p className="text-muted-foreground text-xs">
Receive notifications via text message.
</p>
</div>
</Label>
</RadioGroup>
);
}
Card Style
Basic features for personal use.
Advanced tools for professionals.
import { Label } from "@/components/ui/label";
import { Radio, RadioGroup } from "@/components/ui/radio-group";
export default function Particle() {
return (
<RadioGroup defaultValue="r-1">
<div className="flex items-start gap-2">
<Radio id="r-1" value="r-1" />
<div className="flex flex-col gap-1">
<Label htmlFor="r-1">Free</Label>
<p className="text-muted-foreground text-xs">
Basic features for personal use.
</p>
</div>
</div>
<div className="flex items-start gap-2">
<Radio id="r-2" value="r-2" />
<div className="flex flex-col gap-1">
<Label htmlFor="r-2">Pro</Label>
<p className="text-muted-foreground text-xs">
Advanced tools for professionals.
</p>
</div>
</div>
</RadioGroup>
);
}
Form Integration
"use client";
import * as React from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldLabel } from "@/components/ui/field";
import { Fieldset, FieldsetLegend } from "@/components/ui/fieldset";
import { Form } from "@/components/ui/form";
import { Radio, RadioGroup } from "@/components/ui/radio-group";
export default function Particle() {
const [loading, setLoading] = React.useState(false);
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
setLoading(true);
await new Promise((r) => setTimeout(r, 800));
setLoading(false);
alert(`Selected: ${formData.get("frameworks")}`);
};
return (
<Form className="max-w-[160px]" onSubmit={onSubmit}>
<Field
className="gap-4"
name="frameworks"
render={(props) => <Fieldset {...props} />}
>
<FieldsetLegend className="font-medium text-sm">
Frameworks
</FieldsetLegend>
<RadioGroup defaultValue="next">
<FieldLabel>
<Radio disabled={loading} value="next" /> Next.js
</FieldLabel>
<FieldLabel>
<Radio disabled={loading} value="vite" /> Vite
</FieldLabel>
<FieldLabel>
<Radio disabled={loading} value="astro" /> Astro
</FieldLabel>
</RadioGroup>
</Field>
<Button disabled={loading} type="submit">
Submit
</Button>
</Form>
);
}