Last active
August 6, 2019 11:13
-
-
Save ababup1192/30aace0801c06cc42138b7a29784e75a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Main exposing (main) | |
import Browser | |
import Html exposing (Attribute, Html, button, div, input, label, p, text) | |
import Html.Attributes exposing (type_) | |
import Html.Events exposing (on, onClick, targetChecked) | |
import Json.Decode as JD | |
type alias Model = | |
{ checkedValues : List String } | |
initialModel : Model | |
initialModel = | |
{ checkedValues = [] } | |
type Msg | |
= UpdateCheckedValue String Bool | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
UpdateCheckedValue value checked -> | |
if checked then | |
{ model | checkedValues = value :: model.checkedValues } | |
else | |
{ model | checkedValues = List.filter (\v -> not <| v == value) model.checkedValues } | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ input [ type_ "checkbox", onChecked <| UpdateCheckedValue "foo" ] [] | |
, label [] [ text "foo" ] | |
, input [ type_ "checkbox", onChecked <| UpdateCheckedValue "bar" ] [] | |
, label [] [ text "bar" ] | |
, p [] | |
[ text <| | |
String.join ", " model.checkedValues | |
] | |
] | |
main : Program () Model Msg | |
main = | |
Browser.sandbox | |
{ init = initialModel | |
, view = view | |
, update = update | |
} | |
onChecked : (Bool -> msg) -> Attribute msg | |
onChecked onCheckedAction = | |
on "change" <| JD.map onCheckedAction targetChecked |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment