elm

Elm: preserve type of values in HTML select?


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?


Solution

  • 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/softwarecraft/2017-01-13_playing-with-prisms-for-the-not-so-isomorphic