In an Elm program, option selection is handled by the program's update function. The constructors in this module automate part of the set-up for capturing input from selectors. They also provide built-in web accessibility features and default styling via Bulma element classes.
Construct a set of radio buttons that send update messages like regular buttons, but include a value that identifies the user's selection
To make the radio buttons work as intended, the update function should handle the message by updating a record field in the program's model to the user's selection (see example).
Example:
--- MODEL ---
type EmailFrequency
= Daily
| Weekly
| Monthly
type alias Model =
{ emailFrequency : EmailFrequency
, ...
}
--- UPDATE ---
type Msg
= UpdateEmailFrequency EmailFrequency
| ...
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateEmailFrequency option ->
{ model
| emailFrequency = option
}
...
--- VIEW ---
{ id = 10
, options =
[ ("Every day", Daily)
, ("Every week", Weekly)
, ("Every month", Monthly)
]
, selected = model.emailFrequency
}
|> Ui.Selector.radioButtons UpdateEmailFrequencylabel > <input type="radio" />Construct a dropdown selector where selecting an option sends an update message like a button, but also includes a value that identifies the user's selection
To make the dropdown selector work as intended, the update function should handle the message by updating a record field in the program's model to the user's selection (see example).
Example:
--- MODEL ---
type ShippingDestination
= UnitedStates
| Canada
| Global
type alias Model =
{ shippingDestination : Maybe ShippingDestination
, ...
}
initial : Model
initial =
{ shippingDestination = Nothing
, ...
}
--- UPDATE ---
type Msg
= UpdateShippingDestination (Maybe ShippingDestination)
| ...
update : Msg -> Model -> Model
update msg model =
case msg of
UpdateShippingDestination value ->
{ model
| shippingDestination = value
}
...
--- VIEW ---
{ id = 11
, placeholder = "Select shipping destination"
, options =
[ ("United States", UnitedStates)
, ("Canada", Canada)
, ("Global", Global)
]
, selected = model.shippingDestination
}
|> Ui.Selector.dropdown UpdateShippingDestination
Construct a checkbox that sends update messages like a button
To make the checkbox work as intended, the update function should handle the message by toggling a boolean value in the program's model (see example).
Example:
<label>><input type="checkbox" />