Last active
November 17, 2018 11:28
-
-
Save baritonehands/d749f2374f90298c106fda8686204a1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Html exposing (Html, div, br, span, text) | |
import Html.App as App | |
import Select exposing (select) | |
main = | |
App.beginnerProgram | |
{ model = model | |
, view = view | |
, update = update | |
} | |
type alias Model = | |
{ state: State | |
, medal: Int | |
} | |
model : Model | |
model = | |
{ state = Empty | |
, medal = 0 | |
} | |
type Msg | |
= StateSelected State | |
| MedalSelected Int | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
StateSelected state -> | |
{ model | state = state } | |
MedalSelected medal -> | |
{ model | medal = medal } | |
type State | |
= Empty | |
| AL | |
| AK | |
| AS | |
| AZ | |
| AR | |
| CA | |
| CO | |
| CT | |
| DE | |
| DC | |
| FM | |
| FL | |
| GA | |
| GU | |
| HI | |
| ID | |
| IL | |
| IN | |
| IA | |
| KS | |
| KY | |
| LA | |
| ME | |
| MH | |
| MD | |
| MA | |
| MI | |
| MN | |
| MS | |
| MO | |
| MT | |
| NE | |
| NV | |
| NH | |
| NJ | |
| NM | |
| NY | |
| NC | |
| ND | |
| MP | |
| OH | |
| OK | |
| OR | |
| PW | |
| PA | |
| PR | |
| RI | |
| SC | |
| SD | |
| TN | |
| TX | |
| UT | |
| VT | |
| VI | |
| VA | |
| WA | |
| WV | |
| WI | |
| WY | |
states = | |
[ (Empty, "") | |
, (AL, "Alabama") | |
, (AK, "Alaska") | |
, (AZ, "Arizona") | |
, (AR, "Arkansas") | |
, (CA, "California") | |
, (CO, "Colorado") | |
, (CT, "Connecticut") | |
, (DE, "Delaware") | |
, (DC, "District Of Columbia") | |
, (FL, "Florida") | |
, (GA, "Georgia") | |
, (GU, "Guam") | |
, (HI, "Hawaii") | |
, (ID, "Idaho") | |
, (IL, "Illinois") | |
, (IN, "Indiana") | |
, (IA, "Iowa") | |
, (KS, "Kansas") | |
, (KY, "Kentucky") | |
, (LA, "Louisiana") | |
, (ME, "Maine") | |
, (MD, "Maryland") | |
, (MA, "Massachusetts") | |
, (MI, "Michigan") | |
, (MN, "Minnesota") | |
, (MS, "Mississippi") | |
, (MO, "Missouri") | |
, (MT, "Montana") | |
, (NE, "Nebraska") | |
, (NV, "Nevada") | |
, (NH, "New Hampshire") | |
, (NJ, "New Jersey") | |
, (NM, "New Mexico") | |
, (NY, "New York") | |
, (NC, "North Carolina") | |
, (ND, "North Dakota") | |
, (OH, "Ohio") | |
, (OK, "Oklahoma") | |
, (OR, "Oregon") | |
, (PW, "Palau") | |
, (PA, "Pennsylvania") | |
, (RI, "Rhode Island") | |
, (SC, "South Carolina") | |
, (SD, "South Dakota") | |
, (TN, "Tennessee") | |
, (TX, "Texas") | |
, (UT, "Utah") | |
, (VT, "Vermont") | |
, (VA, "Virginia") | |
, (WA, "Washington") | |
, (WV, "West Virginia") | |
, (WI, "Wisconsin") | |
, (WY, "Wyoming") | |
] | |
medals = | |
[ (0, "") | |
, (1, "Gold") | |
, (2, "Silver") | |
, (3, "Bronze") | |
] | |
view model = | |
div [] | |
[ div [] | |
[ select [] states StateSelected Empty | |
, br [] [] | |
, span [] [ text (toString model.state) ] | |
] | |
, div [] | |
[ select [] medals MedalSelected 0 | |
, br [] [] | |
, span [] [ text (toString model.medal) ] | |
] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Select exposing (select) | |
import Dict exposing (Dict) | |
import Html exposing (Html, Attribute, text) | |
import Html.Attributes exposing (value) | |
import Html.Events exposing (on, targetValue) | |
import Json.Decode as Json | |
toDict : List ( a, String ) -> Dict String ( a, String ) | |
toDict options = | |
List.map (\t -> ( toString (fst t), t )) options | |
|> Dict.fromList | |
onChange : (String -> msg) -> Attribute msg | |
onChange message = | |
on "change" (Json.map message targetValue) | |
handler : (a -> msg) -> Dict String ( a, String ) -> a -> String -> msg | |
handler msg opt default k = | |
case Dict.get k opt of | |
Just v -> | |
msg (fst v) | |
Nothing -> | |
msg default | |
option : ( a, String ) -> Html msg | |
option ( msg, title ) = | |
Html.option [ value (toString msg) ] [ text title ] | |
select : List (Attribute msg) -> List ( a, String ) -> (a -> msg) -> a -> Html msg | |
select attrs opts msg default = | |
let | |
lookup = | |
toDict opts | |
in | |
Html.select ((onChange (handler msg lookup default)) :: attrs) (List.map option opts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment