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.
--- 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" />--- 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)
--- MODEL ---
type alias Model = { userAgrees : Bool , ... }
initial : Model initial = { userAgrees = False , ... }
--- UPDATE ---
type Msg = ToggleUserAgrees | ...
update : Msg -> Model -> Model update msg model = case msg of ToggleUserAgrees -> { model | userAgrees = model.userAgrees |> Basics.not }
--- VIEW ---
{ id = 9 , label = "I agree to the terms and conditions" , checked = model.userAgrees } |> Ui.Selector.checkbox ToggleUserAgrees
HTML tag:
<label>><input type="checkbox" />ARIA role: checkbox (default for checkbox input)
Element class: .checkbox
Available modifiers: none
Bulma CSS reference: https://bulma.io/documentation/form/checkbox/