A list of radios grouped together
What kind of site are you building?
Note: Unlike checkboxes, radios are intended to only have a single radio selected at a time. Because of this, it makes sense to manage the state from the group, rather than the individual radio.
This is a set of example components that could be useful when building out a variety of forms. As these are highly dependent on the context they’ll be used in, it’s very unlikely you’ll need all of them so I’ve instead separated these out into examples.
To use these components, copy the code from the code preview and paste it into your project.
In order to use these components you’ll need one of the following imports
import { Radio } from "#/ui/components/Radio"
import { RadioCard } from "#/ui/components/RadioCard"
As we’ll be managing the state here too, we’ll need to import the useState
hook from React
import { useState } from "react"
// ...
export const RadioGroup = () => {
const [selected, setSelected] = useState("one")
return (
<fieldset>
<legend className="sr-only">Group Label</legend>
{/* Swap this for whatever radio style you want */}
<div>
<Radio
id="one"
name="radio-group"
checked={selected === "one"}
onChange={() => setSelected("one")}
/>
<Radio
id="two"
name="radio-group"
checked={selected === "two"}
onChange={() => setSelected("two")}
/>
</div>
...
</fieldset>
)
}
How do you prefer to receive notifications?
<div>
<label className="text-base font-semibold text-gray-900">Notifications</label>
<p className="text-sm text-gray-500">How do you prefer to receive notifications?</p>
<fieldset className="mt-4">
<legend className="sr-only">Notification method</legend>
<div className="flex gap-4 items-center flex-col">
<Radio label="One" />
<Radio label="Two" />
<Radio label="Three" />
</div>
</fieldset>
</div>
How do you prefer to receive notifications?
<div>
<label className="text-base font-semibold text-gray-900">Notifications</label>
<p className="text-sm text-gray-500">How do you prefer to receive notifications?</p>
<fieldset className="mt-4">
<legend className="sr-only">Notification method</legend>
<div className="flex gap-4 flex-col ">
<div className="flex items-start gap-3">
<Radio id="one" className="mt-0.5" />
<div className="flex items-start flex-col leading-tight">
<label for="one">One</label>
<p className="text-sm text-gray-500">
{"A marketing or docs site for presenting information"}
</p>
</div>
</div>
<div className="flex items-start gap-3">
<Radio id="one" className="mt-0.5" />
<div className="flex items-start flex-col leading-tight">
<label for="one">One</label>
<p className="text-sm text-gray-500">
{"A marketing or docs site for presenting information"}
</p>
</div>
</div>
<div className="flex items-start gap-3">
<Radio id="one" className="mt-0.5" />
<div className="flex items-start flex-col leading-tight">
<label for="one">One</label>
<p className="text-sm text-gray-500">
{"A marketing or docs site for presenting information"}
</p>
</div>
</div>
</div>
</fieldset>
</div>
<div>
<label className="text-base font-semibold text-gray-900">Choose your colors</label>
<fieldset className="mt-4">
<legend className="sr-only">Color picker</legend>
<div className="flex gap-4">
<RadioCard className="rounded-full border-none size-8 bg-red-500 hover:bg-red-400 aria-selected:outline-2 aria-selected:outline-indigo-500 outline-offset-2" />
<RadioCard className="rounded-full border-none size-8 bg-blue-500 hover:bg-blue-400 aria-selected:outline-2 aria-selected:outline-indigo-500 outline-offset-2" />
<RadioCard className="rounded-full border-none size-8 bg-green-500 hover:bg-green-400 aria-selected:outline-2 aria-selected:outline-indigo-500 outline-offset-2" />
<RadioCard className="rounded-full border-none size-8 bg-fuchsia-500 hover:bg-fuchsia-400 aria-selected:outline-2 aria-selected:outline-indigo-500 outline-offset-2" />
</div>
</fieldset>
</div>
<div>
<label className="text-base font-semibold text-gray-900">What's your budget?</label>
<fieldset className="mt-4">
<legend className="sr-only">Budget picker</legend>
<div className="flex gap-4">
<RadioCard className="aria-checked:bg-indigo-500 aria-checked:text-white" client:load>
$100
</RadioCard>
<RadioCard className="aria-checked:bg-indigo-500 aria-checked:text-white" client:load>
$1000
</RadioCard>
<RadioCard className="aria-checked:bg-indigo-500 aria-checked:text-white" client:load>
$10000
</RadioCard>
<RadioCard className="aria-checked:bg-indigo-500 aria-checked:text-white " client:load>
$100000
</RadioCard>
</div>
</fieldset>
</div>
<div>
<label className="text-base font-semibold text-gray-900">
What kind of site are you building?
</label>
<fieldset className="mt-4">
<legend className="sr-only">Site selector</legend>
<div className="flex gap-4 flex-col">
<div className="flex gap-4">
<RadioCard client:load defaultChecked showRadio className="flex items-start flex-1">
<div className="text-left flex-1">
<label htmlFor="custom-Radio" className="font-bold">
Website
</label>
<p className="text-sm text-gray-500">
{"A marketing or docs site for presenting information"}
</p>
</div>
</RadioCard>
<RadioCard client:load showRadio className="flex items-start flex-1">
<div className="text-left flex-1">
<label htmlFor="custom-Radio" className="font-bold">
Application
</label>
<p className="text-sm text-gray-500">
{"A web application to manage stuff with your account"}
</p>
</div>
</RadioCard>
<RadioCard client:load defaultChecked showRadio className="flex items-start flex-1">
<div className="text-left flex-1">
<label htmlFor="custom-Radio" className="font-bold">
Ecommerce
</label>
<p className="text-sm text-gray-500">Buy or sell things</p>
</div>
</RadioCard>
</div>
</div>
</fieldset>
</div>