Radios
Radios
let you create a radio group.
-->
Radios
let you create a radio group.
You only need two things: a name
for the radio group and a list of values to used for your radio items.
Radios
wraps radio items in a <fieldset>
element since they’re related. You can also provide an optional label with legend
.
Wrapping radios in fieldset is the right thing to do!
If you like to use custom labels for each radio, just pass an array of objects into the radios
property instead.
Just set checked
, disabled
, or readonly
to true
for the radio item.
The difference between disabled
and readonly
radios is that disabled
radios aren’t sent to the server while readonly
radios are.
A radiogroup sometimes contain an “others” option that lets a user type into. You can create this simply by adding hasInput
to the radios
object.
Radios
create multiple radio buttons that are wrapped in a <fieldset>
. The required properties are name
and radios
. If you want to, you can give the group an optional label with legend
.
Each radio
needs to have a value
while a label
is optional. If you don’t provide a label
, Radios
will create labels for each radio by converting value
to Title Case.
For convenience, you can also pass an array of strings to radios
. In this case, each value will be set as the value
of the checkbox.
If you want a radio to be checked, set checked
to true
on that radio item.
If you want all radios to be disabled, use the disabled
property on Radios
. Disabled radios will not send data when submitted, even if they are checked.
If you want to disable a single radio, add the disabled
property to that radio instead.
If you want all radios to be read-only, use the readonly
property on Radios
. Use this over disabled
if you want to send the radio state and value on form submission.
When you do this, make sure the radio you want is checked or nothing will be submitted!
Native radioes don’t have the readonly
attribute. To simulate readonly
, Radios
create a hidden <input>
with the same name
and value
for the checked radio item.
You can add one TextInput
to Radios
. This is often used for adding an “other” option. To activate this, add the hasInput
property to the radio item that should have the input field.
This TextInput
will have its name set to the radio’s name + input
. Both Radio
’s value and TextInput
’s value will be sent to the server when the form is submitted.
All other properties for each radio item will be passed into their respective radio component.
You can change the icon of radio items by setting icon
to Radios
.
The contents of this section will only work in a .svelte
file and not inside
an .astro
file.
You can bind to the <fieldset>
element with the ref
property.
If you bind radios
, you can also get the <input>
element of each radio with the ref
property.
If you need to know which radio is checked, you can bind
to the checked
property.
{
checked: null
}
Only the change
event is supported on Radios
. You can listen to the event like this:
We only support the change
event because Svelte 4 doesn’t have automatic
event-forwarding capability. All events will be forwarded automatically when
we migrate to Svelte 5.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Radios
create multiple radio buttons that are wrapped in a <fieldset>
. The required properties are name
and radios
. If you want to, you can give the group an optional label with legend
.
Each radio
needs to have a value
while a label
is optional. If you don’t provide a label
, Radios
will create labels for each radio by converting value
to Title Case.
For convenience, you can also pass an array of strings to radios
. In this case, each value will be set as the value
of the checkbox.
If you want a radio to be checked, set checked
to true
on that radio item.
If you want all radios to be disabled, use the disabled
property on Radios
. Disabled radios will not send data when submitted, even if they are checked.
If you want to disable a single radio, add the disabled
property to that radio instead.
If you want all radios to be read-only, use the readonly
property on Radios
. Use this over disabled
if you want to send the radio state and value on form submission.
When you do this, make sure the radio you want is checked or nothing will be submitted!
Native radioes don’t have the readonly
attribute. To simulate readonly
, Radios
create a hidden <input>
with the same name
and value
for the checked radio item.
You can add one TextInput
to Radios
. This is often used for adding an “other” option. To activate this, add the hasInput
property to the radio item that should have the input field.
This TextInput
will have its name set to the radio’s name + input
. Both Radio
’s value and TextInput
’s value will be sent to the server when the form is submitted.
All other properties for each radio item will be passed into their respective radio component.
You can change the icon of radio items by setting icon
to Radios
.
The contents of this section will only work in a .svelte
file and not inside
an .astro
file.
You can bind to the <fieldset>
element with the ref
property.
If you bind radios
, you can also get the <input>
element of each radio with the ref
property.
If you need to know which radio is checked, you can bind
to the checked
property.
{
checked: null
}
Only the change
event is supported on Radios
. You can listen to the event like this:
We only support the change
event because Svelte 4 doesn’t have automatic
event-forwarding capability. All events will be forwarded automatically when
we migrate to Svelte 5.
You can import the basic styles for Radios
through Splendid Styles.
If you wish to include only the necessary styles in your project, you can import the following files:
Radios
produces the following HTML:
Each radio item has the following output:
If readonly
and checked
are present, each radio will produce a hidden input with the associated value. This allows you to sent the value
to the server even when the radio input is disabled.
If hasInput
is present, the radio will produce a TextInput
with name set to the radio group’s name plus input
.
If icon
is passed to Radios
or each radio item, .radio
will be changed to the SVG you specified.
The following classes are used by Radios
:
Class | Description |
---|---|
Fieldset | Styles the <fieldset> |
Legend | Styles the legend |
Radios | Styles the radio container |
The following properties can be used to add classes to Radios
.
Property | Description |
---|---|
class | Add classes to Radios |
legendClass | Add classes to the legend |
RadiosClass | Add classes to the radios container |
The following classes are used by each radio element:
Class | Description |
---|---|
Radio | Styles the Radio container |
Radiomark | Styles the container that contains the radio |
Field | Styles the <input> |
Label | Styles the label |
The following properties can be used to add classes each radio element:
Property | Description |
---|---|
class | Add classes to Radio |
labelClass | Add classes to the label |
fieldClass | Add classes to the <input> |
iconClass | Add classes to your custom SVG Icon |
The following classes will be added to the Radio
element upon the satisfaction of the described conditions:
Class | Description |
---|---|
checked | Applied when the radio is checked |
disabled | Applied when the radio is disabled |
readonly | Applied when the radio is readonly |
Each Radio
uses the following CSS variables to define its appearance:
Variable | Default | Description |
---|---|---|
--border-width | 1px | Border width of the <input> |
--border-style | solid | Border style of the <input> |
--border-color | black | Border color of the <input> |
--bg-color | transparent | Background color of the <input> |
--gap | 0.5em | Gap between the radio and the label |
--size | 1em | Size of the <input> |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
You can import the basic styles for Radios
through Splendid Styles.
If you wish to include only the necessary styles in your project, you can import the following files:
Radios
produces the following HTML:
Each radio item has the following output:
If readonly
and checked
are present, each radio will produce a hidden input with the associated value. This allows you to sent the value
to the server even when the radio input is disabled.
If hasInput
is present, the radio will produce a TextInput
with name set to the radio group’s name plus input
.
If icon
is passed to Radios
or each radio item, .radio
will be changed to the SVG you specified.
The following classes are used by Radios
:
Class | Description |
---|---|
Fieldset | Styles the <fieldset> |
Legend | Styles the legend |
Radios | Styles the radio container |
The following properties can be used to add classes to Radios
.
Property | Description |
---|---|
class | Add classes to Radios |
legendClass | Add classes to the legend |
RadiosClass | Add classes to the radios container |
The following classes are used by each radio element:
Class | Description |
---|---|
Radio | Styles the Radio container |
Radiomark | Styles the container that contains the radio |
Field | Styles the <input> |
Label | Styles the label |
The following properties can be used to add classes each radio element:
Property | Description |
---|---|
class | Add classes to Radio |
labelClass | Add classes to the label |
fieldClass | Add classes to the <input> |
iconClass | Add classes to your custom SVG Icon |
The following classes will be added to the Radio
element upon the satisfaction of the described conditions:
Class | Description |
---|---|
checked | Applied when the radio is checked |
disabled | Applied when the radio is disabled |
readonly | Applied when the radio is readonly |
Each Radio
uses the following CSS variables to define its appearance:
Variable | Default | Description |
---|---|---|
--border-width | 1px | Border width of the <input> |
--border-style | solid | Border style of the <input> |
--border-color | black | Border color of the <input> |
--bg-color | transparent | Background color of the <input> |
--gap | 0.5em | Gap between the radio and the label |
--size | 1em | Size of the <input> |
You can use these properties to customize Radios
’ functionality.
Property | Type | Description |
---|---|---|
legend | string | Legend for the group of radios |
name | string | Name of the radio group. (required). |
radios | array | Radio items. See radios . (required). |
disabled | boolean | Adds the disabled state to all radios when present. |
readonly | boolean | Adds the readonly state to all radios when present. |
ref | object | Reference for the <fieldset> element |
Use these properties to customize each radio item.
Property | Type | Description |
---|---|---|
id | string | Id of the input. A random value will be created if undefined |
value | string | Input value. (required) |
label | string | The label |
checked | boolean | Adds the checked state when present. |
disabled | boolean | Adds the disabled state when present. |
readonly | boolean | Adds the readonly state when present. |
ref | object | Reference for the <input> element |
hasInput | boolean | Creates an additional TextInput alongside the checkbox if true . See additional input field for usage instructions. |
List of radio items. Each item can be a string or an object.
string[] | object[]
If a list of strings are passed as radios
, each string will be used as the value of the radio item.
Properties that can be passed into each radio items are indicated on the for each radio item table above.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
You can use these properties to customize Radios
’ functionality.
Property | Type | Description |
---|---|---|
legend | string | Legend for the group of radios |
name | string | Name of the radio group. (required). |
radios | array | Radio items. See radios . (required). |
disabled | boolean | Adds the disabled state to all radios when present. |
readonly | boolean | Adds the readonly state to all radios when present. |
ref | object | Reference for the <fieldset> element |
Use these properties to customize each radio item.
Property | Type | Description |
---|---|---|
id | string | Id of the input. A random value will be created if undefined |
value | string | Input value. (required) |
label | string | The label |
checked | boolean | Adds the checked state when present. |
disabled | boolean | Adds the disabled state when present. |
readonly | boolean | Adds the readonly state when present. |
ref | object | Reference for the <input> element |
hasInput | boolean | Creates an additional TextInput alongside the checkbox if true . See additional input field for usage instructions. |
List of radio items. Each item can be a string or an object.
string[] | object[]
If a list of strings are passed as radios
, each string will be used as the value of the radio item.
Properties that can be passed into each radio items are indicated on the for each radio item table above.