Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having radio buttons reflect the model

Tags:

elm

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?

like image 800
Old Man Avatar asked Aug 28 '17 18:08

Old Man


People also ask

What is the purpose of radio button?

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.

When Should radio buttons be used?

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.

What attributes do radio buttons have?

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.

Can two radio buttons have the same value?

You can't have more than one button checked in the same radio group. You need to give the two sets different names.


2 Answers

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
like image 171
Simon H Avatar answered Oct 23 '22 08:10

Simon H


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.

like image 30
zoul Avatar answered Oct 23 '22 07:10

zoul