Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elm: preserve type of values in HTML select?

Tags:

elm

In AngularJS, ng-options lets one specify an array of any values, not just strings, while preserving types. For example, I might create an HTML select using ng-options over an array of integers. When an option is selected, the integer value is placed into the model—no string-to-int conversion necessary.

In Elm, an option's value attribute accepts only a string, and thus the select's onInput event sends back a string. Then I have to manually convert it to an int.

Does Elm have any equivalent to AngularJS's ng-options? Or any way to use a select with arbitrary, even non-scalar, values?

like image 759
jchamberlain Avatar asked Jan 13 '17 19:01

jchamberlain


2 Answers

There isn't really a way to do this. You're going to have to parse from a string to your desired type and back. You could wrap a <select> with Prism-based API.

Give me a couple hours and I'll write a tiny library for that after work... :D

EDIT:

Package - http://package.elm-lang.org/packages/toastal/select-prism/latest

Blog Post - https://toast.al/posts/2017-01-13-playing-with-prisms-for-the-not-so-isomorphic.html

like image 136
toastal Avatar answered Nov 01 '22 19:11

toastal


You might also be interested in my elm-select helper library. It doesn't do all that cool Prism / Lens stuff, but it might be simpler in some cases:

http://package.elm-lang.org/packages/lgastako/elm-select/1.0.0/Select

Full example code here:

https://github.com/lgastako/elm-select/blob/master/src/Main.elm

like image 21
John Avatar answered Nov 01 '22 19:11

John