Components
Radio
Picks exactly one option from a short list.
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
import { RadioGroup, RadioGroupItem } from '@dex/shared/components/radio-group'
export function RadioDefault() {
return (
<FieldSet>
<FieldLegend>How should Dex contact you?</FieldLegend>
<RadioGroup defaultValue="email">
<div className="flex items-center gap-3">
<RadioGroupItem id="radio-contact-email" value="email" />
<Label htmlFor="radio-contact-email" className="font-normal">
Email
</Label>
</div>
<div className="flex items-center gap-3">
<RadioGroupItem id="radio-contact-phone" value="phone" />
<Label htmlFor="radio-contact-phone" className="font-normal">
Phone
</Label>
</div>
<div className="flex items-center gap-3">
<RadioGroupItem id="radio-contact-whatsapp" value="whatsapp" />
<Label htmlFor="radio-contact-whatsapp" className="font-normal">
WhatsApp
</Label>
</div>
</RadioGroup>
</FieldSet>
)
}
import { FieldDescription, FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
import { RadioGroup, RadioGroupItem } from '@dex/shared/components/radio-group'
export function RadioWithDescriptions() {
const options = [
{
value: 'email',
label: 'Email',
description: 'New role matches and interview updates, as they happen.',
},
{
value: 'phone',
label: 'Phone',
description: 'A call from your talent partner, weekdays 9am to 6pm.',
},
{
value: 'whatsapp',
label: 'WhatsApp',
description: 'Quick messages about next steps and interview times.',
},
]
return (
<FieldSet>
<FieldLegend>How should Dex contact you?</FieldLegend>
<RadioGroup defaultValue="email">
{options.map((option) => (
<div key={option.value} className="flex items-start gap-3">
<RadioGroupItem id={`radio-described-${option.value}`} value={option.value} />
<div className="flex flex-col gap-0.5">
<Label htmlFor={`radio-described-${option.value}`}>{option.label}</Label>
<FieldDescription>{option.description}</FieldDescription>
</div>
</div>
))}
</RadioGroup>
</FieldSet>
)
}
'use client'
import { useState } from 'react'
import { FieldLegend, FieldMessage, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
import { RadioGroup, RadioGroupItem } from '@dex/shared/components/radio-group'
export function RadioControlled() {
const notices = ['Immediately', '1 month', '3 months']
const [notice, setNotice] = useState('1 month')
return (
<FieldSet>
<FieldLegend>What is your notice period?</FieldLegend>
<RadioGroup value={notice} onValueChange={setNotice}>
{notices.map((option) => (
<div key={option} className="flex items-center gap-3">
<RadioGroupItem id={`radio-notice-${option}`} value={option} />
<Label htmlFor={`radio-notice-${option}`} className="font-normal">
{option}
</Label>
</div>
))}
</RadioGroup>
<FieldMessage
description={
notice === 'Immediately'
? 'Dex tells companies you can start straight away.'
: `Dex tells companies you can start in ${notice}.`
}
/>
</FieldSet>
)
}
'use client'
import { useState } from 'react'
import { FieldLegend, FieldMessage, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
import { RadioGroup, RadioGroupItem } from '@dex/shared/components/radio-group'
export function RadioInvalid() {
const answers = ['Yes', 'Yes, with sponsorship', 'No']
const [answer, setAnswer] = useState('')
return (
<FieldSet invalid={!answer}>
<FieldLegend>Do you have the right to work in the UK?</FieldLegend>
<RadioGroup value={answer} onValueChange={setAnswer}>
{answers.map((option) => (
<div key={option} className="flex items-center gap-3">
<RadioGroupItem id={`radio-right-to-work-${option}`} value={option} />
<Label htmlFor={`radio-right-to-work-${option}`} className="font-normal">
{option}
</Label>
</div>
))}
</RadioGroup>
{!answer && <FieldMessage error="Choose whether you have the right to work in the UK." />}
</FieldSet>
)
}
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
import { RadioGroup, RadioGroupItem } from '@dex/shared/components/radio-group'
export function RadioDisabled() {
return (
<FieldSet disabled>
<FieldLegend>Where do you want to work?</FieldLegend>
<RadioGroup defaultValue="hybrid">
<div className="flex items-center gap-3">
<RadioGroupItem id="radio-disabled-remote" value="remote" />
<Label htmlFor="radio-disabled-remote" className="font-normal">
Remote
</Label>
</div>
<div className="flex items-center gap-3">
<RadioGroupItem id="radio-disabled-hybrid" value="hybrid" />
<Label htmlFor="radio-disabled-hybrid" className="font-normal">
Hybrid
</Label>
</div>
</RadioGroup>
</FieldSet>
)
}
- Use radios for picking exactly one option from a short list. For several options use checkboxes.
- A radio is always shown in a group, in a
FieldSetwith its question as theFieldLegend. - Join each radio to its label with
idandhtmlFor, so pressing the label chooses it. The label takesfont-normalunless a description follows it. - Set
invalidon theFieldSet, oraria-invalidon theRadioGroup, and every option's border turns red. One message goes under the group. - A disabled radio that is selected keeps its fill, so it still shows the choice.