In this example, the initial fontSize
is Medium
. This is reflected in the size of the font, but the medium radio button is not selected. Is there a way to make the radio button always reflect the current fontSize
?
Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.
Radio buttons are used when there is a list of two or more options that are mutually exclusive and the user must select exactly one choice. In other words, clicking a non-selected radio button will deselect whatever other button was previously selected in the list.
Radio buttons are created using <input> tag in HTML whith radio as the type attribute. Checkboxes are created using <input> tag in HTML with type attribute as checkbox. Radio buttons in HTML are used in the "Select one" type of menus.
You can't have more than one button checked in the same radio group. You need to give the two sets different names.
Is this what you were looking for?
view : Model -> Html Msg
view model =
div []
[ fieldset []
[ radio "Small" (model.fontSize == Small) (SwitchTo Small)
, radio "Medium"(model.fontSize == Medium) (SwitchTo Medium)
, radio "Large" (model.fontSize == Large) (SwitchTo Large)
]
, Markdown.toHtml [ sizeToStyle model.fontSize ] model.content
]
radio : String -> Bool > msg -> Html msg
radio value isChecked msg =
label
[ style [("padding", "20px")]
]
[ input [ type_ "radio", name "font-size", onInput msg, checked isChecked ] []
, text value
]
Note that I changed onClick to onInput, which I think is better practise for form selections.
As an aside, I tend to put the msg parameter at the beginning of the type signature as that's the least likely to be part of a pipeline of functions:
radio : msg -> Bool -> String -> Html msg
This is the code in question:
view : Model -> Html Msg
view model =
div []
[ fieldset []
[ radio "Small" (SwitchTo Small)
, radio "Medium" (SwitchTo Medium)
, radio "Large" (SwitchTo Large)
]
, Markdown.toHtml [ sizeToStyle model.fontSize ] model.content
]
radio : String -> msg -> Html msg
radio value msg =
label
[ style [("padding", "20px")]
]
[ input [ type_ "radio", name "font-size", onClick msg ] []
, text value
]
And this is the line used to render a radio input:
input [ type_ "radio", name "font-size", onClick msg ] []
There’s a checked
attribute for radios (see the docs), so it looks you could add that depending on the current font size? Something like:
radio : String -> Bool -> msg -> Html msg
radio value isChecked msg =
label
[ style [("padding", "20px")]
]
[ input [ type_ "radio", name "font-size", checked isChecked, onClick msg ] []
, text value
]
…and then set the isChecked
argument according to the model in the view
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With