Radio

A radio button for selecting one option from a set.

import { ... } from '@corndev/ui'

Selected: pro

HTML

No JavaScript required — these classes are the whole component.

<div class="corn-radio-group">
  <label><input type="radio" name="plan" class="corn-radio" data-size="md" /> Free</label>
  <label><input type="radio" name="plan" class="corn-radio" data-size="md" /> Pro</label>
</div>

Classes

Class
.corn-radio
.corn-radio-group

Attributes

AttributeValues
data-sizesm|md|lg
data-errorpresent or absent

Vanilla JS

Optional. createRadio only adds and removes the classes and attributes above, so the HTML is equivalent. Reach for it when you need to change options at runtime.

import { createRadio } from '@corndev/ui';
import '@corndev/ui/theme';

const el = document.querySelector('#target');
const instance = createRadio(el, { /* options */ });

Options

OptionTypeDefault
disabledbooleanfalse
errorbooleanfalse
size'sm'|'md'|'lg''md'

React

Radio, RadioGroup — from @corndev/ui-react.

RadioGroup supplies name/checked/onChange to each child Radio, which are matched by their value prop.

The props are the same as the options above, plus className.

<RadioGroup> props

Wraps Radio children and owns the selection.

PropTypeDefaultDescription
namestringundefinedShared radio group name (generated if omitted).
valuestringundefinedCurrently selected radio value.
onChange(value: string) => voidundefinedCalled with the newly selected value.
disabledbooleanfalseDisables every radio in the group.
classNamestringundefined
childrenReact.ReactNodeundefined

Group

Use RadioGroup to wire a shared name, the selected value, and one onChange.

<RadioGroup name="size" value={size} onChange={setSize}>
  <Label><Radio value="sm" /> Small</Label>
  <Label><Radio value="md" /> Medium</Label>
  <Label><Radio value="lg" /> Large</Label>
</RadioGroup>

Sizes

<Label><Radio size="sm" /> Small</Label>
<Label><Radio size="md" /> Medium</Label>
<Label><Radio size="lg" /> Large</Label>