A native <select> with Designsystemet styling.
Mirrors Select from @digdir/designsystemet-react.
To pick one of your own values, use single:
municipalities =
{ options = Municipality.all
, selected = model.municipality
, toValue = Municipality.toCode
, toLabel = Municipality.toName
, onSelect = PickedMunicipality
, placeholder = "Velg kommune"
}
Select.single municipalities [ Attr.required True ]view, option and optgroup are the
plain elements, for when you need option groups or want to handle the strings
yourself.
A select needs a label. Put it in a Field:
Field.view []
[ Label.view [] [ Html.text "Kommune" ]
, Select.single municipalities [ Attr.required True ]
]<Select> + <Select.Option>s for your values → Select.single { … } []<Select> → Select.view []<Select.Option value="…"> → Select.option [ Attr.value "…" ]<Select.Optgroup label="…"> → Select.optgroup { label = "…" } []width="auto" → Select.autoWidtharia-readonly → Select.view [ Attr.attribute "aria-readonly" "true" ]readOnly → not ported: deprecated in React, use aria-readonlyA <select> can't be readonly in HTML. With aria-readonly="true",
@digdir/designsystemet-web stops the user from changing it and the CSS shows
it as read-only.
A <select class="ds-input">. Listen with Events.onInput, which gives you
the chosen option's value.
An <option>. Mark the chosen one with Attr.selected True. Setting
value on the select does nothing on first render, as the options aren't
there yet.
A labelled group of options.
Select.view [ Events.onInput PickedRoom ]
[ Select.optgroup { label = "Bygg A" }
[]
[ Select.option [ Attr.value "a101" ] [ Html.text "A101" ]
, Select.option [ Attr.value "a102" ] [ Html.text "A102" ]
]
]As wide as its longest option.
As wide as its container. This is the default.
A select for picking one of your own values.
toValueturns a value into the string the browser keeps invalue. Give every option a different one.toLabelis the text the user sees.onSelectgets the picked value back as your type.placeholderis the prompt shown whileselectedisNothing. It can't be picked and disappears once something is selected.selectedis aMaybeso the screen matches the model. Without a prompt, the browser would show the first option as chosen while your model has nothing. Ifselectedisn't amongoptions(say the list was reloaded without it), the prompt is shown too.If "none" is a real choice, make it one of your options:
A
<select>can't refuse a change. The browser already shows the pick whenonSelectarrives, so always store the value. Ifupdateignores it, the screen and the model disagree untilselectedchanges.The attributes go on the
<select>. The options are keyed bytoValue, so the list can change under the user without Elm reusing the wrong<option>.