Radio Group
Provides shared state to a series of radio buttons.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Radio, RadioGroup};
use function Phem\Ui\RadioIndicator;
#[Component]
function RadioGroupHeroDemo(): Node
{
$apples = [
'fuji-apple' => 'Fuji',
'gala-apple' => 'Gala',
'granny-smith-apple' => 'Granny Smith',
];
return (
<RadioGroup aria-labelledby="best-apple" defaultValue="fuji-apple" name="apple">
<div className="text-sm font-bold" id="best-apple">Best apple</div>
{% for value, name in apples %}
<label key={{ value }} className="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<Radio value={{ value }}>
<RadioIndicator />
</Radio>
{{ name }}
</label>
{% endfor %}
</RadioGroup>
);
}
Examples#
Labeling a radio group#
Label the group with aria-labelledby and a sibling label element. An enclosing <label> is the simplest labeling pattern for each radio.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Radio, RadioGroup};
use function Phem\Ui\RadioIndicator;
#[Component]
function RadioGroupLabelingDemo(): Node
{
$types = ['ssd' => 'SSD', 'hdd' => 'HDD'];
return (
<RadioGroup aria-labelledby="storage-type-label" defaultValue="ssd">
<div id="storage-type-label" className="text-sm font-bold">Storage type</div>
{% for value, name in types %}
<label key={{ value }} className="flex items-center gap-2 text-sm">
<Radio value={{ value }}>
<RadioIndicator />
</Radio>
{{ name }}
</label>
{% endfor %}
</RadioGroup>
);
}
#
By default, Radio renders a <span> element to support enclosing labels. Prefer rendering each radio as a native button with nativeButton when using sibling labels (for/id).
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Radio, RadioGroup};
use function Phem\Ui\RadioIndicator;
#[Component]
function RadioGroupNativeButtonDemo(): Node
{
$types = ['ssd' => 'SSD', 'hdd' => 'HDD'];
return (
<RadioGroup aria-labelledby="storage-type" defaultValue="ssd">
<div id="storage-type" className="text-sm font-bold">Storage type</div>
{% for value, name in types %}
<div key={{ value }} className="flex items-center gap-2 text-sm">
<Radio id="storage-type-{{ value }}" value={{ value }} nativeButton>
<RadioIndicator />
</Radio>
<label htmlFor="storage-type-{{ value }}">{{ name }}</label>
</div>
{% endfor %}
</RadioGroup>
);
}
Form integration#
Use Field and Fieldset for group labeling and form integration.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\{Field, Fieldset, Radio, RadioGroup};
use function Phem\Ui\{FieldItem, FieldLabel, FieldsetLegend, Form, RadioIndicator};
#[Component]
function RadioGroupFormDemo(): Node
{
$types = ['ssd' => 'SSD', 'hdd' => 'HDD'];
return (
<Form>
<Field>
<Fieldset>
<FieldsetLegend className="text-sm">Storage type</FieldsetLegend>
<RadioGroup aria-label="Storage type" name="storageType" defaultValue="ssd">
{% for value, name in types %}
<FieldItem key={{ value }}>
<FieldLabel className="flex items-center gap-2 font-normal">
<Radio value={{ value }}>
<RadioIndicator />
</Radio>
{{ name }}
</FieldLabel>
</FieldItem>
{% endfor %}
</RadioGroup>
</Fieldset>
</Field>
</Form>
);
}
API reference#
Import the components of the family:
use Phem\Ui\RadioGroup;
use Phem\Ui\Radio;
use function Phem\Ui\RadioIndicator;RadioGroup#
A group of radio buttons: Base UI RadioGroup, on the phem-radio-group controller.
defaultValue is the value of the checked radio. A hidden input keeps the value for a form (name). Bind it to a state of the app with phem:bind. Label the group with aria-labelledby. With required, a group with no checked radio stops the submit of its form, and the focus goes to the radio of the tab stop.
| Prop | Type | Default |
|---|---|---|
defaultValue | ?string | null |
name | ?string | null |
disabled | bool | false |
readOnly | bool | false |
required | bool | false |
value | ?Bound | null |
children | ?Html | null |
...attrs | array | [] |
Radio#
A radio of the group: Base UI Radio.Root. Put it in a <label> with its text.
| Prop | Type | Default |
|---|---|---|
value (required) | string | — |
disabled | bool | false |
nativeButton | bool | false |
children | ?Html | null |
...attrs | array | [] |
RadioIndicator#
The dot of a checked radio.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
The source of the examples: examples/ui/RadioGroup.phem.