Radio field
v0.0.10
mix mishka.ui.gen.component radio_field
Radio component
The radio component lets users select one option from a group, with the checked prop determining which option is selected by default. Only one radio button can be selected at a time, making it ideal for choosing between mutually exclusive options like contact methods or shipping choices.
Radio component
<.radio_field name="female" value="female" label="female" />
<.radio_field name="male" value="male" label="Male" checked />Radio group component
The group_radio component is ideal for grouping related radio buttons in forms, where only one option can be selected. It enables organized layouts with customizable styling, spacing, and alignment (horizontal or vertical), ensuring a cohesive and user-friendly selection experience.
Radio group component
<!--Default variation is vertical-->
<.group_radio name="" id="" space="small" color="danger">
<:radio value="option1">Option 1</:radio>
<:radio value="option2">Option 2</:radio>
<:radio value="option3">Option 3</:radio>
<:radio value="option4" checked>Option 4</:radio>
</.group_radio>
<.group_radio name="" space="small" variation="horizontal">
<:radio value="option1">Option 1</:radio>
<:radio value="option2">Option 2</:radio>
<:radio value="option3">Option 3</:radio>
<:radio value="option4" checked>Option 4</:radio>
</.group_radio>radio_check helper
Here is a real-world example provided for you.
radio_check helper
<.form_wrapper
for={@form}
id="user-form"
phx-change="validate"
phx-submit="save"
class="space-y-5"
>
<.input label="Full name" field={@form[:fullname]} />
<.group_radio field={@form[:job]} space="small" variation="horizontal" color="misc">
<:radio
:for={item <- Ecto.Enum.values(User, :job)}
value={item}
checked={radio_check(:list, {:job, item}, @form)}
>
<%= String.capitalize(to_string(item)) %>
</:radio>
</.group_radio>
<.h3>What is your hobbies?</.h3>
<.group_checkbox variation="horizontal" field={@form[:hobbies]} color="danger">
<:checkbox
value="reading"
checked={checkbox_check(:list, {:hobbies, "reading"}, @form)}
>
Reading
</:checkbox>
<:checkbox
value="programming"
checked={checkbox_check(:list, {:hobbies, "programming"}, @form)}
>
Programming
</:checkbox>
<:checkbox
value="photography"
checked={checkbox_check(:list, {:hobbies, "photography"}, @form)}
>
Photography
</:checkbox>
</.group_checkbox>
<.toggle_field
field={@form[:accepted_terms]}
label="Terms"
checked={toggle_check(:accepted_terms, @form)}
color="primary"
phx-debounce="300"
/>
<.radio_field
field={@form[:subscribe]}
checked={radio_check(:boolean, :subscribe, @form)}
label="Subscribe?"
color="danger"
value="true"
/>
<:actions>
<.button phx-disable-with="Saving...">Save User</.button>
</:actions>
</.form_wrapper>Attributes
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
id
|
string
| — | — | No | A unique identifier is used to manage state and interaction |
class
|
any
| — | — | No | Custom CSS class for additional styling |
label_class
|
string
| — | — | No | Custom CSS class for the label styling |
color
|
string
|
primary
|
basenaturalwhiteprimarysecondarydarksuccesswarningdangerinfosilvermiscdawn
| No | Determines color theme |
border
|
string
|
extra_small
| — | No | Determines border style |
space
|
string
|
medium
|
extra_smallsmallmediumlargeextra_largenone
| No | Space between items |
wrapper_class
|
string
| — | — | No | Custom CSS class for the wrapper |
radio_class
|
string
| — | — | No | Custom CSS class for the radio input |
size
|
string
|
extra_large
|
extra_smallsmallmediumlargeextra_large
| No | Determines the overall size of the elements, including padding, font size, and other items |
ring
|
boolean
|
true
| — | No | Determines a ring border on focused input, utilities for creating outline rings with box-shadows. |
reverse
|
boolean
|
false
| — | No | Switches the order of the element and label |
checked
|
boolean
|
false
| — | No | Specifies if the element is checked by default |
error_icon
|
string
| — | — | No | Icon to be displayed alongside error messages |
label
|
string
| — | — | No | Specifies text for the label |
errors
|
list
|
[]
| — | No | List of error messages to be displayed |
name
|
any
| — | — | No | Name of input |
value
|
any
| — | — | No | Value of input |
field
|
Phoenix.HTML.FormField
| — | — | No | a form field struct retrieved from the form |
rest
|
global
| — | — | No | Global attributes can define defaults which are merged with attributes provided by the caller |
Options
Each option takes one or more of its values, separated by commas.
| Option | Values |
|---|---|
--color
|
basenaturalwhiteprimarysecondarydarksuccesswarningdangerinfosilvermiscdawn
|
--size
|
extra_smallsmallmediumlargeextra_large
|
--space
|
extra_smallsmallmediumlargeextra_largenone
|
--module
| A module name of your own |
What it generates
mix mishka.ui.gen.component radio_field