Components
Radio Group
Provides shared state to a series of radio buttons.
Usage guidelines#
- Form controls must have an accessible name. It can be created using
<label>elements, or the Field and Fieldset components. See Labeling a radio group.
Anatomy#
Import the component and assemble its parts:
import { Radio } from '@egoist/solid-base/radio';
import { RadioGroup } from '@egoist/solid-base/radio-group';
<RadioGroup>
<Radio.Root>
<Radio.Indicator />
</Radio.Root>
</RadioGroup>;Examples#
Labeling a radio group#
Label the group with aria-labelledby and a sibling label element:
<div id="storage-type-label">Storage type</div>
<RadioGroup aria-labelledby="storage-type-label">{/* ... */}</RadioGroup>An enclosing <label> is the simplest labeling pattern for each radio:
<label>
<Radio.Root value="ssd" />
SSD
</label>Rendering as a native button#
By default, <Radio.Root> renders a <span> element to support enclosing labels. Prefer
rendering each radio as a native button when using sibling labels (for/id).
<div id="storage-type">Storage type</div>
<RadioGroup defaultValue="ssd" aria-labelledby="storage-type">
<div>
<label for="storage-type-ssd">SSD</label>
<Radio.Root value="ssd" id="storage-type-ssd" nativeButton render="button">
<Radio.Indicator />
</Radio.Root>
</div>
</RadioGroup>Native buttons with wrapping labels are supported by using the render callback to avoid invalid
HTML, so the hidden input is placed outside the label:
<div id="storage-type">Storage type</div>
<RadioGroup defaultValue="ssd" aria-labelledby="storage-type">
<Radio.Root
value="ssd"
nativeButton
render={(buttonProps) => (
<label>
<button {...buttonProps} />
SSD
</label>
)}
/>
</RadioGroup>Controlled#
Pass value and update it from onValueChange:
const [storage, setStorage] = createSignal('ssd');
<RadioGroup value={storage()} onValueChange={(value) => setStorage(value)}>
{/* ... */}
</RadioGroup>;API reference#
<RadioGroup>
Provides shared state to a series of radio buttons. Renders a <div> element.
idstring—The id of the radio group element.
disabledbooleanfalseWhether the component should ignore user interaction.
readOnlybooleanfalseWhether the user should be unable to select a different radio button in the group.
requiredbooleanfalseWhether the user must choose a value before submitting a form.
namestring—Identifies the field when a form is submitted.
formstring—Identifies the form that owns the radio inputs. Useful when the radio group is rendered outside the form.
valueValue—The controlled value of the radio item that should be currently selected.
To render an uncontrolled radio group, use the defaultValue prop instead.
defaultValueValue—The uncontrolled value of the radio button that should be initially selected.
To render a controlled radio group, use the value prop instead.
onValueChange| ((value: Value, eventDetails: RadioGroup.ChangeEventDetails) => void)—Callback fired when the value changes.
inputRefRefProp<HTMLInputElement | null>—A ref to access the hidden input element (the checked one, or the first enabled one). Called with null when that input unmounts.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-disabledPresent when the radio group is disabled.
The radio buttons inside the group are documented on the Radio page:
<Radio.Root>
Represents the radio button itself. Renders a <span> element and a hidden <input> beside.
idstring—The id of the hidden input element.
When nativeButton is true, the id is applied to the root element.
value*Value—The unique identifying value of the radio in a group.
disabledboolean—Whether the component should ignore user interaction.
requiredboolean—Whether the user must choose a value before submitting a form.
readOnlyboolean—Whether the user should be unable to select the radio button.
inputRefRefProp<HTMLInputElement>—A ref to access the hidden input element.
nativeButtonbooleanfalseWhether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-checkedPresent when the radio is checked.
data-uncheckedPresent when the radio is not checked.
data-disabledPresent when the radio is disabled.
data-readonlyPresent when the radio is readonly.
data-requiredPresent when the radio is required.
data-validPresent when the radio is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the radio is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the radio has been touched (when wrapped in Field.Root).
data-dirtyPresent when the radio's value has changed (when wrapped in Field.Root).
data-filledPresent when the radio is checked (when wrapped in Field.Root).
data-focusedPresent when the radio is focused (when wrapped in Field.Root).
<Radio.Indicator>
Indicates whether the radio button is selected. Renders a <span> element.
keepMountedbooleanfalseWhether to keep the HTML element in the DOM when the radio button is inactive.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-checkedPresent when the radio is checked.
data-uncheckedPresent when the radio is not checked.
data-disabledPresent when the radio is disabled.
data-readonlyPresent when the radio is readonly.
data-requiredPresent when the radio is required.
data-validPresent when the radio is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the radio is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the radio has been touched (when wrapped in Field.Root).
data-dirtyPresent when the radio's value has changed (when wrapped in Field.Root).
data-filledPresent when the radio is checked (when wrapped in Field.Root).
data-focusedPresent when the radio is focused (when wrapped in Field.Root).